-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-baseballGameFromTuring.swift
59 lines (49 loc) · 1.87 KB
/
2-baseballGameFromTuring.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
A crazy baseball game. The game consists of several rounds, past rounds may affect future rounds.
Case integer: store an integer on the array
Case "+": Sum the previous two values
Case "D": Double the previous value
Case C: Invalidates the previous value
Ex. ops = ["5", "2", "C", "D", "+"] => 30
Explanation:
"5" - Add 5 to the record, record is now 5.
"2" - Add 2 th the record, record is now [5, 2]
"C" - Invalidate and remove the previous score, record is now [5]
"D" - Doubles the previous value, record is now [5, 10]
"+" - Sum the two previous values, record is now [5, 10, 15]
Result is the sum of the whole array.
Output = 30
*/
// This is a classical example of stack data structure
//let ops = ["5", "2", "C", "D", "+"]
let ops: [String] = ["5", "-2", "4", "C", "D", "9", "+", "+"]
//let ops: [String] = ["1"]
func calculateCrazyScore(of operations: [String]) -> Int {
var stack: [String] = [] // stack initialized empty
for op in operations {
switch op {
case "+": // pop two values and put then back
let plusOp1 = stack.removeLast()
let plusOp2 = stack.removeLast()
let plusIntOp1 = Int(plusOp1)! // I can do that cause the problem make sure that has two integer values when + is encountered
let plusIntOp2 = Int(plusOp2)!
let plusTempResult = plusIntOp1 + plusIntOp2
stack.append(plusOp2)
stack.append(plusOp1)
stack.append(String(plusTempResult))
case "C":
let _ = stack.removeLast()
case "D":
let doubleOp1 = stack.removeLast()
let doubleIntOp1 = Int(doubleOp1)!
let doubleTempResult = 2 * doubleIntOp1
stack.append(doubleOp1)
stack.append(String(doubleTempResult))
default:
stack.append(op)
}
print("Stack: \(stack)")
}
return stack.reduce(0, { sum, op in return sum + Int(op)!})
}
print(calculateCrazyScore(of: ops))