Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exercises(raindrops): implement #155

Merged
merged 5 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,24 @@
],
"difficulty": 1
},
{
"uuid": "3342ce3f-5569-4a11-944b-ef7c7e678fa2",
"slug": "raindrops",
"name": "Raindrops",
"practices": [
"integers",
"math",
"slices",
"strings"
],
"prerequisites": [
"integers",
"math",
"slices",
"strings"
],
"difficulty": 1
},
{
"uuid": "0590d6df-afbe-4983-92d4-8fe6797ad5fa",
"slug": "acronym",
Expand Down
20 changes: 20 additions & 0 deletions exercises/practice/raindrops/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Instructions

Your task is to convert a number into a string that contains raindrop sounds corresponding to certain potential factors.
A factor is a number that evenly divides into another number, leaving no remainder.
The simplest way to test if one number is a factor of another is to use the [modulo operation][modulo].

The rules of `raindrops` are that if a given number:

- has 3 as a factor, add 'Pling' to the result.
- has 5 as a factor, add 'Plang' to the result.
- has 7 as a factor, add 'Plong' to the result.
- _does not_ have any of 3, 5, or 7 as a factor, the result should be the digits of the number.

## Examples

- 28 has 7 as a factor, but not 3 or 5, so the result would be "Plong".
- 30 has both 3 and 5 as factors, but not 7, so the result would be "PlingPlang".
- 34 is not factored by 3, 5, or 7, so the result would be "34".

[modulo]: https://en.wikipedia.org/wiki/Modulo_operation
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": [
"ee7"
],
"files": {
"solution": [
"raindrops.zig"
],
"test": [
"test_raindrops.zig"
],
"example": [
".meta/example.zig"
]
},
"blurb": "Convert a number to a string, the content of which depends on the number's factors.",
"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"
}
9 changes: 9 additions & 0 deletions exercises/practice/raindrops/.meta/example.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const std = @import("std");

pub fn convert(buffer: []u8, comptime n: u32) []const u8 {
const pling = if (n % 3 == 0) "Pling" else "";
const plang = if (n % 5 == 0) "Plang" else "";
const plong = if (n % 7 == 0) "Plong" else "";
const result = pling ++ plang ++ plong;
return if (result.len > 0) result else std.fmt.bufPrint(buffer, "{}", .{n}) catch unreachable;
}
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.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub fn convert(buffer: []u8, n: u32) []const u8 {
_ = buffer;
_ = n;
@compileError("please implement the convert function");
}
83 changes: 83 additions & 0 deletions exercises/practice/raindrops/test_raindrops.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const std = @import("std");
const testing = std.testing;

const raindrops = @import("raindrops.zig");

fn testConvert(comptime n: u32, expected: []const u8) !void {
const buffer_size = 15; // The maximum length is for PlingPlangPlong
var buffer: [buffer_size]u8 = undefined;
const actual = raindrops.convert(&buffer, n);
try testing.expectEqualStrings(expected, actual);
}

test "the sound for 1 is 1" {
try testConvert(1, "1");
}

test "the sound for 3 is Pling" {
try testConvert(3, "Pling");
}

test "the sound for 5 is Plang" {
try testConvert(5, "Plang");
}

test "the sound for 7 is Plong" {
try testConvert(7, "Plong");
}

test "the sound for 6 is Pling as it has a factor 3" {
try testConvert(6, "Pling");
}

test "2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base" {
try testConvert(8, "8");
}

test "the sound for 9 is Pling as it has a factor 3" {
try testConvert(9, "Pling");
}

test "the sound for 10 is Plang as it has a factor 5" {
try testConvert(10, "Plang");
}

test "the sound for 14 is Plong as it has a factor of 7" {
try testConvert(14, "Plong");
}

test "the sound for 15 is PlingPlang as it has factors 3 and 5" {
try testConvert(15, "PlingPlang");
}

test "the sound for 21 is PlingPlong as it has factors 3 and 7" {
try testConvert(21, "PlingPlong");
}

test "the sound for 25 is Plang as it has a factor 5" {
try testConvert(25, "Plang");
}

test "the sound for 27 is Pling as it has a factor 3" {
try testConvert(27, "Pling");
}

test "the sound for 35 is PlangPlong as it has factors 5 and 7" {
try testConvert(35, "PlangPlong");
}

test "the sound for 49 is Plong as it has a factor 7" {
try testConvert(49, "Plong");
}

test "the sound for 52 is 52" {
try testConvert(52, "52");
}

test "the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7" {
try testConvert(105, "PlingPlangPlong");
}

test "the sound for 3125 is Plang as it has a factor 5" {
try testConvert(3125, "Plang");
}