Skip to content

Commit

Permalink
Added the raindrops exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
gvrooyen committed Sep 8, 2024
1 parent d9a52d3 commit 4a78414
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 1 deletion.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "raindrops",
"name": "Raindrops",
"uuid": "6e40cd45-7923-49ea-9267-1a3286ec99c7",
"practices": [],
"prerequisites": [],
"difficulty": 1
}
]
},
Expand Down
24 changes: 24 additions & 0 deletions exercises/practice/raindrops/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Instructions

Your task is to convert a number into its corresponding raindrop sounds.

If a given number:

- is divisible by 3, add "Pling" to the result.
- is divisible by 5, add "Plang" to the result.
- is divisible by 7, add "Plong" to the result.
- **is not** divisible by 3, 5, or 7, the result should be the number as a string.

## Examples

- 28 is divisible by 7, but not 3 or 5, so the result would be `"Plong"`.
- 30 is divisible by 3 and 5, but not 7, so the result would be `"PlingPlang"`.
- 34 is not divisible by 3, 5, or 7, so the result would be `"34"`.

~~~~exercism/note
A common way to test if one number is evenly divisible by another is to compare the [remainder][remainder] or [modulus][modulo] to zero.
Most languages provide operators or functions for one (or both) of these.
[remainder]: https://exercism.org/docs/programming/operators/remainder
[modulo]: https://en.wikipedia.org/wiki/Modulo_operation
~~~~
3 changes: 3 additions & 0 deletions exercises/practice/raindrops/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Introduction

Raindrops is a slightly more complex version of the FizzBuzz challenge, a classic interview question.
19 changes: 19 additions & 0 deletions exercises/practice/raindrops/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"gvrooyen"
],
"files": {
"solution": [
"raindrops.odin"
],
"test": [
"raindrops_test.odin"
],
"example": [
".meta/raindrops_example.odin"
]
},
"blurb": "Convert a number into its corresponding raindrop sounds - Pling, Plang and Plong.",
"source": "A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division.",
"source_url": "https://en.wikipedia.org/wiki/Fizz_buzz"
}
14 changes: 14 additions & 0 deletions exercises/practice/raindrops/.meta/raindrops_example.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package raindrops

import "core:strings"

convert :: proc(number: int) -> string {
b := strings.builder_make_none()
defer strings.builder_destroy(&b)

if number % 3 == 0 do strings.write_string(&b, "Pling")
if number % 5 == 0 do strings.write_string(&b, "Plang")
if number % 7 == 0 do strings.write_string(&b, "Plong")
if strings.builder_len(b) == 0 do strings.write_int(&b, number)
return strings.to_string(b)
}
64 changes: 64 additions & 0 deletions exercises/practice/raindrops/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[1575d549-e502-46d4-a8e1-6b7bec6123d8]
description = "the sound for 1 is 1"

[1f51a9f9-4895-4539-b182-d7b0a5ab2913]
description = "the sound for 3 is Pling"

[2d9bfae5-2b21-4bcd-9629-c8c0e388f3e0]
description = "the sound for 5 is Plang"

[d7e60daa-32ef-4c23-b688-2abff46c4806]
description = "the sound for 7 is Plong"

[6bb4947b-a724-430c-923f-f0dc3d62e56a]
description = "the sound for 6 is Pling as it has a factor 3"

[ce51e0e8-d9d4-446d-9949-96eac4458c2d]
description = "2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base"

[0dd66175-e3e2-47fc-8750-d01739856671]
description = "the sound for 9 is Pling as it has a factor 3"

[022c44d3-2182-4471-95d7-c575af225c96]
description = "the sound for 10 is Plang as it has a factor 5"

[37ab74db-fed3-40ff-b7b9-04acdfea8edf]
description = "the sound for 14 is Plong as it has a factor of 7"

[31f92999-6afb-40ee-9aa4-6d15e3334d0f]
description = "the sound for 15 is PlingPlang as it has factors 3 and 5"

[ff9bb95d-6361-4602-be2c-653fe5239b54]
description = "the sound for 21 is PlingPlong as it has factors 3 and 7"

[d2e75317-b72e-40ab-8a64-6734a21dece1]
description = "the sound for 25 is Plang as it has a factor 5"

[a09c4c58-c662-4e32-97fe-f1501ef7125c]
description = "the sound for 27 is Pling as it has a factor 3"

[bdf061de-8564-4899-a843-14b48b722789]
description = "the sound for 35 is PlangPlong as it has factors 5 and 7"

