Skip to content

Commit c9d6604

Browse files
author
Mohammad
committed
Nov 9
1 parent a1f51c3 commit c9d6604

File tree

5 files changed

+48
-23
lines changed

5 files changed

+48
-23
lines changed

src/test/kotlin/com/igorwojda/integer/fibonacci/basic/Challenge.kt

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,19 @@ fun main() {
1616
}*/
1717

1818
private fun fibonacci(n: Int): Int {
19-
if (n < 2) {
20-
return n
21-
}
22-
2319
var first = 0
2420
var second = 1
25-
var current = 0
21+
val listItems: MutableList<Int> = mutableListOf<Int>(first, second)
2622

27-
(2..n).forEach {
28-
println("it value --> $n")
29-
current = first + second
23+
if (n == 0) return 0
24+
(2 until n).forEach { _ ->
25+
val next = first + second
26+
listItems.add(next)
3027
first = second
31-
second = current
32-
println("it current value --> $current")
28+
second = next
3329
}
34-
35-
return current
30+
println("fib list --> $listItems")
31+
return listItems.sum()
3632
}
3733

3834
private class Test {

src/test/kotlin/com/igorwojda/integer/fizzbuzz/Challenge.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,26 @@ private fun fizzBuzz(n: Int): List<String> {
2222
}
2323
}*/
2424

25-
(1 until n +1).forEach {
25+
/*(1 until n +1).forEach {
2626
val item = when {
2727
(it % 3 == 0 && it % 5 == 0) -> { "FizzBuzz" }
2828
(it % 3 == 0) -> { "Fizz" }
2929
(it % 5 == 0) -> { "Buzz" }
3030
else -> { it.toString() }
3131
}
3232
fizzBuzzList.add(item)
33-
}
33+
}*/
3434

35-
return fizzBuzzList
35+
for (i in n downTo 1) {
36+
val item = when {
37+
(i % 3 == 0 && i % 5 == 0) -> { "FizzBuzz" }
38+
(i % 3 == 0) -> { "Fizz" }
39+
(i % 5 == 0) -> { "Buzz" }
40+
else -> { i.toString() }
41+
}
42+
fizzBuzzList.add(item)
43+
}
44+
return fizzBuzzList.reversed()
3645
}
3746

3847
private class Test {

src/test/kotlin/com/igorwojda/integer/generateallpairs/Challenge.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ import org.junit.jupiter.api.Test
66
fun main() {
77
getAllPairs(10)
88
}
9+
910
private fun getAllPairs(n: Int): List<Pair<Int, Int>> {
10-
val listPairValue = arrayListOf<Pair<Int, Int>>()
11-
(0 until n + 1).forEach { i ->
12-
(0 until n + 1).forEach { j ->
13-
listPairValue.add(i to j)
11+
val pairedList: MutableList<Pair<Int, Int>> = mutableListOf()
12+
(0 until n).forEach { i ->
13+
// for every i
14+
(0 until n).forEach { j ->
15+
// take every j and attach
16+
pairedList.add(Pair(i, j))
1417
}
1518
}
16-
return listPairValue
19+
println(pairedList)
20+
return pairedList
1721
}
1822

1923
private class Test {

src/test/kotlin/com/igorwojda/integer/power/Challenge.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ import org.amshove.kluent.shouldBeEqualTo
44
import org.junit.jupiter.api.Test
55

66
private fun power(base: Int, exponent: Int): Int {
7-
TODO("not implemented")
7+
// loop through the exponent and multiply the result variable with base that many times and return
8+
var result = 1
9+
if (base == 0) return 0
10+
for (power in 0 until exponent) {
11+
println("loop data:$power $result")
12+
result *= base
13+
}
14+
return result
815
}
916

1017
private class Test {

src/test/kotlin/com/igorwojda/integer/printnumber/steps/Challenge.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@ package com.igorwojda.integer.printnumber.steps
33
import org.amshove.kluent.shouldBeEqualTo
44
import org.junit.jupiter.api.Test
55

6-
private fun printNumber(n: Int, step: Int = 1): List<Int> {
7-
TODO("not implemented")
6+
private fun printNumber(n: Int, step: Int): List<Int> {
7+
// Use a while loop, that helps to reduce via step while iterating with the reduced value
8+
if (n == 0) return emptyList()
9+
val resultList: MutableList<Int> = mutableListOf<Int>()
10+
var current = n
11+
while(current > 0) {
12+
println(current)
13+
resultList.add(current)
14+
current -= step
15+
}
16+
return resultList
817
}
918

1019
private class Test {

0 commit comments

Comments
 (0)