Skip to content

Commit 902c1c5

Browse files
authored
Add practice exercise: clock (#199)
1 parent ef8dc66 commit 902c1c5

File tree

11 files changed

+439
-0
lines changed

11 files changed

+439
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@
178178
"prerequisites": [],
179179
"difficulty": 5
180180
},
181+
{
182+
"slug": "clock",
183+
"name": "Clock",
184+
"uuid": "9f346edc-0b89-4616-b62c-2119325fde2a",
185+
"practices": [],
186+
"prerequisites": [],
187+
"difficulty": 5
188+
},
181189
{
182190
"slug": "eliuds-eggs",
183191
"name": "Eliud's Eggs",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SQLite-specific instructions
2+
3+
## JSON documentation
4+
5+
[JSON Functions And Operators][json-docs]
6+
7+
[json-docs]: https://www.sqlite.org/json1.html
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Instructions
2+
3+
Implement a clock that handles times without dates.
4+
5+
You should be able to add and subtract minutes to it.
6+
7+
Two clocks that represent the same time should be equal to each other.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"authors": [
3+
"jimmytty"
4+
],
5+
"files": {
6+
"solution": [
7+
"clock.sql"
8+
],
9+
"test": [
10+
"clock_test.sql"
11+
],
12+
"example": [
13+
".meta/example.sql"
14+
]
15+
},
16+
"blurb": "Implement a clock that handles times without dates.",
17+
"source": "Pairing session with Erin Drummond"
18+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
UPDATE clock
2+
SET result =
3+
STRFTIME(
4+
'%H:%M',
5+
TIME('00:00',
6+
PRINTF('%d HOUR', JSON_EXTRACT(input, '$.hour')),
7+
PRINTF('%d MINUTE', JSON_EXTRACT(input, '$.minute'))
8+
)
9+
)
10+
WHERE property = 'create'
11+
;
12+
13+
UPDATE clock
14+
SET result =
15+
STRFTIME('%H:%M',
16+
TIME(
17+
'00:00',
18+
PRINTF('%d HOUR', JSON_EXTRACT(input, '$.hour')),
19+
PRINTF('%d minute', JSON_EXTRACT(input, '$.minute')),
20+
PRINTF('+%d minute', JSON_EXTRACT(input, '$.value'))
21+
)
22+
)
23+
WHERE property = 'add'
24+
;
25+
26+
UPDATE clock
27+
SET result =
28+
STRFTIME('%H:%M',
29+
TIME(
30+
'00:00',
31+
PRINTF('%d HOUR', JSON_EXTRACT(input, '$.hour')),
32+
PRINTF('%d minute', JSON_EXTRACT(input, '$.minute')),
33+
PRINTF('-%d minute', JSON_EXTRACT(input, '$.value'))
34+
)
35+
)
36+
WHERE property = 'subtract'
37+
;
38+
39+
UPDATE clock
40+
SET result =
41+
TIME('00:00',
42+
PRINTF('%d HOUR', JSON_EXTRACT(input, '$.clock1.hour' )),
43+
PRINTF('%d MINUTE', JSON_EXTRACT(input, '$.clock1.minute'))
44+
) =
45+
TIME('00:00',
46+
PRINTF('%d HOUR', JSON_EXTRACT(input, '$.clock2.hour' )),
47+
PRINTF('%d MINUTE', JSON_EXTRACT(input, '$.clock2.minute'))
48+
)
49+
WHERE property = 'equal';
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
[a577bacc-106b-496e-9792-b3083ea8705e]
13+
description = "Create a new clock with an initial time -> on the hour"
14+
15+
[b5d0c360-3b88-489b-8e84-68a1c7a4fa23]
16+
description = "Create a new clock with an initial time -> past the hour"
17+
18+
[473223f4-65f3-46ff-a9f7-7663c7e59440]
19+
description = "Create a new clock with an initial time -> midnight is zero hours"
20+
21+
[ca95d24a-5924-447d-9a96-b91c8334725c]
22+
description = "Create a new clock with an initial time -> hour rolls over"
23+
24+
[f3826de0-0925-4d69-8ac8-89aea7e52b78]
25+
description = "Create a new clock with an initial time -> hour rolls over continuously"
26+
27+
[a02f7edf-dfd4-4b11-b21a-86de3cc6a95c]
28+
description = "Create a new clock with an initial time -> sixty minutes is next hour"
29+
30+
[8f520df6-b816-444d-b90f-8a477789beb5]
31+
description = "Create a new clock with an initial time -> minutes roll over"
32+
33+
[c75c091b-47ac-4655-8d40-643767fc4eed]
34+
description = "Create a new clock with an initial time -> minutes roll over continuously"
35+
36+
[06343ecb-cf39-419d-a3f5-dcbae0cc4c57]
37+
description = "Create a new clock with an initial time -> hour and minutes roll over"
38+
39+
[be60810e-f5d9-4b58-9351-a9d1e90e660c]
40+
description = "Create a new clock with an initial time -> hour and minutes roll over continuously"
41+
42+
[1689107b-0b5c-4bea-aad3-65ec9859368a]
43+
description = "Create a new clock with an initial time -> hour and minutes roll over to exactly midnight"
44+
45+
[d3088ee8-91b7-4446-9e9d-5e2ad6219d91]
46+
description = "Create a new clock with an initial time -> negative hour"
47+
48+
[77ef6921-f120-4d29-bade-80d54aa43b54]
49+
description = "Create a new clock with an initial time -> negative hour rolls over"
50+
51+
[359294b5-972f-4546-bb9a-a85559065234]
52+
description = "Create a new clock with an initial time -> negative hour rolls over continuously"
53+
54+
[509db8b7-ac19-47cc-bd3a-a9d2f30b03c0]
55+
description = "Create a new clock with an initial time -> negative minutes"
56+
57+
[5d6bb225-130f-4084-84fd-9e0df8996f2a]
58+
description = "Create a new clock with an initial time -> negative minutes roll over"
59+
60+
[d483ceef-b520-4f0c-b94a-8d2d58cf0484]
61+
description = "Create a new clock with an initial time -> negative minutes roll over continuously"
62+
63+
[1cd19447-19c6-44bf-9d04-9f8305ccb9ea]
64+
description = "Create a new clock with an initial time -> negative sixty minutes is previous hour"
65+
66+
[9d3053aa-4f47-4afc-bd45-d67a72cef4dc]
67+
description = "Create a new clock with an initial time -> negative hour and minutes both roll over"
68+
69+
[51d41fcf-491e-4ca0-9cae-2aa4f0163ad4]
70+
description = "Create a new clock with an initial time -> negative hour and minutes both roll over continuously"
71+
72+
[d098e723-ad29-4ef9-997a-2693c4c9d89a]
73+
description = "Add minutes -> add minutes"
74+
75+
[b6ec8f38-e53e-4b22-92a7-60dab1f485f4]
76+
description = "Add minutes -> add no minutes"
77+
78+
[efd349dd-0785-453e-9ff8-d7452a8e7269]
79+
description = "Add minutes -> add to next hour"
80+
81+
[749890f7-aba9-4702-acce-87becf4ef9fe]
82+
description = "Add minutes -> add more than one hour"
83+
84+
[da63e4c1-1584-46e3-8d18-c9dc802c1713]
85+
description = "Add minutes -> add more than two hours with carry"
86+
87+
[be167a32-3d33-4cec-a8bc-accd47ddbb71]
88+
description = "Add minutes -> add across midnight"
89+
90+
[6672541e-cdae-46e4-8be7-a820cc3be2a8]
91+
description = "Add minutes -> add more than one day (1500 min = 25 hrs)"
92+
93+
[1918050d-c79b-4cb7-b707-b607e2745c7e]
94+
description = "Add minutes -> add more than two days"
95+
96+
[37336cac-5ede-43a5-9026-d426cbe40354]
97+
description = "Subtract minutes -> subtract minutes"
98+
99+
[0aafa4d0-3b5f-4b12-b3af-e3a9e09c047b]
100+
description = "Subtract minutes -> subtract to previous hour"
101+
102+
[9b4e809c-612f-4b15-aae0-1df0acb801b9]
103+
description = "Subtract minutes -> subtract more than an hour"
104+
105+
[8b04bb6a-3d33-4e6c-8de9-f5de6d2c70d6]
106+
description = "Subtract minutes -> subtract across midnight"
107+
108+
[07c3bbf7-ce4d-4658-86e8-4a77b7a5ccd9]
109+
description = "Subtract minutes -> subtract more than two hours"
110+
111+
[90ac8a1b-761c-4342-9c9c-cdc3ed5db097]
112+
description = "Subtract minutes -> subtract more than two hours with borrow"
113+
114+
[2149f985-7136-44ad-9b29-ec023a97a2b7]
115+
description = "Subtract minutes -> subtract more than one day (1500 min = 25 hrs)"
116+
117+
[ba11dbf0-ac27-4acb-ada9-3b853ec08c97]
118+
description = "Subtract minutes -> subtract more than two days"
119+
120+
[f2fdad51-499f-4c9b-a791-b28c9282e311]
121+
description = "Compare two clocks for equality -> clocks with same time"
122+
123+
[5d409d4b-f862-4960-901e-ec430160b768]
124+
description = "Compare two clocks for equality -> clocks a minute apart"
125+
126+
[a6045fcf-2b52-4a47-8bb2-ef10a064cba5]
127+
description = "Compare two clocks for equality -> clocks an hour apart"
128+
129+
[66b12758-0be5-448b-a13c-6a44bce83527]
130+
description = "Compare two clocks for equality -> clocks with hour overflow"
131+
132+
[2b19960c-212e-4a71-9aac-c581592f8111]
133+
description = "Compare two clocks for equality -> clocks with hour overflow by several days"
134+
135+
[6f8c6541-afac-4a92-b0c2-b10d4e50269f]
136+
description = "Compare two clocks for equality -> clocks with negative hour"
137+
138+
[bb9d5a68-e324-4bf5-a75e-0e9b1f97a90d]
139+
description = "Compare two clocks for equality -> clocks with negative hour that wraps"
140+
141+
[56c0326d-565b-4d19-a26f-63b3205778b7]
142+
description = "Compare two clocks for equality -> clocks with negative hour that wraps multiple times"
143+
144+
[c90b9de8-ddff-4ffe-9858-da44a40fdbc2]
145+
description = "Compare two clocks for equality -> clocks with minute overflow"
146+
147+
[533a3dc5-59a7-491b-b728-a7a34fe325de]
148+
description = "Compare two clocks for equality -> clocks with minute overflow by several days"
149+
150+
[fff49e15-f7b7-4692-a204-0f6052d62636]
151+
description = "Compare two clocks for equality -> clocks with negative minute"
152+
153+
[605c65bb-21bd-43eb-8f04-878edf508366]
154+
description = "Compare two clocks for equality -> clocks with negative minute that wraps"
155+
156+
[b87e64ed-212a-4335-91fd-56da8421d077]
157+
description = "Compare two clocks for equality -> clocks with negative minute that wraps multiple times"
158+
159+
[822fbf26-1f3b-4b13-b9bf-c914816b53dd]
160+
description = "Compare two clocks for equality -> clocks with negative hours and minutes"
161+
162+
[e787bccd-cf58-4a1d-841c-ff80eaaccfaa]
163+
description = "Compare two clocks for equality -> clocks with negative hours and minutes that wrap"
164+
165+
[96969ca8-875a-48a1-86ae-257a528c44f5]
166+
description = "Compare two clocks for equality -> full clock and zeroed clock"

exercises/practice/clock/clock.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Schema:
2+
-- CREATE TABLE clock (
3+
-- property TEXT NOT NULL,
4+
-- input TEXT NOT NULL, -- json object
5+
-- result TEXT
6+
-- );
7+
--
8+
-- Task: update the clock table and set the result column based on the input.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
-- Create database:
2+
.read ./create_fixture.sql
3+
4+
-- Read user student solution and save any output as markdown in user_output.md:
5+
.mode markdown
6+
.output user_output.md
7+
.read ./clock.sql
8+
.output
9+
10+
-- Create a clean testing environment:
11+
.read ./create_test_table.sql
12+
13+
-- Comparison of user input and the tests updates the status for each test:
14+
UPDATE tests
15+
SET status = 'pass'
16+
FROM (SELECT property, input, result FROM clock) AS actual
17+
WHERE (actual.property, actual.input, actual.result) =
18+
(tests.property, tests.input, tests.expected) ;
19+
20+
-- Update message for failed tests to give helpful information:
21+
UPDATE tests
22+
SET message = (
23+
'Result for "'
24+
|| PRINTF('property=%s, input=%s', tests.property, tests.input)
25+
|| '"' || ' is <'
26+
|| COALESCE(actual.result, 'NULL')
27+
|| '> but should be <'
28+
|| tests.expected
29+
|| '>'
30+
)
31+
FROM (SELECT property, input, result FROM clock) AS actual
32+
WHERE (actual.property, actual.input) = (tests.property, tests.input)
33+
AND tests.status = 'fail';
34+
35+
-- Save results to ./output.json (needed by the online test-runner)
36+
.mode json
37+
.once './output.json'
38+
SELECT description, status, message, output, test_code, task_id FROM tests;
39+
40+
-- Display test results in readable form for the student:
41+
.mode table
42+
SELECT description, status, message FROM tests;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
DROP TABLE IF EXISTS clock;
2+
CREATE TABLE clock (
3+
property TEXT NOT NULL,
4+
input TEXT NOT NULL, -- json object
5+
result TEXT
6+
);
7+
8+
.mode csv
9+
.import ./data.csv clock
10+
11+
UPDATE clock SET result = NULL;

0 commit comments

Comments
 (0)