-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathControlFlow.kt
191 lines (154 loc) · 4.21 KB
/
ControlFlow.kt
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
class ControlFlow {
fun test() {
println("If Expression")
val ten = 10
val twenty = 20
val max = getMax(ten, twenty)
println("")
println("When Expression")
print("(when 1)fibonacci array = [")
for (i in 1..10) {
print(fibonacci1(i))
if (i != 10)
print(", ")
}
println("]")
print("(when 2)fibonacci array = [")
for (i in 1..10) {
print(fibonacci2(i))
if (i != 10)
print(", ")
}
println("]")
print("(when 3)")
val taxi = Sedan()
recogniseCar(taxi)
println("")
println("For Expression")
val primes = intArrayOf(2, 3, 5, 7, 11, 13, 17, 23, 29, 31)
print("(If 1)primes array = [")
for (num in primes) {
print("$num ,")
}
println("]")
print("(If 2)primes array = [")
for (index in 0 until primes.size) {
print("${primes[index]} ,")
}
println("]")
print("(If 3)primes array = [")
for (index in primes.indices) {
print("${primes[index]} ,")
}
println("]")
print("(If 4)primes array = [")
primes.forEach {
print("$it ,")
}
println("]")
print("(If 5)primes array = [")
primes.forEachIndexed { index, i ->
print(i)
if (index != primes.lastIndex)
print(", ")
}
println("]")
print("Iterate over 2 numbers from the bottom. Primes array = [")
for (index in primes.size - 1 downTo 0 step 2) {
print("${primes[index]} ,")
}
println("]")
println("")
println("While Expression")
print("Factors of 36 = [")
var factors = findFactors1(36)
factors.forEachIndexed { index, i ->
print(i)
if (index != factors.lastIndex)
print(", ")
}
println("]")
print("Factors of 42 = [")
factors = findFactors2(42)
factors.forEachIndexed { index, i ->
print(i)
if (index != factors.lastIndex)
print(", ")
}
println("]")
}
private fun getMax(num1: Int, num2: Int): Int {
return if (num1 > num2) {
println("$num1 is larger")
num1
} else {
println("$num2 is larger")
num2
}
}
private fun fibonacci1(size: Int): Int {
if (size < 1) {
throw ArrayIndexOutOfBoundsException("size must be positive")
}
return when (size) {
1, 2 -> 1
else -> {
fibonacci1(size - 1) + fibonacci1(size - 2)
}
}
}
private fun fibonacci2(size: Int): Int {
return when {
size < 0 -> throw ArrayIndexOutOfBoundsException("size must be positive")
size == 1 -> 1
size == 2 -> 1
else -> (fibonacci2(size - 1) + fibonacci2(size - 2))
}
}
private fun recogniseCar(car: Car) {
when (car) {
is Sedan -> println("This is a ${car.getName()}")
is Suv -> println("This is a ${car.getName()}")
else -> println("No idea")
}
}
private fun findFactors1(number: Int): IntArray {
var factors = IntArray(0)
var i = 1
while (i <= (number / 2)) {
if (number.rem(i) == 0) {
factors += i
}
i++
}
factors += number
return factors.sortedArray()
}
private fun findFactors2(number: Int): IntArray {
var factors = IntArray(0)
var i = 1
do {
if (number.rem(i) == 0) {
factors += i
}
i++
} while (i <= (number / 2))
factors += number
return factors.sortedArray()
}
}
open class Car {
open fun getName(): String {
return "car"
}
}
class Sedan : Car() {
override fun getName(): String {
return "sedan"
}
}
class Suv : Car() {
override fun getName(): String {
return "suv"
}
}