-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercises.swift
159 lines (127 loc) · 3.39 KB
/
Exercises.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Printing the running total of a list
func printRunningTotal(list: [Int]) {
var runningTotal = 0
for number in list {
runningTotal += number
print(runningTotal)
}
}
printRunningTotal(list: [2,4,0,2,6])
// Compute the first 100 Fibbonacci numbers
func fibbonacci(limit: Int) -> [Int] {
var numbers = [0, 1]
for _ in 0..<limit {
let v1 = numbers[numbers.count - 2]
let v2 = numbers[numbers.count - 1]
numbers.append(v1 + v2)
}
return numbers
}
fibbonacci(limit: 5)
// Rotates an array by k elements.
func rotateArray(array: [Int], number: Int) -> [Int] {
// 'Int' could just as easily be 'Any'
if number > array.count {
fatalError("Can't rotate an array by more than its length")
}
let firstSegment = array.prefix(upTo: number)
let secondSegment = array.suffix(from: number)
return Array(secondSegment + firstSegment)
}
rotateArray(array: [1,2,3,4,5,6], number: 2)
// Converts a string to Morse code
func morseCode(input: String) -> String {
let morseCode = [
" ": " ",
"A": ".-",
"B": "-...",
"C": "-.-.",
"D": "-..",
"E": ".",
"F": "..-.",
"G": "--.",
"H": "....",
"I": "..",
"J": ".---",
"K": "-.-",
"L": ".-..",
"M": "--",
"N": "-.",
"O": "---",
"P": ".--.",
"Q": "--.-",
"R": ".-.",
"S": "...",
"T": "-",
"U": "..-",
"V": "...-",
"W": ".--",
"X": "-..-",
"Y": "-.--",
"Z": "--..",
"0": "-----",
"1": ".----",
"2": "..---",
"3": "...--",
"4": "....-",
"5": ".....",
"6": "-....",
"7": "--...",
"8": "---..",
"9": "----.",
".": ".-.-.-",
",": "--..--",
":": "---...",
"?": "..--..",
"'": ".----.",
"-": "-....-",
"/": "-..-.",
"(": "-.--.-",
")": "-.--.-",
"\"": ".-..-.",
"@": ".--.-.",
"=": "-...-",
"!": "-.-.--"
]
var result : [String] = []
for character in input {
let morseSequence = morseCode[character.uppercased()] ?? "<unknown>"
result.append(morseSequence)
}
return result.joined(separator: " ")
}
morseCode(input: "Hello ACEM")
// Removes duplicates from a list
func removeDuplicates(array: [String]) -> [String] {
// 'String' could be any hashable
var itemsSeen : [String] = []
// Could also be a Set
var result : [String] = []
for item in array {
if itemsSeen.contains(item) == false {
itemsSeen.append(item)
result.append(item)
}
}
return result
}
removeDuplicates(array: ["Hello", "yes", "yes", "Hello", "World"])
// Tests the Collatz conjecture on an integer. Returns the number of steps needed to reduce to 1.
// https://en.wikipedia.org/wiki/Collatz_conjecture
func collatz(input: Int) -> Int {
guard input > 0 else {
fatalError("Input must be positive.")
}
var current = input
var steps = 0
while current != 1 {
if current % 2 == 0 {
current = current / 2
} else {
current = current * 3 + 1
}
steps += 1
}
return steps
}
collatz(input: 9)