[c4680bee-69ba-439d-99b5-70c5fd1a7a83]
description = "the sound for 49 is Plong as it has a factor 7"

[17f2bc9a-b65a-4d23-8ccd-266e8c271444]
description = "the sound for 52 is 52"

[e46677ed-ff1a-419f-a740-5c713d2830e4]
description = "the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7"

[13c6837a-0fcd-4b86-a0eb-20572f7deb0b]
description = "the sound for 3125 is Plang as it has a factor 5"
5 changes: 5 additions & 0 deletions exercises/practice/raindrops/raindrops.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package raindrops

convert :: proc(number: int) -> string {
#panic("Please implement the `convert` procedure.")
}
110 changes: 110 additions & 0 deletions exercises/practice/raindrops/raindrops_test.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* These are the unit tests for the exercise. Only the first one is enabled to start with. You can
* enable the other tests by uncommenting the `@(test)` attribute of the test procedure. Your
* solution should pass all tests before it is ready for submission.
*/

package raindrops

import "core:mem"
import "core:testing"

@(test)
test_the_sound_for_1_is_1 :: proc(t: ^testing.T) {
testing.expect_value(t, convert(1), "1")
}

// @(test)
test_the_sound_for_3_is_pling :: proc(t: ^testing.T) {
testing.expect_value(t, convert(3), "Pling")
}

// @(test)
test_the_sound_for_5_is_plang :: proc(t: ^testing.T) {
testing.expect_value(t, convert(5), "Plang")
}

// @(test)
test_the_sound_for_7_is_plong :: proc(t: ^testing.T) {
testing.expect_value(t, convert(7), "Plong")
}

// @(test)
test_the_sound_for_6_is_pling :: proc(t: ^testing.T) {
testing.expect_value(t, convert(6), "Pling")
}

// @(test)
test_the_sound_for_8_is_8 :: proc(t: ^testing.T) {
testing.expect_value(t, convert(8), "8")
}

// @(test)
test_the_sound_for_9_is_pling :: proc(t: ^testing.T) {
testing.expect_value(t, convert(9), "Pling")
}

// @(test)
test_the_sound_for_10_is_plang :: proc(t: ^testing.T) {
testing.expect_value(t, convert(10), "Plang")
}

// @(test)
test_the_sound_for_14_is_plong :: proc(t: ^testing.T) {
testing.expect_value(t, convert(14), "Plong")
}

// @(test)
test_the_sound_for_15_is_plingplang :: proc(t: ^testing.T) {
testing.expect_value(t, convert(15), "PlingPlang")
}

// @(test)
test_the_sound_for_21_is_plingplong :: proc(t: ^testing.T) {
testing.expect_value(t, convert(21), "PlingPlong")
}

// @(test)
test_the_sound_for_25_is_plang :: proc(t: ^testing.T) {
testing.expect_value(t, convert(25), "Plang")
}

// @(test)
test_the_sound_for_27_is_pling :: proc(t: ^testing.T) {
testing.expect_value(t, convert(27), "Pling")
}

// @(test)
test_the_sound_for_35_is_plangplong :: proc(t: ^testing.T) {
testing.expect_value(t, convert(35), "PlangPlong")
}

// @(test)
test_the_sound_for_49_is_plong :: proc(t: ^testing.T) {
testing.expect_value(t, convert(49), "Plong")
}

// @(test)
test_the_sound_for_52_is_52 :: proc(t: ^testing.T) {
testing.expect_value(t, convert(52), "52")
}

// @(test)
test_the_sound_for_105_is_plingplangplong :: proc(t: ^testing.T) {
testing.expect_value(t, convert(105), "PlingPlangPlong")
}

// @(test)
test_the_sound_for_3125_is_plang :: proc(t: ^testing.T) {
testing.expect_value(t, convert(3125), "Plang")
}

// @(test)
test_no_memory_leaks :: proc(t: ^testing.T) {
track: mem.Tracking_Allocator
mem.tracking_allocator_init(&track, context.allocator)
defer mem.tracking_allocator_destroy(&track)
context.allocator = mem.tracking_allocator(&track)
test_the_sound_for_105_is_plingplangplong(t)
testing.expect_value(t, len(track.allocation_map), 0)
testing.expect_value(t, len(track.bad_free_array), 0)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package rna_transcription

import "core:log"
import "core:mem"
import "core:testing"

Expand Down

0 comments on commit 4a78414

Please sign in to comment.