Skip to content

Commit 657f62a

Browse files
isogram, raindrops: reject comptime solutions
1 parent a7cb826 commit 657f62a

5 files changed

Lines changed: 44 additions & 27 deletions

File tree

exercises/practice/isogram/test_isogram.zig

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,62 @@ const testing = std.testing;
33

44
const isogram = @import("isogram.zig");
55

6+
fn testIsIsogram(phrase: []const u8, expected: bool) !void {
7+
try testing.expectEqual(expected, isogram.isIsogram(phrase));
8+
}
9+
610
test "empty string" {
7-
try testing.expect(isogram.isIsogram(""));
11+
try testIsIsogram("", true);
812
}
913

1014
test "isogram with only lower case characters" {
11-
try testing.expect(isogram.isIsogram("isogram"));
15+
try testIsIsogram("isogram", true);
1216
}
1317

1418
test "word with one duplicated character" {
15-
try testing.expect(!isogram.isIsogram("eleven"));
19+
try testIsIsogram("eleven", false);
1620
}
1721

1822
test "word with one duplicated character from the end of the alphabet" {
19-
try testing.expect(!isogram.isIsogram("zzyzx"));
23+
try testIsIsogram("zzyzx", false);
2024
}
2125

2226
test "longest reported english isogram" {
23-
try testing.expect(isogram.isIsogram("subdermatoglyphic"));
27+
try testIsIsogram("subdermatoglyphic", true);
2428
}
2529

2630
test "word with duplicated character in mixed case" {
27-
try testing.expect(!isogram.isIsogram("Alphabet"));
31+
try testIsIsogram("Alphabet", false);
2832
}
2933

3034
test "word with duplicated character in mixed case, lowercase first" {
31-
try testing.expect(!isogram.isIsogram("alphAbet"));
35+
try testIsIsogram("alphAbet", false);
3236
}
3337

3438
test "hypothetical isogrammic word with hyphen" {
35-
try testing.expect(isogram.isIsogram("thumbscrew-japingly"));
39+
try testIsIsogram("thumbscrew-japingly", true);
3640
}
3741

3842
test "hypothetical word with duplicated character following hyphen" {
39-
try testing.expect(!isogram.isIsogram("thumbscrew-jappingly"));
43+
try testIsIsogram("thumbscrew-jappingly", false);
4044
}
4145

4246
test "isogram with duplicated hyphen" {
43-
try testing.expect(isogram.isIsogram("six-year-old"));
47+
try testIsIsogram("six-year-old", true);
4448
}
4549

4650
test "made-up name that is an isogram" {
47-
try testing.expect(isogram.isIsogram("Emily Jung Schwartzkopf"));
51+
try testIsIsogram("Emily Jung Schwartzkopf", true);
4852
}
4953

5054
test "duplicated character in the middle" {
51-
try testing.expect(!isogram.isIsogram("accentor"));
55+
try testIsIsogram("accentor", false);
5256
}
5357

5458
test "same first and last characters" {
55-
try testing.expect(!isogram.isIsogram("angola"));
59+
try testIsIsogram("angola", false);
5660
}
5761

5862
test "word with duplicated character and with two hyphens" {
59-
try testing.expect(!isogram.isIsogram("up-to-date"));
63+
try testIsIsogram("up-to-date", false);
6064
}
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
const std = @import("std");
22

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;
3+
fn append(buffer: []u8, len: usize, sound: []const u8) usize {
4+
@memcpy(buffer[len..][0..sound.len], sound);
5+
return len + sound.len;
6+
}
7+
8+
pub fn convert(buffer: []u8, n: u32) []const u8 {
9+
var len: usize = 0;
10+
if (n % 3 == 0) len = append(buffer, len, "Pling");
11+
if (n % 5 == 0) len = append(buffer, len, "Plang");
12+
if (n % 7 == 0) len = append(buffer, len, "Plong");
13+
if (len == 0) return std.fmt.bufPrint(buffer, "{d}", .{n}) catch unreachable;
14+
return buffer[0..len];
915
}

exercises/practice/raindrops/test_raindrops.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ const testing = std.testing;
33

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

6-
fn testConvert(comptime n: u32, expected: []const u8) !void {
6+
fn testConvert(n: u32, expected: []const u8) !void {
77
const buffer_size = 15; // The maximum length is for PlingPlangPlong
88
var buffer: [buffer_size]u8 = undefined;
99
const actual = raindrops.convert(&buffer, n);
10+
try testing.expectEqual(@as([*]const u8, &buffer), actual.ptr);
1011
try testing.expectEqualStrings(expected, actual);
1112
}
1213

generators/exercises/isogram.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
from lib import zstr
1+
from lib import zbool, zstr
2+
3+
HEADER = """
4+
fn testIsIsogram(phrase: []const u8, expected: bool) !void {
5+
try testing.expectEqual(expected, isogram.isIsogram(phrase));
6+
}
7+
"""
28

39

410
def gen_case(case):
5-
phrase = case["input"]["phrase"]
6-
expected = case["expected"]
7-
neg = "" if expected else "!"
8-
return f" try testing.expect({neg}isogram.isIsogram({zstr(phrase)}));\n"
11+
phrase = zstr(case["input"]["phrase"])
12+
expected = zbool(case["expected"])
13+
return f" try testIsIsogram({phrase}, {expected});\n"

generators/exercises/raindrops.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
IMPORT_SELF = True
44

55
HEADER = """
6-
fn testConvert(comptime n: u32, expected: []const u8) !void {
6+
fn testConvert(n: u32, expected: []const u8) !void {
77
const buffer_size = 15; // The maximum length is for PlingPlangPlong
88
var buffer: [buffer_size]u8 = undefined;
99
const actual = raindrops.convert(&buffer, n);
10+
try testing.expectEqual(@as([*]const u8, &buffer), actual.ptr);
1011
try testing.expectEqualStrings(expected, actual);
1112
}
1213
"""

0 commit comments

Comments
 (0)