Add Aufgabe 3 and start swift project

This commit is contained in:
cediackermann 2026-05-11 11:04:17 +02:00
parent 2547bee34d
commit 7afc27e1cd
6 changed files with 285 additions and 0 deletions

8
.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc

24
.vscode/launch.json vendored Normal file
View file

@ -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"
}
]
}

141
03/Aufgabe3.swift Normal file
View file

@ -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))")
}

83
03/PureOrNotPure.md Normal file
View file

@ -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
```

20
Package.swift Normal file
View file

@ -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]
)

9
Sources/m323/m323.swift Normal file
View file

@ -0,0 +1,9 @@
// The Swift Programming Language
// https://docs.swift.org/swift-book
@main
struct m323 {
static func main() {
print("Hello, world!")
}
}