Skip to content

Commit a986f7a

Browse files
Add etl
1 parent 97efcb0 commit a986f7a

File tree

8 files changed

+203
-0
lines changed

8 files changed

+203
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,14 @@
362362
"prerequisites": [],
363363
"difficulty": 2
364364
},
365+
{
366+
"slug": "etl",
367+
"name": "ETL",
368+
"uuid": "1e65a945-b044-4392-b8d9-59bcefa7a314",
369+
"practices": [],
370+
"prerequisites": [],
371+
"difficulty": 2
372+
},
365373
{
366374
"slug": "grains",
367375
"name": "Grains",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Instructions
2+
3+
Your task is to change the data format of letters and their point values in the game.
4+
5+
Currently, letters are stored in groups based on their score, in a one-to-many mapping.
6+
7+
- 1 point: "A", "E", "I", "O", "U", "L", "N", "R", "S", "T",
8+
- 2 points: "D", "G",
9+
- 3 points: "B", "C", "M", "P",
10+
- 4 points: "F", "H", "V", "W", "Y",
11+
- 5 points: "K",
12+
- 8 points: "J", "X",
13+
- 10 points: "Q", "Z",
14+
15+
This needs to be changed to store each individual letter with its score in a one-to-one mapping.
16+
17+
- "a" is worth 1 point.
18+
- "b" is worth 3 points.
19+
- "c" is worth 3 points.
20+
- "d" is worth 2 points.
21+
- etc.
22+
23+
As part of this change, the team has also decided to change the letters to be lower-case rather than upper-case.
24+
25+
~~~~exercism/note
26+
If you want to look at how the data was previously structured and how it needs to change, take a look at the examples in the test suite.
27+
~~~~
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Introduction
2+
3+
You work for a company that makes an online multiplayer game called Lexiconia.
4+
5+
To play the game, each player is given 13 letters, which they must rearrange to create words.
6+
Different letters have different point values, since it's easier to create words with some letters than others.
7+
8+
The game was originally launched in English, but it is very popular, and now the company wants to expand to other languages as well.
9+
10+
Different languages need to support different point values for letters.
11+
The point values are determined by how often letters are used, compared to other letters in that language.
12+
13+
For example, the letter 'C' is quite common in English, and is only worth 3 points.
14+
But in Norwegian it's a very rare letter, and is worth 10 points.
15+
16+
To make it easier to add new languages, your team needs to change the way letters and their point values are stored in the game.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"etl.zig"
8+
],
9+
"test": [
10+
"test_etl.zig"
11+
],
12+
"example": [
13+
".meta/example.zig"
14+
]
15+
},
16+
"blurb": "Change the data format for scoring a game to more easily add other languages.",
17+
"source": "Based on an exercise by the JumpstartLab team for students at The Turing School of Software and Design.",
18+
"source_url": "https://turing.edu"
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const std = @import("std");
2+
const ascii = std.ascii;
3+
const mem = std.mem;
4+
5+
pub fn transform(allocator: mem.Allocator, legacy: std.AutoHashMap(i5, []const u8)) mem.Allocator.Error!std.AutoHashMap(u8, i5) {
6+
var result = std.AutoHashMap(u8, i5).init(allocator);
7+
var iter = legacy.iterator();
8+
while (iter.next()) |entry| {
9+
const points: i5 = entry.key_ptr.*;
10+
for (entry.value_ptr.*) |letter| {
11+
try result.put(ascii.toLower(letter), points);
12+
}
13+
}
14+
return result;
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
[78a7a9f9-4490-4a47-8ee9-5a38bb47d28f]
13+
description = "single letter"
14+
15+
[60dbd000-451d-44c7-bdbb-97c73ac1f497]
16+
description = "single score with multiple letters"
17+
18+
[f5c5de0c-301f-4fdd-a0e5-df97d4214f54]
19+
description = "multiple scores with multiple letters"
20+
21+
[5db8ea89-ecb4-4dcd-902f-2b418cc87b9d]
22+
description = "multiple scores with differing numbers of letters"

exercises/practice/etl/etl.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const std = @import("std");
2+
const mem = std.mem;
3+
4+
pub fn transform(allocator: mem.Allocator, legacy: std.AutoHashMap(i5, []const u8)) mem.Allocator.Error!std.AutoHashMap(u8, i5) {
5+
_ = allocator;
6+
_ = legacy;
7+
@compileError("please implement the transform function");
8+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
const std = @import("std");
2+
const testing = std.testing;
3+
4+
const etl = @import("etl.zig");
5+
const transform = etl.transform;
6+
7+
test "single letter" {
8+
var legacy = std.AutoHashMap(i5, []const u8).init(testing.allocator);
9+
try legacy.put(1, "A");
10+
var actual = try transform(testing.allocator, legacy);
11+
legacy.deinit();
12+
13+
try testing.expectEqual(1, actual.count());
14+
try testing.expectEqual(1, actual.get('a'));
15+
actual.deinit();
16+
}
17+
18+
test "single score with multiple letters" {
19+
var legacy = std.AutoHashMap(i5, []const u8).init(testing.allocator);
20+
try legacy.put(1, "AEIOU");
21+
var actual = try transform(testing.allocator, legacy);
22+
legacy.deinit();
23+
24+
try testing.expectEqual(5, actual.count());
25+
try testing.expectEqual(1, actual.get('a'));
26+
try testing.expectEqual(1, actual.get('e'));
27+
try testing.expectEqual(1, actual.get('i'));
28+
try testing.expectEqual(1, actual.get('o'));
29+
try testing.expectEqual(1, actual.get('u'));
30+
actual.deinit();
31+
}
32+
33+
test "multiple scores with multiple letters" {
34+
var legacy = std.AutoHashMap(i5, []const u8).init(testing.allocator);
35+
try legacy.put(1, "AE");
36+
try legacy.put(2, "DG");
37+
var actual = try transform(testing.allocator, legacy);
38+
legacy.deinit();
39+
40+
try testing.expectEqual(4, actual.count());
41+
try testing.expectEqual(1, actual.get('a'));
42+
try testing.expectEqual(2, actual.get('d'));
43+
try testing.expectEqual(1, actual.get('e'));
44+
try testing.expectEqual(2, actual.get('g'));
45+
actual.deinit();
46+
}
47+
48+
test "multiple scores with differing numbers of letters" {
49+
var legacy = std.AutoHashMap(i5, []const u8).init(testing.allocator);
50+
try legacy.put(1, "AEIOULNRST");
51+
try legacy.put(10, "QZ");
52+
try legacy.put(2, "DG");
53+
try legacy.put(3, "BCMP");
54+
try legacy.put(4, "FHVWY");
55+
try legacy.put(5, "K");
56+
try legacy.put(8, "JX");
57+
var actual = try transform(testing.allocator, legacy);
58+
legacy.deinit();
59+
60+
try testing.expectEqual(26, actual.count());
61+
try testing.expectEqual(1, actual.get('a'));
62+
try testing.expectEqual(3, actual.get('b'));
63+
try testing.expectEqual(3, actual.get('c'));
64+
try testing.expectEqual(2, actual.get('d'));
65+
try testing.expectEqual(1, actual.get('e'));
66+
try testing.expectEqual(4, actual.get('f'));
67+
try testing.expectEqual(2, actual.get('g'));
68+
try testing.expectEqual(4, actual.get('h'));
69+
try testing.expectEqual(1, actual.get('i'));
70+
try testing.expectEqual(8, actual.get('j'));
71+
try testing.expectEqual(5, actual.get('k'));
72+
try testing.expectEqual(1, actual.get('l'));
73+
try testing.expectEqual(3, actual.get('m'));
74+
try testing.expectEqual(1, actual.get('n'));
75+
try testing.expectEqual(1, actual.get('o'));
76+
try testing.expectEqual(3, actual.get('p'));
77+
try testing.expectEqual(10, actual.get('q'));
78+
try testing.expectEqual(1, actual.get('r'));
79+
try testing.expectEqual(1, actual.get('s'));
80+
try testing.expectEqual(1, actual.get('t'));
81+
try testing.expectEqual(1, actual.get('u'));
82+
try testing.expectEqual(4, actual.get('v'));
83+
try testing.expectEqual(4, actual.get('w'));
84+
try testing.expectEqual(8, actual.get('x'));
85+
try testing.expectEqual(4, actual.get('y'));
86+
try testing.expectEqual(10, actual.get('z'));
87+
actual.deinit();
88+
}

0 commit comments

Comments
 (0)