From 04e76f1879b0f3f701ab7e80144a437d69832ddb Mon Sep 17 00:00:00 2001 From: cediackermann Date: Mon, 18 May 2026 08:58:34 +0200 Subject: [PATCH] add more stuff --- .vscode/launch.json | 20 +++ 01/Drei.java | 37 +++-- 01/Eins.java | 34 ++--- 01/ShoppingCartFunctional.java | 32 ++-- 01/ShoppingCartImperative.java | 70 ++++----- 03/Aufgabe3.swift | 144 ------------------ Package.swift | 56 ++++--- {03 => Sources/Aufgabe3}/PureOrNotPure.md | 16 +- Sources/Aufgabe3/main.swift | 144 ++++++++++++++++++ Sources/Aufgabe4/01_MapUebungen/main.swift | 30 ++++ Sources/Aufgabe4/02_FilterUebungen/main.swift | 26 ++++ .../03_MapUndFilterUebungen/main.swift | 62 ++++++++ .../Aufgabe4/04_FoldLeftUebungen/main.swift | 26 ++++ .../Aufgabe4/05_FlatMapUebungen/main.swift | 33 ++++ .../06_ForComprehensionsUebungen/main.swift | 26 ++++ Sources/Aufgabe4/File.txt | 1 + Sources/LeetCode/LeetCode.swift | 58 +++++++ Sources/LeetCode/main.swift | 13 ++ .../WeDontLikeCharA/main.swift | 12 +- Sources/m323/m323.swift | 6 +- Tests/m323Tests/m323Tests.swift | 3 +- 21 files changed, 570 insertions(+), 279 deletions(-) delete mode 100644 03/Aufgabe3.swift rename {03 => Sources/Aufgabe3}/PureOrNotPure.md (88%) create mode 100644 Sources/Aufgabe3/main.swift create mode 100644 Sources/Aufgabe4/01_MapUebungen/main.swift create mode 100644 Sources/Aufgabe4/02_FilterUebungen/main.swift create mode 100644 Sources/Aufgabe4/03_MapUndFilterUebungen/main.swift create mode 100644 Sources/Aufgabe4/04_FoldLeftUebungen/main.swift create mode 100644 Sources/Aufgabe4/05_FlatMapUebungen/main.swift create mode 100644 Sources/Aufgabe4/06_ForComprehensionsUebungen/main.swift create mode 100644 Sources/Aufgabe4/File.txt create mode 100644 Sources/LeetCode/LeetCode.swift create mode 100644 Sources/LeetCode/main.swift rename 02/WeDontLikeCharA.swift => Sources/WeDontLikeCharA/main.swift (57%) diff --git a/.vscode/launch.json b/.vscode/launch.json index 8960b4e..8c3f0f0 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -59,6 +59,26 @@ "target": "Aufgabe3", "configuration": "release", "preLaunchTask": "swift: Build Release Aufgabe3" + }, + { + "type": "swift", + "request": "launch", + "args": [], + "cwd": "${workspaceFolder:m323}", + "name": "Debug 01_MapUebungen", + "target": "01_MapUebungen", + "configuration": "debug", + "preLaunchTask": "swift: Build Debug 01_MapUebungen" + }, + { + "type": "swift", + "request": "launch", + "args": [], + "cwd": "${workspaceFolder:m323}", + "name": "Release 01_MapUebungen", + "target": "01_MapUebungen", + "configuration": "release", + "preLaunchTask": "swift: Build Release 01_MapUebungen" } ] } diff --git a/01/Drei.java b/01/Drei.java index db6376a..6d3e7f5 100644 --- a/01/Drei.java +++ b/01/Drei.java @@ -3,26 +3,25 @@ import java.util.List; public class Drei { - public int getTipPercentage(List names) { - if (names.size() <= 2) { - return 0; - } else if (names.size() <= 4) { - return 10; - } else { - return 20; - } + public int getTipPercentage(List names) { + if (names.size() <= 2) { + return 0; + } else if (names.size() <= 4) { + return 10; + } else { + return 20; } + } - public static void main(String[] args) { - Drei tipCalculator = new Drei(); - List names = new ArrayList<>(); - names.add("test"); - names.add("test2"); - names.add("test3"); - names.add("test4"); - names.add("test5"); - - System.out.println(tipCalculator.getTipPercentage(names)); - } + public static void main(String[] args) { + Drei tipCalculator = new Drei(); + List names = new ArrayList<>(); + names.add("test"); + names.add("test2"); + names.add("test3"); + names.add("test4"); + names.add("test5"); + System.out.println(tipCalculator.getTipPercentage(names)); + } } diff --git a/01/Eins.java b/01/Eins.java index 63c8152..d05e0b0 100644 --- a/01/Eins.java +++ b/01/Eins.java @@ -1,23 +1,23 @@ public class Eins { - public static void main(String[] args) { - System.out.println(calculateScore("imperative") == 9); - System.out.println(calculateScore("no") == 2); - System.out.println(wordScore("declarative") == 9); - System.out.println(wordScore("yes") == 3); - } + public static void main(String[] args) { + System.out.println(calculateScore("imperative") == 9); + System.out.println(calculateScore("no") == 2); + System.out.println(wordScore("declarative") == 9); + System.out.println(wordScore("yes") == 3); + } - public static int calculateScore(String word) { - int score = 0; - for (char c : word.toCharArray()) { - if (c != 'a') { - score++; - } - } - return score; + public static int calculateScore(String word) { + int score = 0; + for (char c : word.toCharArray()) { + if (c != 'a') { + score++; + } } + return score; + } - public static int wordScore(String word) { - return word.replace("a", "").length(); - } + public static int wordScore(String word) { + return word.replace("a", "").length(); + } } diff --git a/01/ShoppingCartFunctional.java b/01/ShoppingCartFunctional.java index 02160f1..765d548 100644 --- a/01/ShoppingCartFunctional.java +++ b/01/ShoppingCartFunctional.java @@ -3,23 +3,21 @@ import java.util.List; public class ShoppingCartFunctional { - /** - * Pure function that calculates the discount percentage based on the cart content. - * @param cart The list of items in the shopping cart. - * @return 5 if a book is present, 0 otherwise. - */ - public static double getDiscountPercentage(List cart) { - return cart.stream() - .anyMatch(item -> item.toLowerCase().contains("book")) - ? 5 - : 0; - } + /** + * Pure function that calculates the discount percentage based on the cart content. + * + * @param cart The list of items in the shopping cart. + * @return 5 if a book is present, 0 otherwise. + */ + public static double getDiscountPercentage(List cart) { + return cart.stream().anyMatch(item -> item.toLowerCase().contains("book")) ? 5 : 0; + } - public static void main(String[] args) { - List cartWithBook = Arrays.asList("Apple", "Java Book"); - List cartWithoutBook = Arrays.asList("Apple", "Orange"); + public static void main(String[] args) { + List cartWithBook = Arrays.asList("Apple", "Java Book"); + List cartWithoutBook = Arrays.asList("Apple", "Orange"); - System.out.println("Discount with book: " + getDiscountPercentage(cartWithBook)); - System.out.println("Discount without book: " + getDiscountPercentage(cartWithoutBook)); - } + System.out.println("Discount with book: " + getDiscountPercentage(cartWithBook)); + System.out.println("Discount without book: " + getDiscountPercentage(cartWithoutBook)); + } } diff --git a/01/ShoppingCartImperative.java b/01/ShoppingCartImperative.java index f19feb3..90d5540 100644 --- a/01/ShoppingCartImperative.java +++ b/01/ShoppingCartImperative.java @@ -2,47 +2,47 @@ import java.util.ArrayList; import java.util.List; public class ShoppingCartImperative { - private List items = new ArrayList<>(); - private boolean bookAdded = false; + private List items = new ArrayList<>(); + private boolean bookAdded = false; - public void addItem(String item) { - items.add(item); - if (item.toLowerCase().contains("book")) { - bookAdded = true; - } else { - bookAdded = false; - } + public void addItem(String item) { + items.add(item); + if (item.toLowerCase().contains("book")) { + bookAdded = true; + } else { + bookAdded = false; } + } - public void removeItem(String item) { - items.remove(item); - for (String remainingItem : items) { - if (remainingItem.toLowerCase().contains("book")) { - bookAdded = true; - return; - } else { - bookAdded = false; - } - } + public void removeItem(String item) { + items.remove(item); + for (String remainingItem : items) { + if (remainingItem.toLowerCase().contains("book")) { + bookAdded = true; + return; + } else { + bookAdded = false; + } } + } - public List getItems() { - return new ArrayList<>(items); - } + public List getItems() { + return new ArrayList<>(items); + } - public double getDiscount() { - return bookAdded ? 5 : 0; - } + public double getDiscount() { + return bookAdded ? 5 : 0; + } - public static void main(String[] args) { - ShoppingCartImperative cart = new ShoppingCartImperative(); - cart.addItem("Apple"); - cart.addItem("Java Book"); - System.out.println("Items: " + cart.getItems()); - System.out.println("Discount: " + cart.getDiscount()); + public static void main(String[] args) { + ShoppingCartImperative cart = new ShoppingCartImperative(); + cart.addItem("Apple"); + cart.addItem("Java Book"); + System.out.println("Items: " + cart.getItems()); + System.out.println("Discount: " + cart.getDiscount()); - cart.removeItem("Java Book"); - System.out.println("Items after removal: " + cart.getItems()); - System.out.println("Discount after removal: " + cart.getDiscount()); - } + cart.removeItem("Java Book"); + System.out.println("Items after removal: " + cart.getItems()); + System.out.println("Discount after removal: " + cart.getDiscount()); + } } diff --git a/03/Aufgabe3.swift b/03/Aufgabe3.swift deleted file mode 100644 index 6a5b5bc..0000000 --- a/03/Aufgabe3.swift +++ /dev/null @@ -1,144 +0,0 @@ -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] { - let itemsCopy = items - return itemsCopy.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] { - let itemsCopy = items - - return itemsCopy.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/Package.swift b/Package.swift index 84c75c2..e1577ad 100644 --- a/Package.swift +++ b/Package.swift @@ -1,30 +1,36 @@ -// swift-tools-version: 6.3 -// The swift-tools-version declares the minimum version of Swift required to build this package. +// swift-tools-version: 6.0 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" - ), - .executableTarget( - name: "WeDontLikeCharA", - path: "02", - sources: ["WeDontLikeCharA.swift"] - ), - .executableTarget( - name: "Aufgabe3", - path: "03", - sources: ["Aufgabe3.swift"] - ), - .testTarget( - name: "m323Tests", - dependencies: ["m323"] - ), - ], - swiftLanguageModes: [.v6] + name: "m323", + platforms: [.macOS(.v15)], + targets: [ + .executableTarget( + name: "m323", + path: "Sources/m323" + ), + .executableTarget( + name: "WeDontLikeCharA", + path: "Sources/WeDontLikeCharA" + ), + .executableTarget( + name: "Aufgabe3", + path: "Sources/Aufgabe3", + exclude: ["PureOrNotPure.md"] + ), + .executableTarget(name: "01_MapUebungen", path: "Sources/Aufgabe4/01_MapUebungen"), + .executableTarget(name: "02_FilterUebungen", path: "Sources/Aufgabe4/02_FilterUebungen"), + .executableTarget( + name: "03_MapUndFilterUebungen", path: "Sources/Aufgabe4/03_MapUndFilterUebungen"), + .executableTarget(name: "04_FoldLeftUebungen", path: "Sources/Aufgabe4/04_FoldLeftUebungen"), + .executableTarget(name: "05_FlatMapUebungen", path: "Sources/Aufgabe4/05_FlatMapUebungen"), + .executableTarget( + name: "06_ForComprehensionsUebungen", path: "Sources/Aufgabe4/06_ForComprehensionsUebungen"), + .executableTarget(name: "LeetCode", path: "Sources/LeetCode"), + .testTarget( + name: "m323Tests", + dependencies: ["m323"] + ), + ] ) diff --git a/03/PureOrNotPure.md b/Sources/Aufgabe3/PureOrNotPure.md similarity index 88% rename from 03/PureOrNotPure.md rename to Sources/Aufgabe3/PureOrNotPure.md index 61b8ebd..8ab812a 100644 --- a/03/PureOrNotPure.md +++ b/Sources/Aufgabe3/PureOrNotPure.md @@ -4,9 +4,9 @@ | ------- | -------------------- | ------------------------------------------------ | ----------------------------------- | ---------------- | | 1.1 | Ja | Nein | Nein | impure | | 1.2 | Ja | Ja | Ja | pure | -| 1.3 | Ja | ja | Ja | impure | +| 1.3 | Ja | ja | Ja | pure | | 1.4 | Ja | Nein | Ja | impure | -| 1.5 | Ja | Ja | Ja | impure | +| 1.5 | Ja | Ja | Ja | pure | | 1.6 | Ja | Ja | Nein | impure | ## Aufgabe 1.1 @@ -40,11 +40,7 @@ console.log(add(2, 4)); // Ausgabe: 6 ```js function firstCharacter(str) { - if (!len(str) > 0) { - return str.charAt(0); - } else { - return ""; - } + return str.charAt(0); } console.log(firstCharacter("Hello")); // Ausgabe: H @@ -68,11 +64,7 @@ console.log(multiplyWithRandom(10, Math.random())); // Ausgabe: Eine zufällige ```js // Funktion zum Teilen einer Zahl durch eine andere function divideNumbers(dividend, divisor) { - if (dividen != 0 && divisor != 0) { - return dividend / divisor; - } else { - return 0; - } + return dividend / divisor; } console.log(divideNumbers(10, 2)); // Ausgabe: 5 diff --git a/Sources/Aufgabe3/main.swift b/Sources/Aufgabe3/main.swift new file mode 100644 index 0000000..b457566 --- /dev/null +++ b/Sources/Aufgabe3/main.swift @@ -0,0 +1,144 @@ +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] { + let itemsCopy = items + return itemsCopy.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] { + let itemsCopy = items + + return itemsCopy.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/Sources/Aufgabe4/01_MapUebungen/main.swift b/Sources/Aufgabe4/01_MapUebungen/main.swift new file mode 100644 index 0000000..abcdeb2 --- /dev/null +++ b/Sources/Aufgabe4/01_MapUebungen/main.swift @@ -0,0 +1,30 @@ +// +// 01_MapUebungen.swift +// m323 +// +// Created by cediackermann on 18.05.2026. +// + +// Aufgabe 1 + +let numberList = [1, 2, 3, 4, 5] + +func doubleListItems(numbers: [Int]) -> [Int] { + let doubleList = numbers.map { $0 * 2 } + return doubleList +} + +print(doubleListItems(numbers: numberList)) + +// Aufgabe 2 + +let nameList = ["Alice", "Bob", "Charlie"] + +func makeUpperCase(list: [String]) -> [String] { + let upperCaseList = list.map { $0.uppercased() } + return upperCaseList +} + +print(makeUpperCase(list: nameList)) + +// Aufgabe 3 diff --git a/Sources/Aufgabe4/02_FilterUebungen/main.swift b/Sources/Aufgabe4/02_FilterUebungen/main.swift new file mode 100644 index 0000000..e1bc5f2 --- /dev/null +++ b/Sources/Aufgabe4/02_FilterUebungen/main.swift @@ -0,0 +1,26 @@ +// +// main.swift +// m323 +// +// Created by cediackermann on 18.05.2026. +// + +// Aufgabe 1 + +let numberList = [1, 2, 3, 4, 5] + +func getEvenNumbers(list: [Int]) -> [Int] { + let filteredList = list.filter { $0 % 2 == 0 } + return filteredList +} + +print(getEvenNumbers(list: numberList)) + +let nameList = ["Alice", "Bob", "Charlie", "Diana"] + +func getLongStrings(list: [String]) -> [String] { + let filteredList = list.filter { $0.count > 4 } + return filteredList +} + +print(getLongStrings(list: nameList)) diff --git a/Sources/Aufgabe4/03_MapUndFilterUebungen/main.swift b/Sources/Aufgabe4/03_MapUndFilterUebungen/main.swift new file mode 100644 index 0000000..cc9cb59 --- /dev/null +++ b/Sources/Aufgabe4/03_MapUndFilterUebungen/main.swift @@ -0,0 +1,62 @@ +import Foundation + +// +// main.swift +// m323 +// +// Created by cediackermann on 18.05.2026. +// + +// Aufgabe 1 +struct Employee { + var name: String + var department: String + var salary: Int +} + +let employees: [Employee] = [ + Employee(name: "Max Mustermann", department: "IT", salary: 50000), + Employee(name: "Erika Musterfrau", department: "Marketing", salary: 45000), + Employee(name: "Klaus Klein", department: "IT", salary: 55000), + Employee(name: "Julia Gross", department: "HR", salary: 40000), +] + +func extractHighEarningITPersonnel(employees: [Employee]) -> [Employee] { + let highEarningITPersonnel = employees.filter { + $0.department == "IT" && $0.salary >= 50000 + } + + let updatedPersonnel = highEarningITPersonnel.map { employee -> Employee in + var updatedEmployee = employee // Create a mutable copy + let firstName = updatedEmployee.name.split(separator: " ", maxSplits: 1).first ?? "" + updatedEmployee.name = firstName.uppercased() + return updatedEmployee + } + + return updatedPersonnel +} + +print(extractHighEarningITPersonnel(employees: employees)) + +// Aufgabe 2 + +let courseList = [ + "Programmierung in Scala", + "Datenbanken", + "Webentwicklung mit JavaScript", + "Algorithmen und Datenstrukturen", +] + +func filterCourses(courses: [String]) -> [String] { + let processedCourses = + courses + .filter { !$0.contains("Daten") } + .map { + $0.replacingOccurrences(of: " ", with: "") + } + .sorted() + .sorted(by: >) + return processedCourses +} + +print(filterCourses(courses: courseList)) diff --git a/Sources/Aufgabe4/04_FoldLeftUebungen/main.swift b/Sources/Aufgabe4/04_FoldLeftUebungen/main.swift new file mode 100644 index 0000000..1417380 --- /dev/null +++ b/Sources/Aufgabe4/04_FoldLeftUebungen/main.swift @@ -0,0 +1,26 @@ +// +// main.swift +// m323 +// +// Created by cediackermann on 18.05.2026. +// + +// Aufgabe 1 + +let numberList = [1, 2, 3, 4, 5] + +func foldLeft(list: [Int]) -> Int { + return list.reduce(0, +) +} + +print(foldLeft(list: numberList)) + +// Aufgabe 2 + +let stringList = ["Hallo", " ", "Welt", "!"] + +func foldStringsLeft(list: [String]) -> String { + return list.reduce("", +) +} + +print(foldStringsLeft(list: stringList)) diff --git a/Sources/Aufgabe4/05_FlatMapUebungen/main.swift b/Sources/Aufgabe4/05_FlatMapUebungen/main.swift new file mode 100644 index 0000000..0b90763 --- /dev/null +++ b/Sources/Aufgabe4/05_FlatMapUebungen/main.swift @@ -0,0 +1,33 @@ +import Foundation + +// +// main.swift +// m323 +// +// Created by cediackermann on 18.05.2026. +// + +// Aufgabe 1 + +let list: [[Int]] = [[1, 2], [3, 4], [5, 6]] + +func getFlatMap(list: [[Int]]) -> [Int] { + return list.flatMap { $0 } +} + +print(getFlatMap(list: list)) + +// Aufgabe 2 + +let dictionary: [String: [String]] = [ + "Max": ["Blau", "Grün"], + "Anna": ["Rot"], + "Julia": ["Gelb", "Blau", "Grün"], +] + +func getDistinctColors(list: [String: [String]]) -> [String] { + let flattenedColors = list.flatMap { $0.1 } + return Array(Set(flattenedColors)) +} + +print(getDistinctColors(list: dictionary)) diff --git a/Sources/Aufgabe4/06_ForComprehensionsUebungen/main.swift b/Sources/Aufgabe4/06_ForComprehensionsUebungen/main.swift new file mode 100644 index 0000000..d14286e --- /dev/null +++ b/Sources/Aufgabe4/06_ForComprehensionsUebungen/main.swift @@ -0,0 +1,26 @@ +// +// main.swift +// m323 +// +// Created by cediackermann on 18.05.2026. +// + +// Aufgabe 1 + +for number in 1...10 { + print(number * number) +} + +// Aufgabe 2 + +let evenNumbers: [Int] = { + var numbers: [Int] = [] + for number in 1...20 { + if number % 2 == 0 { + numbers.append(number) + } + } + return numbers +}() + +print(evenNumbers) diff --git a/Sources/Aufgabe4/File.txt b/Sources/Aufgabe4/File.txt new file mode 100644 index 0000000..452b70e --- /dev/null +++ b/Sources/Aufgabe4/File.txt @@ -0,0 +1 @@ +05_FlatMapUebungen \ No newline at end of file diff --git a/Sources/LeetCode/LeetCode.swift b/Sources/LeetCode/LeetCode.swift new file mode 100644 index 0000000..4fff6b2 --- /dev/null +++ b/Sources/LeetCode/LeetCode.swift @@ -0,0 +1,58 @@ +import Foundation + +// +// LeetCode.swift +// m323 +// +// Created by cediackermann on 18.05.2026. +// + +class LeetCode { + func hammingWeight(_ n: Int) -> Int { + var weight = 0 + let quotient = n / 2 + weight += n % 2 + let roundedQuotient = floor(Double(quotient)) + if roundedQuotient != 0 { + weight += hammingWeight(Int(roundedQuotient)) + } + + return weight + } + + func singleNumber(_ nums: [Int]) -> Int { + var result = 0 + for num in nums { + result ^= num + } + return result + } + + func containsDuplicate(_ nums: [Int]) -> Bool { + return nums.count != Set(nums).count + } + + func sortedSquares(_ nums: [Int]) -> [Int] { + return nums.map { $0 * $0 }.sorted() + } + + func productExceptSelf(_ nums: [Int]) -> [Int] { + var result = [Int](repeating: 1, count: nums.count) + var prefix = [Int](repeating: 1, count: nums.count) + var suffix = [Int](repeating: 1, count: nums.count) + var runningProduct = 1 + for i in 0.. $1.score } for entry in sortedWords { - print("\(entry.word): \(entry.score) points") + print("\(entry.word): \(entry.score) points") } diff --git a/Sources/m323/m323.swift b/Sources/m323/m323.swift index 4ddd261..ac24d39 100644 --- a/Sources/m323/m323.swift +++ b/Sources/m323/m323.swift @@ -3,7 +3,7 @@ @main struct m323 { - static func main() { - print("Hello, world!") - } + static func main() { + print("Hello, world!") + } } diff --git a/Tests/m323Tests/m323Tests.swift b/Tests/m323Tests/m323Tests.swift index b503c0d..6d1b149 100644 --- a/Tests/m323Tests/m323Tests.swift +++ b/Tests/m323Tests/m323Tests.swift @@ -1,6 +1,7 @@ import Testing + @testable import m323 @Test func example() async throws { - // Write your test here and use APIs like `#expect(...)` to check expected conditions. + // Write your test here and use APIs like `#expect(...)` to check expected conditions. }