└── GradeCalculator.playground
├── playground.xcworkspace
├── contents.xcworkspacedata
└── xcuserdata
│ └── cor.xcuserdatad
│ └── UserInterfaceState.xcuserstate
├── contents.xcplayground
├── timeline.xctimeline
└── Contents.swift
/GradeCalculator.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/GradeCalculator.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/GradeCalculator.playground/playground.xcworkspace/xcuserdata/cor.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CoR/grade-calculator/master/GradeCalculator.playground/playground.xcworkspace/xcuserdata/cor.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/GradeCalculator.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
10 |
11 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/GradeCalculator.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //: ## Caluclate Grades and manage time more efficient
2 | import Foundation
3 |
4 | struct Grade: CustomStringConvertible {
5 | let score: Double
6 | let weight: Int
7 |
8 | var description: String {
9 | return "score: \(score) weight: \(weight)"
10 | }
11 | }
12 |
13 | struct Subject: CustomStringConvertible {
14 | let name: String
15 | let grades: [Grade]
16 |
17 | var finalGrade: Double {
18 |
19 | let scoreTotal = grades.reduce(0.0) { $0 + $1.score * Double($1.weight) }
20 | let importanceTotal = grades.reduce(0) { $0 + $1.weight }
21 |
22 | return scoreTotal / Double(importanceTotal)
23 |
24 | }
25 |
26 | var roundedFinalGrade: Double {
27 | return Double(round(10*finalGrade)/10)
28 | }
29 |
30 | var description: String {
31 | return "name: \(name), grades: \(grades), finalGrade: \(roundedFinalGrade)"
32 | }
33 |
34 | func requiredGrade(target targetGrade: Double, weight: Int) -> Double {
35 | let scoreTotal = grades.reduce(0.0) { $0 + $1.score * Double($1.weight) }
36 | let weightTotal = grades.reduce(0) { $0 + $1.weight } + weight
37 |
38 | return ((targetGrade * Double(weightTotal)) - scoreTotal) / Double(weight)
39 | }
40 | }
41 |
42 |
43 |
44 | let subjects = [
45 | Subject(name: "Scheikunde", grades: [
46 | Grade(score: 4.2, weight: 2),
47 | Grade(score: 4.8, weight: 1),
48 | Grade(score: 2.1, weight: 2),
49 | Grade(score: 5.6, weight: 2)
50 | ]),
51 | Subject(name: "Wiskunde", grades: [
52 | Grade(score: 7.1, weight: 1),
53 | Grade(score: 6.7, weight: 1),
54 | Grade(score: 8.0, weight: 1),
55 | Grade(score: 2.2, weight: 1)
56 | ]),
57 | Subject(name: "Natuurkunde", grades: [
58 | Grade(score: 7.3, weight: 1),
59 | Grade(score: 6.0, weight: 1),
60 | Grade(score: 4.5, weight: 1),
61 | Grade(score: 7.7, weight: 1)
62 | ]),
63 | Subject(name: "Informatica", grades: [
64 | Grade(score: 9.0, weight: 1),
65 | Grade(score: 9.1, weight: 1),
66 | Grade(score: 8.2, weight: 1),
67 | Grade(score: 8.5, weight: 1)
68 | ]),
69 | Subject(name: "Kunst", grades: [
70 | Grade(score: 8.2, weight: 2),
71 | Grade(score: 7.5, weight: 2),
72 | Grade(score: 4.1, weight: 2),
73 | Grade(score: 2.2, weight: 2)
74 | ]),
75 | Subject(name: "Engels", grades: [
76 | Grade(score: 4.4, weight: 1),
77 | Grade(score: 6.1, weight: 1),
78 | Grade(score: 6.5, weight: 2),
79 | Grade(score: 6.0, weight: 1),
80 | Grade(score: 7.8, weight: 1),
81 | Grade(score: 7.5, weight: 2)
82 | ]),
83 | Subject(name: "Nederlands", grades: [
84 | Grade(score: 7.7, weight: 2),
85 | Grade(score: 6.8, weight: 1),
86 | Grade(score: 6.0, weight: 1),
87 |
88 | ]),
89 | Subject(name: "Godsdienst", grades: [
90 | Grade(score: 8.5, weight: 1),
91 | Grade(score: 6.9, weight: 1),
92 | Grade(score: 6.0, weight: 1),
93 |
94 |
95 | ])
96 | ]
97 |
98 |
99 | let finalGrades = subjects.map { "\($0.name): \($0.roundedFinalGrade)" }
100 |
101 | finalGrades
102 |
103 | let natuurkunde = subjects.filter { $0.name == "Natuurkunde" }.first!
104 | let wiskunde = subjects.filter { $0.name == "Wiskunde" }.first!
105 | let informatica = subjects.filter { $0.name == "Informatica" }.first!
106 | let nederlands = subjects.filter { $0.name == "Nederlands" }.first!
107 | let scheikunde = subjects.filter { $0.name == "Scheikunde" }.first!
108 |
109 |
110 | informatica.requiredGrade(target: 5.5, weight: 1)
111 | wiskunde.requiredGrade(target: 6.0, weight: 1)
112 | natuurkunde.requiredGrade(target: 6.0, weight: 1)
113 | nederlands.requiredGrade(target: 6.0, weight: 1)
114 | scheikunde.requiredGrade(target: 4.5, weight: 2)
115 |
116 |
117 |
118 |
119 |
--------------------------------------------------------------------------------