Skip to content

Commit 98d04a0

Browse files
Add split stopwatch (#757)
* Fixes to concept tree * Add Split-Second Stopwatch exercise with initial implementation and tests * Format files * Apply suggestion from @ryanplusplus --------- Co-authored-by: Ryan Hartlage <2488333+ryanplusplus@users.noreply.github.com>
1 parent 6764840 commit 98d04a0

9 files changed

Lines changed: 608 additions & 0 deletions

File tree

config.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,16 @@
10441044
"prerequisites": [],
10451045
"difficulty": 3
10461046
},
1047+
{
1048+
"slug": "split-second-stopwatch",
1049+
"name": "Split-Second Stopwatch",
1050+
"uuid": "2bf80456-020c-4adc-8e81-8dfe75da7083",
1051+
"practices": [
1052+
"classes"
1053+
],
1054+
"prerequisites": [],
1055+
"difficulty": 3
1056+
},
10471057
{
10481058
"slug": "baffling-birthdays",
10491059
"name": "Baffling Birthdays",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Instructions
2+
3+
Your task is to build a stopwatch to keep precise track of lap times.
4+
5+
The stopwatch uses four commands (start, stop, lap, and reset) to keep track of:
6+
7+
1. The current lap's tracked time
8+
2. Previously recorded lap times
9+
10+
What commands can be used depends on which state the stopwatch is in:
11+
12+
1. Ready: initial state
13+
2. Running: tracking time
14+
3. Stopped: not tracking time
15+
16+
| Command | Begin state | End state | Effect |
17+
| ------- | ----------- | --------- | -------------------------------------------------------- |
18+
| Start | Ready | Running | Start tracking time |
19+
| Start | Stopped | Running | Resume tracking time |
20+
| Stop | Running | Stopped | Stop tracking time |
21+
| Lap | Running | Running | Add current lap to previous laps, then reset current lap |
22+
| Reset | Stopped | Ready | Reset current lap and clear previous laps |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Introduction
2+
3+
You've always run for the thrill of it — no schedules, no timers, just the sound of your feet on the pavement.
4+
But now that you've joined a competitive running crew, things are getting serious.
5+
Training sessions are timed to the second, and every split second counts.
6+
To keep pace, you've picked up the _Split-Second Stopwatch_ — a sleek, high-tech gadget that's about to become your new best friend.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"meatball133"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/split_second_stopwatch.cr"
8+
],
9+
"test": [
10+
"spec/split_second_stopwatch_spec.cr"
11+
],
12+
"example": [
13+
".meta/src/example.cr"
14+
]
15+
},
16+
"blurb": "Keep track of time through a digital stopwatch.",
17+
"source": "Erik Schierboom",
18+
"source_url": "https://github.com/exercism/problem-specifications/pull/2547"
19+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class SplitSecondStopwatch
2+
def initialize
3+
@time = 0
4+
@split_times = [] of Int32
5+
@state = "ready"
6+
end
7+
8+
def start
9+
if @state == "running"
10+
raise ArgumentError.new("Stopwatch is already running")
11+
end
12+
13+
@state = "running"
14+
end
15+
16+
def stop
17+
if @state != "running"
18+
raise ArgumentError.new("Stopwatch is not running")
19+
end
20+
21+
@state = "stopped"
22+
end
23+
24+
def total : String
25+
convert_time(@time + @split_times.sum)
26+
end
27+
28+
def current_lap : String
29+
convert_time(@time)
30+
end
31+
32+
def previous_laps : Array(String)
33+
@split_times.map { |t| convert_time(t) }
34+
end
35+
36+
def state : String
37+
@state
38+
end
39+
40+
def advance_time(time : String)
41+
parts = time.split(":").map(&.to_i)
42+
total_seconds = parts[0] * 3600 + parts[1] * 60 + parts[2]
43+
if @state == "running"
44+
@time += total_seconds
45+
end
46+
end
47+
48+
def lap
49+
if @state != "running"
50+
raise ArgumentError.new("Stopwatch is not running")
51+
end
52+
53+
@split_times << @time
54+
@time = 0
55+
end
56+
57+
def reset
58+
if @state != "stopped"
59+
raise ArgumentError.new("Stopwatch needs to be stopped before it can be reset")
60+
end
61+
@time = 0
62+
@split_times.clear
63+
@state = "ready"
64+
end
65+
66+
private def convert_time(time : Int32) : String
67+
hours = time / 3600
68+
minutes = (time % 3600) / 60
69+
seconds = time % 60
70+
71+
"%02d:%02d:%02d" % [hours, minutes, seconds]
72+
end
73+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require "spec"
2+
require "../src/*"
3+
4+
describe "<%-= to_capitalized(@json["exercise"].to_s) %>" do
5+
<%- @json["cases"].as_a.each do |cases| %>
6+
<%= status()%> "<%-= cases["description"] %>" do
7+
<%- cases["input"]["commands"].as_a.each do |command| %>
8+
<%- if command["command"] == "new" %>
9+
stopwatch = SplitSecondStopwatch.new
10+
<%- elsif command["command"] == "state" || command["command"] == "currentLap" %>
11+
stopwatch.<%= command["command"].to_s.underscore %>.should eq("<%= command["expected"] %>")
12+
<%- elsif command["command"] == "previousLaps" %>
13+
stopwatch.previous_laps.should eq(<%= command["expected"].to_s == "[]" ? "[] of String": to_s_deep(command["expected"]) %>)
14+
<%- elsif command["command"] == "start" || command["command"] == "stop" || command["command"] == "lap" || command["command"] == "reset" %>
15+
<%- if command["expected"]? %>
16+
expect_raises(ArgumentError) do
17+
stopwatch.<%= command["command"] %>
18+
end
19+
<%- else -%>
20+
stopwatch.<%= command["command"] %>
21+
<%- end %>
22+
<%- elsif command["command"] == "advanceTime" %>
23+
stopwatch.advance_time("<%= command["by"] %>")
24+
<%- end %>
25+
<%- end %>
26+
end
27+
<% end %>
28+
end
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
[ddb238ea-99d4-4eaa-a81d-3c917a525a23]
13+
description = "new stopwatch starts in ready state"
14+
15+
[b19635d4-08ad-4ac3-b87f-aca10e844071]
16+
description = "new stopwatch's current lap has no elapsed time"
17+
18+
[492eb532-268d-43ea-8a19-2a032067d335]
19+
description = "new stopwatch's total has no elapsed time"
20+
21+
[8a892c1e-9ef7-4690-894e-e155a1fe4484]
22+
description = "new stopwatch does not have previous laps"
23+
24+
[5b2705b6-a584-4042-ba3a-4ab8d0ab0281]
25+
description = "start from ready state changes state to running"
26+
27+
[748235ce-1109-440b-9898-0a431ea179b6]
28+
description = "start does not change previous laps"
29+
30+
[491487b1-593d-423e-a075-aa78d449ff1f]
31+
description = "start initiates time tracking for current lap"
32+
33+
[a0a7ba2c-8db6-412c-b1b6-cb890e9b72ed]
34+
description = "start initiates time tracking for total"
35+
36+
[7f558a17-ef6d-4a5b-803a-f313af7c41d3]
37+
description = "start cannot be called from running state"
38+
39+
[32466eef-b2be-4d60-a927-e24fce52dab9]
40+
description = "stop from running state changes state to stopped"
41+
42+
[621eac4c-8f43-4d99-919c-4cad776d93df]
43+
description = "stop pauses time tracking for current lap"
44+
45+
[465bcc82-7643-41f2-97ff-5e817cef8db4]
46+
description = "stop pauses time tracking for total"
47+
48+
[b1ba7454-d627-41ee-a078-891b2ed266fc]
49+
description = "stop cannot be called from ready state"
50+
51+
[5c041078-0898-44dc-9d5b-8ebb5352626c]
52+
description = "stop cannot be called from stopped state"
53+
54+
[3f32171d-8fbf-46b6-bc2b-0810e1ec53b7]
55+
description = "start from stopped state changes state to running"
56+
57+
[626997cb-78d5-4fe8-b501-29fdef804799]
58+
description = "start from stopped state resumes time tracking for current lap"
59+
60+
[58487c53-ab26-471c-a171-807ef6363319]
61+
description = "start from stopped state resumes time tracking for total"
62+
63+
[091966e3-ed25-4397-908b-8bb0330118f8]
64+
description = "lap adds current lap to previous laps"
65+
66+
[1aa4c5ee-a7d5-4d59-9679-419deef3c88f]
67+
description = "lap resets current lap and resumes time tracking"
68+
69+
[4b46b92e-1b3f-46f6-97d2-0082caf56e80]
70+
description = "lap continues time tracking for total"
71+
72+
[ea75d36e-63eb-4f34-97ce-8c70e620bdba]
73+
description = "lap cannot be called from ready state"
74+
75+
[63731154-a23a-412d-a13f-c562f208eb1e]
76+
description = "lap cannot be called from stopped state"
77+
78+
[e585ee15-3b3f-4785-976b-dd96e7cc978b]
79+
description = "stop does not change previous laps"
80+
81+
[fc3645e2-86cf-4d11-97c6-489f031103f6]
82+
description = "reset from stopped state changes state to ready"
83+
84+
[20fbfbf7-68ad-4310-975a-f5f132886c4e]
85+
description = "reset resets current lap"
86+
87+
[00a8f7bb-dd5c-43e5-8705-3ef124007662]
88+
description = "reset clears previous laps"
89+
90+
[76cea936-6214-4e95-b6d1-4d4edcf90499]
91+
description = "reset cannot be called from ready state"
92+
93+
[ba4d8e69-f200-4721-b59e-90d8cf615153]
94+
description = "reset cannot be called from running state"
95+
96+
[0b01751a-cb57-493f-bb86-409de6e84306]
97+
description = "supports very long laps"

0 commit comments

Comments
 (0)