Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bdc150b

Browse files
412: Fizz Buzz
1 parent f2f9a5e commit bdc150b

File tree

5 files changed

+100
-5
lines changed

5 files changed

+100
-5
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ My solutions to LeetCode problems in Kotlin.
2121
| [186](https://leetcode.com/problems/reverse-words-in-a-string-ii/) | [Reverse Words in a String II](src/main/kotlin/com/schmoczer/leetcode/_0186/ReverseWordsInStringInPlace.kt) | Medium |
2222
| [206](https://leetcode.com/problems/reverse-linked-list/) | [Reverse Linked List](src/main/kotlin/com/schmoczer/leetcode/_0206/ReverseLinkedList.kt) | Easy |
2323
| [344](https://leetcode.com/problems/reverse-string/) | [Reverse String](src/main/kotlin/com/schmoczer/leetcode/_0344/ReverseString.kt) | Easy |
24+
| [412](https://leetcode.com/problems/fizz-buzz/) | [Fizz Buzz](src/main/kotlin/com/schmoczer/leetcode/_0412/FizzBuzz.kt) | Easy |
2425
| [773](https://leetcode.com/problems/sliding-puzzle/) | [Sliding Puzzle](src/main/kotlin/com/schmoczer/leetcode/_0773/SlidingPuzzle.kt) | Hard |
2526
| [796](https://leetcode.com/problems/rotate-string/) | [Rotate String](src/main/kotlin/com/schmoczer/leetcode/_0796/RotateString.kt) | Easy |
2627
| [1480](https://leetcode.com/problems/running-sum-of-1d-array/) | [Running Sum of 1d Array](src/main/kotlin/com/schmoczer/leetcode/_1480/RunningSumOf1dArray.kt) | Easy |
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.schmoczer.leetcode._0412
2+
3+
class FizzBuzz {
4+
// Runtime 1ms Beats 100.00%
5+
fun fizzBuzz(n: Int): List<String> {
6+
val answer = mutableListOf<String>()
7+
for (index in 1..n) {
8+
answer.add(
9+
when {
10+
index % 3 == 0 && index % 5 == 0 -> "FizzBuzz"
11+
index % 3 == 0 -> "Fizz"
12+
index % 5 == 0 -> "Buzz"
13+
else -> "$index"
14+
},
15+
)
16+
}
17+
return answer
18+
}
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Fizz Buzz
2+
3+
Given an integer `n`, return a string array `answer` (1-indexed) where:
4+
5+
- `answer[i] == "FizzBuzz"` if `i` is divisible by `3` and `5`.
6+
- `answer[i] == "Fizz"` if `i` is divisible by `3`.
7+
- `answer[i] == "Buzz"` if `i` is divisible by `5`.
8+
- `answer[i] == i` (as a string) if none of the above conditions are true.
9+
10+
Example 1:
11+
12+
> Input: n = 3
13+
>
14+
> Output: ["1","2","Fizz"]
15+
16+
Example 2:
17+
18+
> Input: n = 5
19+
>
20+
> Output: ["1","2","Fizz","4","Buzz"]
21+
22+
Example 3:
23+
24+
> Input: n = 15
25+
>
26+
> Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
27+
28+
29+
Constraints:
30+
31+
- `1 <= n <= 10^4`
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.schmoczer.leetcode._0412
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import kotlin.test.Test
5+
import kotlin.test.assertContentEquals
6+
7+
class FizzBuzzTest {
8+
private lateinit var sut: FizzBuzz
9+
10+
@BeforeEach
11+
fun setUp() {
12+
sut = FizzBuzz()
13+
}
14+
15+
@Test
16+
fun `fizz buzz for 3`() {
17+
val input = 3
18+
val expected = listOf("1", "2", "Fizz")
19+
20+
val result = sut.fizzBuzz(input)
21+
22+
assertContentEquals(expected, result)
23+
}
24+
25+
@Test
26+
fun `fizz buzz for 5`() {
27+
val input = 5
28+
val expected = listOf("1", "2", "Fizz", "4", "Buzz")
29+
30+
val result = sut.fizzBuzz(input)
31+
32+
assertContentEquals(expected, result)
33+
}
34+
35+
@Test
36+
fun `fizz buzz for 15`() {
37+
val input = 15
38+
val expected = listOf("1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz")
39+
40+
val result = sut.fizzBuzz(input)
41+
42+
assertContentEquals(expected, result)
43+
}
44+
}

‎src/test/kotlin/com/schmoczer/leetcode/_1672/RichestCustomerWealthTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import org.junit.jupiter.api.BeforeEach
55
import kotlin.test.Test
66

77
class RichestCustomerWealthTest {
8-
private lateinit var richestCustomerWealth: RichestCustomerWealth
8+
private lateinit var sut: RichestCustomerWealth
99

1010
@BeforeEach
1111
fun setUp() {
12-
richestCustomerWealth = RichestCustomerWealth()
12+
sut = RichestCustomerWealth()
1313
}
1414

1515
@Test
1616
fun `given two accounts 1,2,3 and 3,2,1 then richest customer wealth is 6`() {
1717
val input = arrayOf(intArrayOf(1, 2, 3), intArrayOf(3, 2, 1))
1818
val expected = 6
1919

20-
val result = richestCustomerWealth.maximumWealth(input)
20+
val result = sut.maximumWealth(input)
2121

2222
assertEquals(expected, result)
2323
}
@@ -27,7 +27,7 @@ class RichestCustomerWealthTest {
2727
val input = arrayOf(intArrayOf(1, 5), intArrayOf(7, 3), intArrayOf(3, 5))
2828
val expected = 10
2929

30-
val result = richestCustomerWealth.maximumWealth(input)
30+
val result = sut.maximumWealth(input)
3131

3232
assertEquals(expected, result)
3333
}
@@ -37,7 +37,7 @@ class RichestCustomerWealthTest {
3737
val input = arrayOf(intArrayOf(2, 8, 7), intArrayOf(7, 1, 3), intArrayOf(1, 9, 5))
3838
val expected = 17
3939

40-
val result = richestCustomerWealth.maximumWealth(input)
40+
val result = sut.maximumWealth(input)
4141

4242
assertEquals(expected, result)
4343
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /