Skip to content

Commit 7a8722a

Browse files
Reorder keys (#1960)
* book-store: reorder keys * darts: reorder keys * grade-school: reorder keys * hamming: reorder keys * high-scores: reorder keys * largest-series-product: reorder keys * list-ops: reorder keys * luhn: reorder keys * triangle: reorder keys * scale-generator: reorder keys * saddle-points: reorder keys * diffie-hellman: reorder keys * collatz-conjecture: reorder keys * anagram: reorder keys * accumulate: reorder keys * phone-number: reorder keys * Add CI script to check correct order of keys * Allow running check_key_order.rb from any dir
1 parent 88ce356 commit 7a8722a

File tree

18 files changed

+126
-89
lines changed

18 files changed

+126
-89
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ jobs:
116116
117117
exit "$fail"
118118
119+
- name: Verify that all keys are in the correct order
120+
run: ./bin/check_key_order.rb
121+
119122
markdown-lint:
120123
name: Lint markdown files
121124
runs-on: ubuntu-latest

bin/check_key_order.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env ruby
2+
3+
CORRECT_ORDER = %w[uuid reimplements description comments scenarios property input expected]
4+
5+
require 'json'
6+
7+
def find_tests(json)
8+
if json['cases']
9+
json['cases'].flat_map {|cases| find_tests(cases)}
10+
elsif json['uuid']
11+
[json]
12+
end
13+
end
14+
15+
exit_code = 0
16+
17+
root_dir = "#{__dir__}/.."
18+
Dir.glob("#{root_dir}/exercises/*/canonical-data.json").each do |path|
19+
json = JSON.parse(File.read(path))
20+
invalid_tests = find_tests(json).select {|test| (CORRECT_ORDER & test.keys) != test.keys}
21+
next if invalid_tests.empty?
22+
23+
puts "The following tests in #{path.delete_prefix("#{root_dir}/")} use the wrong key order:"
24+
invalid_tests.each do |test|
25+
puts "- Test: #{test['uuid']} (#{test['description']})"
26+
puts " Actual: #{test.keys}"
27+
puts " Expected: #{(CORRECT_ORDER & test.keys)}"
28+
puts ""
29+
end
30+
31+
exit_code = 1
32+
end
33+
34+
exit(exit_code)

exercises/accumulate/canonical-data.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@
6464
},
6565
{
6666
"uuid": "0b357334-4cad-49e1-a741-425202edfc7c",
67-
"description": "accumulate recursively",
68-
"property": "accumulate",
6967
"reimplements": "bee8e9b6-b16f-4cd2-be3b-ccf7457e50bb",
68+
"description": "accumulate recursively",
7069
"comments": ["Removes the trailing `)` at the end of the expression"],
70+
"property": "accumulate",
7171
"input": {
7272
"list": ["a", "b", "c"],
7373
"accumulator": "(x) => accumulate([\"1\", \"2\", \"3\"], (y) => x + y)"

exercises/anagram/canonical-data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
{
2525
"uuid": "03eb9bbe-8906-4ea0-84fa-ffe711b52c8b",
2626
"reimplements": "b3cca662-f50a-489e-ae10-ab8290a09bdc",
27+
"description": "detects two anagrams",
2728
"comments": [
2829
"Reimplemented to be consistent about removing references to 'master'"
2930
],
30-
"description": "detects two anagrams",
3131
"property": "findAnagrams",
3232
"input": {
3333
"subject": "solemn",

exercises/book-store/canonical-data.json

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,187 +11,187 @@
1111
"cases": [
1212
{
1313
"uuid": "17146bd5-2e80-4557-ab4c-05632b6b0d01",
14-
"property": "total",
1514
"description": "Only a single book",
1615
"comments": ["Suggested grouping, [[1]]."],
16+
"property": "total",
1717
"input": {
1818
"basket": [1]
1919
},
2020
"expected": 800
2121
},
2222
{
2323
"uuid": "cc2de9ac-ff2a-4efd-b7c7-bfe0f43271ce",
24-
"property": "total",
2524
"description": "Two of the same book",
2625
"comments": ["Suggested grouping, [[2],[2]]."],
26+
"property": "total",
2727
"input": {
2828
"basket": [2, 2]
2929
},
3030
"expected": 1600
3131
},
3232
{
3333
"uuid": "5a86eac0-45d2-46aa-bbf0-266b94393a1a",
34-
"property": "total",
3534
"description": "Empty basket",
3635
"comments": ["Suggested grouping, []."],
36+
"property": "total",
3737
"input": {
3838
"basket": []
3939
},
4040
"expected": 0
4141
},
4242
{
4343
"uuid": "158bd19a-3db4-4468-ae85-e0638a688990",
44-
"property": "total",
4544
"description": "Two different books",
4645
"comments": ["Suggested grouping, [[1,2]]."],
46+
"property": "total",
4747
"input": {
4848
"basket": [1, 2]
4949
},
5050
"expected": 1520
5151
},
5252
{
5353
"uuid": "f3833f6b-9332-4a1f-ad98-6c3f8e30e163",
54-
"property": "total",
5554
"description": "Three different books",
5655
"comments": ["Suggested grouping, [[1,2,3]]."],
56+
"property": "total",
5757
"input": {
5858
"basket": [1, 2, 3]
5959
},
6060
"expected": 2160
6161
},
6262
{
6363
"uuid": "1951a1db-2fb6-4cd1-a69a-f691b6dd30a2",
64-
"property": "total",
6564
"description": "Four different books",
6665
"comments": ["Suggested grouping, [[1,2,3,4]]."],
66+
"property": "total",
6767
"input": {
6868
"basket": [1, 2, 3, 4]
6969
},
7070
"expected": 2560
7171
},
7272
{
7373
"uuid": "d70f6682-3019-4c3f-aede-83c6a8c647a3",
74-
"property": "total",
7574
"description": "Five different books",
7675
"comments": ["Suggested grouping, [[1,2,3,4,5]]."],
76+
"property": "total",
7777
"input": {
7878
"basket": [1, 2, 3, 4, 5]
7979
},
8080
"expected": 3000
8181
},
8282
{
8383
"uuid": "78cacb57-911a-45f1-be52-2a5bd428c634",
84-
"property": "total",
8584
"description": "Two groups of four is cheaper than group of five plus group of three",
8685
"comments": ["Suggested grouping, [[1,2,3,4],[1,2,3,5]]."],
86+
"property": "total",
8787
"input": {
8888
"basket": [1, 1, 2, 2, 3, 3, 4, 5]
8989
},
9090
"expected": 5120
9191
},
9292
{
9393
"uuid": "f808b5a4-e01f-4c0d-881f-f7b90d9739da",
94-
"property": "total",
9594
"description": "Two groups of four is cheaper than groups of five and three",
9695
"comments": [
9796
"Suggested grouping, [[1,2,4,5],[1,3,4,5]]. This differs from the other 'two groups of four' test in that it will fail for solutions that add books to groups in the order in which they appear in the list."
9897
],
98+
"property": "total",
9999
"input": {
100100
"basket": [1, 1, 2, 3, 4, 4, 5, 5]
101101
},
102102
"expected": 5120
103103
},
104104
{
105105
"uuid": "fe96401c-5268-4be2-9d9e-19b76478007c",
106-
"property": "total",
107106
"description": "Group of four plus group of two is cheaper than two groups of three",
108107
"comments": ["Suggested grouping, [[1,2,3,4],[1,2]]."],
108+
"property": "total",
109109
"input": {
110110
"basket": [1, 1, 2, 2, 3, 4]
111111
},
112112
"expected": 4080
113113
},
114114
{
115115
"uuid": "68ea9b78-10ad-420e-a766-836a501d3633",
116-
"property": "total",
117116
"description": "Two each of first 4 books and 1 copy each of rest",
118117
"comments": ["Suggested grouping, [[1,2,3,4,5],[1,2,3,4]]."],
118+
"property": "total",
119119
"input": {
120120
"basket": [1, 1, 2, 2, 3, 3, 4, 4, 5]
121121
},
122122
"expected": 5560
123123
},
124124
{
125125
"uuid": "c0a779d5-a40c-47ae-9828-a340e936b866",
126-
"property": "total",
127126
"description": "Two copies of each book",
128127
"comments": ["Suggested grouping, [[1,2,3,4,5],[1,2,3,4,5]]."],
128+
"property": "total",
129129
"input": {
130130
"basket": [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
131131
},
132132
"expected": 6000
133133
},
134134
{
135135
"uuid": "18fd86fe-08f1-4b68-969b-392b8af20513",
136-
"property": "total",
137136
"description": "Three copies of first book and 2 each of remaining",
138137
"comments": ["Suggested grouping, [[1,2,3,4,5],[1,2,3,4,5],[1]]."],
138+
"property": "total",
139139
"input": {
140140
"basket": [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1]
141141
},
142142
"expected": 6800
143143
},
144144
{
145145
"uuid": "0b19a24d-e4cf-4ec8-9db2-8899a41af0da",
146-
"property": "total",
147146
"description": "Three each of first 2 books and 2 each of remaining books",
148147
"comments": ["Suggested grouping, [[1,2,3,4,5],[1,2,3,4,5],[1,2]]."],
148+
"property": "total",
149149
"input": {
150150
"basket": [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2]
151151
},
152152
"expected": 7520
153153
},
154154
{
155155
"uuid": "bb376344-4fb2-49ab-ab85-e38d8354a58d",
156-
"property": "total",
157156
"description": "Four groups of four are cheaper than two groups each of five and three",
158157
"comments": [
159158
"Suggested grouping, [[1,2,3,4],[1,2,3,5],[1,2,3,4],[1,2,3,5]]."
160159
],
160+
"property": "total",
161161
"input": {
162162
"basket": [1, 1, 2, 2, 3, 3, 4, 5, 1, 1, 2, 2, 3, 3, 4, 5]
163163
},
164164
"expected": 10240
165165
},
166166
{
167167
"uuid": "5260ddde-2703-4915-b45a-e54dbbac4303",
168-
"property": "total",
169168
"description": "Check that groups of four are created properly even when there are more groups of three than groups of five",
170169
"comments": [
171170
"Suggested grouping, [[1,2,3,4],[1,2,3,5],[1,2,3,4],[1,2,3,5],[1,2,3],[1,2,3]]."
172171
],
172+
"property": "total",
173173
"input": {
174174
"basket": [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 5, 5]
175175
},
176176
"expected": 14560
177177
},
178178
{
179179
"uuid": "b0478278-c551-4747-b0fc-7e0be3158b1f",
180-
"property": "total",
181180
"description": "One group of one and four is cheaper than one group of two and three",
182181
"comments": ["Suggested grouping, [[1],[1,2,3,4]]."],
182+
"property": "total",
183183
"input": {
184184
"basket": [1, 1, 2, 3, 4]
185185
},
186186
"expected": 3360
187187
},
188188
{
189189
"uuid": "cf868453-6484-4ae1-9dfc-f8ee85bbde01",
190-
"property": "total",
191190
"description": "One group of one and two plus three groups of four is cheaper than one group of each size",
192191
"comments": [
193192
"Suggested grouping, [[5],[5,4],[5,4,3,2],[5,4,3,2],[5,4,3,1]]."
194193
],
194+
"property": "total",
195195
"input": {
196196
"basket": [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
197197
},

exercises/collatz-conjecture/canonical-data.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@
5050
},
5151
{
5252
"uuid": "2187673d-77d6-4543-975e-66df6c50e2da",
53+
"reimplements": "7d4750e6-def9-4b86-aec7-9f7eb44f95a3",
5354
"description": "zero is an error",
54-
"property": "steps",
5555
"comments": ["Collatz Conjecture holds only for positive integers"],
56-
"reimplements": "7d4750e6-def9-4b86-aec7-9f7eb44f95a3",
56+
"property": "steps",
5757
"input": {
5858
"number": 0
5959
},
@@ -74,9 +74,9 @@
7474
},
7575
{
7676
"uuid": "ec11f479-56bc-47fd-a434-bcd7a31a7a2e",
77+
"reimplements": "c6c795bf-a288-45e9-86a1-841359ad426d",
7778
"description": "negative value is an error",
7879
"comments": ["Collatz Conjecture holds only for positive integers"],
79-
"reimplements": "c6c795bf-a288-45e9-86a1-841359ad426d",
8080
"property": "steps",
8181
"input": {
8282
"number": -15

0 commit comments

Comments
 (0)