-
Notifications
You must be signed in to change notification settings - Fork 0
/
9-flipAndFlop.swift
138 lines (97 loc) · 3.24 KB
/
9-flipAndFlop.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
The algorithm consist into fliping coins to the pattern that into a given array the next element will be always a oposite from the before
and counting how many flips was necessary to change to the right pattern.
Examples:
A = [1,0,1,0,1,1] -> [1,0,1,0,1,0], counter of 1
A = [0,1,0] -> [0,1,0], counter of 0
A = [1,1,0,0,1,1] -> [0,1,0,1,0,1] or [1,0,1,0,1,0], both counter of 3
Edge cases:
A = [1,1,0,1,0,1] -> [0,1,0,1,0,1], counter of 1
A = [0,1,1,0,1,0] -> [1,0,1,0,1,0], counter of 2
*/
/*
Initial thinking:
- Simpler
1. starting by the first element, iterate over all elements changing it if not meet the criteria and summing up the counter by 1
2. starting by the first element, flipit, sum 1, then fo the first
3. the result will be the less flip from the two above
*/
// Input is an Array<Int> = example
// Output is a tuple/set, where first element is the correctet Array<Int> and
// The counter meaning the number of flips
func flipAndFlop(_ array: [Int]) -> ([Int], Int) {
guard array.count > 0 else { return ([], 0)}
guard array.count > 1 else { return (array, 0)}
guard array.count >= 3 else {
if (array.first! + array.last! == 2) || (array.first! + array.last! == 0){
var copy = array
copy[0] = copy[0] == 0 ? 1 : 0
return (copy, 1)
} else {
return (array, 0)
}
}
var fixed = array
var counter: Int = 0
for i in 1..<fixed.count-1 {
let flip = fixed[i]
let flop = flip == 1 ? 0 : 1
if fixed[i-1] == flop && fixed[i+1] == flop {
continue
} else {
if fixed[i-1] == flop {
fixed[i+1] = flop
counter += 1
continue
}
if fixed[i+1] == flop {
fixed[i-1] = flop
counter += 1
continue
}
}
}
return (fixed, counter)
}
var example: [Int] = []
var result: ([Int], Int) = ([], 0)
example = [1,0,1,0,1,1]
result = flipAndFlop(example)
print("Original was: \(example)")
print("Corrected Array is: \(result.0)")
print("Flips made was: \(result.1)")
example = []
result = flipAndFlop(example)
print("Original was: \(example)")
print("Corrected Array is: \(result.0)")
print("Flips made was: \(result.1)")
example = [1,0,1]
result = flipAndFlop(example)
print("Original was: \(example)")
print("Corrected Array is: \(result.0)")
print("Flips made was: \(result.1)")
example = [1,0]
result = flipAndFlop(example)
print("Original was: \(example)")
print("Corrected Array is: \(result.0)")
print("Flips made was: \(result.1)")
example = [0,0,1,0,1,1]
result = flipAndFlop(example)
print("Original was: \(example)")
print("Corrected Array is: \(result.0)")
print("Flips made was: \(result.1)")
example = [1,1,1,0,1,0]
result = flipAndFlop(example)
print("Original was: \(example)")
print("Corrected Array is: \(result.0)")
print("Flips made was: \(result.1)")
example = [1,1]
result = flipAndFlop(example)
print("Original was: \(example)")
print("Corrected Array is: \(result.0)")
print("Flips made was: \(result.1)")
example = [0,0]
result = flipAndFlop(example)
print("Original was: \(example)")
print("Corrected Array is: \(result.0)")
print("Flips made was: \(result.1)")