Skip to content

Commit d1b8784

Browse files
authored
exercises(raindrops): implement (#155)
1 parent dae8f86 commit d1b8784

File tree

7 files changed

+218
-0
lines changed

7 files changed

+218
-0
lines changed

config.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,24 @@
505505
],
506506
"difficulty": 1
507507
},
508+
{
509+
"uuid": "3342ce3f-5569-4a11-944b-ef7c7e678fa2",
510+
"slug": "raindrops",
511+
"name": "Raindrops",
512+
"practices": [
513+
"integers",
514+
"math",
515+
"slices",
516+
"strings"
517+
],
518+
"prerequisites": [
519+
"integers",
520+
"math",
521+
"slices",
522+
"strings"
523+
],
524+
"difficulty": 1
525+
},
508526
{
509527
"uuid": "0590d6df-afbe-4983-92d4-8fe6797ad5fa",
510528
"slug": "acronym",
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Instructions
2+
3+
Your task is to convert a number into a string that contains raindrop sounds corresponding to certain potential factors.
4+
A factor is a number that evenly divides into another number, leaving no remainder.
5+
The simplest way to test if one number is a factor of another is to use the [modulo operation][modulo].
6+
7+
The rules of `raindrops` are that if a given number:
8+
9+
- has 3 as a factor, add 'Pling' to the result.
10+
- has 5 as a factor, add 'Plang' to the result.
11+
- has 7 as a factor, add 'Plong' to the result.
12+
- _does not_ have any of 3, 5, or 7 as a factor, the result should be the digits of the number.
13+
14+
## Examples
15+
16+
- 28 has 7 as a factor, but not 3 or 5, so the result would be "Plong".
17+
- 30 has both 3 and 5 as factors, but not 7, so the result would be "PlingPlang".
18+
- 34 is not factored by 3, 5, or 7, so the result would be "34".
19+
20+
[modulo]: https://en.wikipedia.org/wiki/Modulo_operation
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"ee7"
4+
],
5+
"files": {
6+
"solution": [
7+
"raindrops.zig"
8+
],
9+
"test": [
10+
"test_raindrops.zig"
11+
],
12+
"example": [
13+
".meta/example.zig"
14+
]
15+
},
16+
"blurb": "Convert a number to a string, the content of which depends on the number's factors.",
17+
"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.",
18+
"source_url": "https://en.wikipedia.org/wiki/Fizz_buzz"
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const std = @import("std");
2+
3+
pub fn convert(buffer: []u8, comptime n: u32) []const u8 {
4+
const pling = if (n % 3 == 0) "Pling" else "";
5+
const plang = if (n % 5 == 0) "Plang" else "";
6+
const plong = if (n % 7 == 0) "Plong" else "";
7+
const result = pling ++ plang ++ plong;
8+
return if (result.len > 0) result else std.fmt.bufPrint(buffer, "{}", .{n}) catch unreachable;
9+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[1575d549-e502-46d4-a8e1-6b7bec6123d8]
13+
description = "the sound for 1 is 1"
14+
15+
[1f51a9f9-4895-4539-b182-d7b0a5ab2913]
16+
description = "the sound for 3 is Pling"
17+
18+
[2d9bfae5-2b21-4bcd-9629-c8c0e388f3e0]
19+
description = "the sound for 5 is Plang"
20+
21+
[d7e60daa-32ef-4c23-b688-2abff46c4806]
22+
description = "the sound for 7 is Plong"
23+
24+
[6bb4947b-a724-430c-923f-f0dc3d62e56a]
25+
description = "the sound for 6 is Pling as it has a factor 3"
26+
27+
[ce51e0e8-d9d4-446d-9949-96eac4458c2d]
28+
description = "2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base"
29+
30+
[0dd66175-e3e2-47fc-8750-d01739856671]
31+
description = "the sound for 9 is Pling as it has a factor 3"
32+
33+
[022c44d3-2182-4471-95d7-c575af225c96]
34+
description = "the sound for 10 is Plang as it has a factor 5"
35+
36+
[37ab74db-fed3-40ff-b7b9-04acdfea8edf]
37+
description = "the sound for 14 is Plong as it has a factor of 7"
38+
39+
[31f92999-6afb-40ee-9aa4-6d15e3334d0f]
40+
description = "the sound for 15 is PlingPlang as it has factors 3 and 5"
41+
42+
[ff9bb95d-6361-4602-be2c-653fe5239b54]
43+
description = "the sound for 21 is PlingPlong as it has factors 3 and 7"
44+
45+
[d2e75317-b72e-40ab-8a64-6734a21dece1]
46+
description = "the sound for 25 is Plang as it has a factor 5"
47+
48+
[a09c4c58-c662-4e32-97fe-f1501ef7125c]
49+
description = "the sound for 27 is Pling as it has a factor 3"
50+
51+
[bdf061de-8564-4899-a843-14b48b722789]
52+
description = "the sound for 35 is PlangPlong as it has factors 5 and 7"
53+
54+
[c4680bee-69ba-439d-99b5-70c5fd1a7a83]
55+
description = "the sound for 49 is Plong as it has a factor 7"
56+
57+
[17f2bc9a-b65a-4d23-8ccd-266e8c271444]
58+
description = "the sound for 52 is 52"
59+
60+
[e46677ed-ff1a-419f-a740-5c713d2830e4]
61+
description = "the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7"
62+
63+
[13c6837a-0fcd-4b86-a0eb-20572f7deb0b]
64+
description = "the sound for 3125 is Plang as it has a factor 5"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub fn convert(buffer: []u8, n: u32) []const u8 {
2+
_ = buffer;
3+
_ = n;
4+
@compileError("please implement the convert function");
5+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const std = @import("std");
2+
const testing = std.testing;
3+
4+
const raindrops = @import("raindrops.zig");
5+
6+
fn testConvert(comptime n: u32, expected: []const u8) !void {
7+
const buffer_size = 15; // The maximum length is for PlingPlangPlong
8+
var buffer: [buffer_size]u8 = undefined;
9+
const actual = raindrops.convert(&buffer, n);
10+
try testing.expectEqualStrings(expected, actual);
11+
}
12+
13+
test "the sound for 1 is 1" {
14+
try testConvert(1, "1");
15+
}
16+
17+
test "the sound for 3 is Pling" {
18+
try testConvert(3, "Pling");
19+
}
20+
21+
test "the sound for 5 is Plang" {
22+
try testConvert(5, "Plang");
23+
}
24+
25+
test "the sound for 7 is Plong" {
26+
try testConvert(7, "Plong");
27+
}
28+
29+
test "the sound for 6 is Pling as it has a factor 3" {
30+
try testConvert(6, "Pling");
31+
}
32+
33+
test "2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base" {
34+
try testConvert(8, "8");
35+
}
36+
37+
test "the sound for 9 is Pling as it has a factor 3" {
38+
try testConvert(9, "Pling");
39+
}
40+
41+
test "the sound for 10 is Plang as it has a factor 5" {
42+
try testConvert(10, "Plang");
43+
}
44+
45+
test "the sound for 14 is Plong as it has a factor of 7" {
46+
try testConvert(14, "Plong");
47+
}
48+
49+
test "the sound for 15 is PlingPlang as it has factors 3 and 5" {
50+
try testConvert(15, "PlingPlang");
51+
}
52+
53+
test "the sound for 21 is PlingPlong as it has factors 3 and 7" {
54+
try testConvert(21, "PlingPlong");
55+
}
56+
57+
test "the sound for 25 is Plang as it has a factor 5" {
58+
try testConvert(25, "Plang");
59+
}
60+
61+
test "the sound for 27 is Pling as it has a factor 3" {
62+
try testConvert(27, "Pling");
63+
}
64+
65+
test "the sound for 35 is PlangPlong as it has factors 5 and 7" {
66+
try testConvert(35, "PlangPlong");
67+
}
68+
69+
test "the sound for 49 is Plong as it has a factor 7" {
70+
try testConvert(49, "Plong");
71+
}
72+
73+
test "the sound for 52 is 52" {
74+
try testConvert(52, "52");
75+
}
76+
77+
test "the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7" {
78+
try testConvert(105, "PlingPlangPlong");
79+
}
80+
81+
test "the sound for 3125 is Plang as it has a factor 5" {
82+
try testConvert(3125, "Plang");
83+
}

0 commit comments

Comments
 (0)