From 7afc27e1cd216643ebc096346e0e51a5d49d20e6 Mon Sep 17 00:00:00 2001 From: cediackermann Date: Mon, 11 May 2026 11:04:17 +0200 Subject: [PATCH] Add Aufgabe 3 and start swift project --- .gitignore | 8 +++ .vscode/launch.json | 24 +++++++ 03/Aufgabe3.swift | 141 ++++++++++++++++++++++++++++++++++++++++ 03/PureOrNotPure.md | 83 +++++++++++++++++++++++ Package.swift | 20 ++++++ Sources/m323/m323.swift | 9 +++ 6 files changed, 285 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 03/Aufgabe3.swift create mode 100644 03/PureOrNotPure.md create mode 100644 Package.swift create mode 100644 Sources/m323/m323.swift diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0023a53 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +/.build +/Packages +xcuserdata/ +DerivedData/ +.swiftpm/configuration/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..e4eaac7 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "configurations": [ + { + "type": "swift", + "request": "launch", + "args": [], + "cwd": "${workspaceFolder:m323}", + "name": "Debug m323", + "target": "m323", + "configuration": "debug", + "preLaunchTask": "swift: Build Debug m323" + }, + { + "type": "swift", + "request": "launch", + "args": [], + "cwd": "${workspaceFolder:m323}", + "name": "Release m323", + "target": "m323", + "configuration": "release", + "preLaunchTask": "swift: Build Release m323" + } + ] +} diff --git a/03/Aufgabe3.swift b/03/Aufgabe3.swift new file mode 100644 index 0000000..933e2ef --- /dev/null +++ b/03/Aufgabe3.swift @@ -0,0 +1,141 @@ +import Foundation + +// 3.1 +func calculateSum(items: [Int]) -> Int { + var sum = 0 + for item in items { + sum += item + } + return sum +} + +let items = [1, 2, 3, 4, 5] +print(calculateSum(items: items)) + +// 3.2 +func calculateAverage(items: [Int]) -> Int { + let sum = items.reduce(0, +) + + if !items.isEmpty { + let avg = sum / items.count + return avg + } + + return 0 +} + +print(calculateAverage(items: items)) + +// 3.3 + +func sortListAlphabetical(items: [String]) -> [String] { + return items.sorted() +} + +let stringItems = ["ABC", "CDF", "BDC"] + +print(sortListAlphabetical(items: stringItems)) + +// 3.4 + +struct ComplexType { + var date = Date() + var priority: Int + var title: String +} + +func sortComplexType(items: [ComplexType]) -> [ComplexType] { + return items.sorted { + ($0.date, $0.priority, $0.title) < ($1.date, $1.priority, $1.title) + } +} + +let now = Date() +let calendar = Calendar.current + +let sampleData = [ + ComplexType( + date: now, + priority: 1, + title: "Submit Quarterly Report" + ), + ComplexType( + date: calendar.date(byAdding: .day, value: -1, to: now)!, + priority: 2, + title: "Review Pull Request" + ), + ComplexType( + date: calendar.date(byAdding: .day, value: 1, to: now)!, + priority: 1, + title: "Project Kickoff Meeting" + ), + ComplexType( + date: now, + priority: 3, + title: "Update Documentation" + ), + ComplexType( + date: calendar.date(byAdding: .hour, value: -5, to: now)!, + priority: 2, + title: "Check System Logs" + ), +] + +print(sortComplexType(items: sampleData)) + +// Aufgabe 3.5 + +struct Tree { + var branches: [Branch] +} + +struct Branch { + var branches: [Branch] + var leaves: [Leaf] +} + +struct Leaf { + var color: String + var name: String +} + +let leafA = Leaf(color: "Green", name: "Oak Leaf Alpha") +let leafB = Leaf(color: "Yellow", name: "Oak Leaf Beta") +let leafC = Leaf(color: "Green", name: "Oak Leaf Gamma") + +let smallBranch = Branch( + branches: [], + leaves: [leafC] +) + +let mainBranch1 = Branch( + branches: [smallBranch], // Contains the sub-branch + leaves: [leafA] +) + +let mainBranch2 = Branch( + branches: [], + leaves: [leafB] +) + +let myTree = Tree(branches: [mainBranch1, mainBranch2]) + +func getLeaves(tree: Tree) -> [Leaf] { + var leaves = [Leaf]() + var stack = tree.branches + while !stack.isEmpty { + let currentBranch = stack.removeLast() + + leaves.append(contentsOf: currentBranch.leaves) + + stack.append(contentsOf: currentBranch.branches) + + } + + return leaves +} + +let result = getLeaves(tree: myTree) +for leaf in result { + print("\(leaf.name) (\(leaf.color))") +} diff --git a/03/PureOrNotPure.md b/03/PureOrNotPure.md new file mode 100644 index 0000000..8ab812a --- /dev/null +++ b/03/PureOrNotPure.md @@ -0,0 +1,83 @@ +# Pure or not Pure + +| Aufgabe | Nur ein Rückgabewert | Resultat nur Abhängig von übergebenen Parametern | Verändert keine existierenden Werte | pure oder impure | +| ------- | -------------------- | ------------------------------------------------ | ----------------------------------- | ---------------- | +| 1.1 | Ja | Nein | Nein | impure | +| 1.2 | Ja | Ja | Ja | pure | +| 1.3 | Ja | ja | Ja | pure | +| 1.4 | Ja | Nein | Ja | impure | +| 1.5 | Ja | Ja | Ja | pure | +| 1.6 | Ja | Ja | Nein | impure | + +## Aufgabe 1.1 + +```js +function addToCartItems(items, item) { + return [...items, item]; +} + +let cartItems = []; +cartItems = addToCartItems(cartItems, "Apple"); +console.log(cartItems); // Ausgabe: ['Apple'] +cartItems = addToCartItems(cartItems, "Banana"); +console.log(cartItems); // Ausgabe: ['Apple', 'Banana'] +cartItems = addToCartItems(cartItems, "Orange"); +console.log(cartItems); // Ausgabe: ['Apple', 'Banana', 'Orange'] +``` + +## Aufgabe 1.2 + +```js +function add(a, b) { + return a + b; +} + +console.log(add(5, 3)); // Ausgabe: 8 +console.log(add(2, 4)); // Ausgabe: 6 +``` + +## Aufgabe 1.3 + +```js +function firstCharacter(str) { + return str.charAt(0); +} + +console.log(firstCharacter("Hello")); // Ausgabe: H +console.log(firstCharacter("JavaScript")); // Ausgabe: J +``` + +## Aufgabe 1.4 + +```js +// Methode, um eine Zahl mit einem zufälligen Wert zu multiplizieren +function multiplyWithRandom(number, randomValue) { + return number * randomValue; +} + +console.log(multiplyWithRandom(5, Math.random())); // Ausgabe: Eine zufällige Zahl zwischen 0 und 5 +console.log(multiplyWithRandom(10, Math.random())); // Ausgabe: Eine zufällige Zahl zwischen 0 und 10 +``` + +## Aufgabe 1.5 + +```js +// Funktion zum Teilen einer Zahl durch eine andere +function divideNumbers(dividend, divisor) { + return dividend / divisor; +} + +console.log(divideNumbers(10, 2)); // Ausgabe: 5 +console.log(divideNumbers(8, 4)); // Ausgabe: 2 +``` + +## Aufgabe 1.6 + +```js +// Methode zum Ausgeben und Rückgeben einer Zeichenkette +function printAndReturnString(str) { + return str; // Rückgabe der Zeichenkette +} + +console.log(printAndReturnString("Hello")); // Ausgabe: Hello +``` diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..140689d --- /dev/null +++ b/Package.swift @@ -0,0 +1,20 @@ +// swift-tools-version: 6.3 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "m323", + targets: [ + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. + .executableTarget( + name: "m323" + ), + .testTarget( + name: "m323Tests", + dependencies: ["m323"] + ), + ], + swiftLanguageModes: [.v6] +) diff --git a/Sources/m323/m323.swift b/Sources/m323/m323.swift new file mode 100644 index 0000000..4ddd261 --- /dev/null +++ b/Sources/m323/m323.swift @@ -0,0 +1,9 @@ +// The Swift Programming Language +// https://docs.swift.org/swift-book + +@main +struct m323 { + static func main() { + print("Hello, world!") + } +}