Skip to content

Commit 1607b8f

Browse files
Add split-second-stopwatch exercise (#2403)
* Add `last-lap` exercise * Add `last-lap` exercise * Add `split-second-stopwatch` exercise * Fix project file * Use generator
1 parent 3a3fc54 commit 1607b8f

File tree

13 files changed

+2459
-5
lines changed

13 files changed

+2459
-5
lines changed

config.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,6 +2713,20 @@
27132713
"for-loops"
27142714
],
27152715
"difficulty": 7
2716+
},
2717+
{
2718+
"slug": "split-second-stopwatch",
2719+
"name": "Split-Second Stopwatch",
2720+
"uuid": "1d95976d-b3db-4705-b345-0832f226620f",
2721+
"practices": [
2722+
"datetimes",
2723+
"classes"
2724+
],
2725+
"prerequisites": [
2726+
"datetimes",
2727+
"classes"
2728+
],
2729+
"difficulty": 5
27162730
}
27172731
],
27182732
"foregone": [

exercises/Exercises.sln

Lines changed: 1438 additions & 5 deletions
Large diffs are not rendered by default.
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: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
###############################
2+
# Core EditorConfig Options #
3+
###############################
4+
5+
; This file is for unifying the coding style for different editors and IDEs.
6+
; More information at:
7+
; https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
8+
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017
9+
10+
root = true
11+
12+
[*]
13+
indent_style = space
14+
15+
[SplitSecondStopwatch.cs]
16+
indent_size = 4
17+
18+
###############################
19+
# .NET Coding Conventions #
20+
###############################
21+
22+
# Organize usings
23+
dotnet_sort_system_directives_first = true
24+
dotnet_separate_import_directive_groups = true
25+
26+
# this. preferences
27+
dotnet_style_qualification_for_field = false:suggestion
28+
dotnet_style_qualification_for_property = false:suggestion
29+
dotnet_style_qualification_for_method = false:suggestion
30+
dotnet_style_qualification_for_event = false:suggestion
31+
32+
# Language keywords vs BCL types preferences
33+
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
34+
dotnet_style_predefined_type_for_member_access = true:suggestion
35+
36+
# Parentheses preferences
37+
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
38+
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
39+
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
40+
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion
41+
42+
# Modifier preferences
43+
dotnet_style_require_accessibility_modifiers = always:suggestion
44+
dotnet_style_readonly_field = true:suggestion
45+
46+
# Expression-level preferences
47+
dotnet_style_object_initializer = true:suggestion
48+
dotnet_style_collection_initializer = true:suggestion
49+
dotnet_style_explicit_tuple_names = true:suggestion
50+
dotnet_style_prefer_inferred_tuple_names = true:suggestion
51+
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
52+
dotnet_style_prefer_auto_properties = true:suggestion
53+
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
54+
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
55+
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
56+
dotnet_style_coalesce_expression = true:suggestion
57+
dotnet_style_null_propagation = true:suggestion
58+
59+
###############################
60+
# Naming Conventions #
61+
###############################
62+
63+
# Style Definitions
64+
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
65+
66+
# Use PascalCase for constant fields
67+
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
68+
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
69+
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
70+
dotnet_naming_symbols.constant_fields.applicable_kinds = field
71+
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
72+
dotnet_naming_symbols.constant_fields.required_modifiers = const
73+
74+
###############################
75+
# C# Code Style Rules #
76+
###############################
77+
78+
# var preferences
79+
csharp_style_var_for_built_in_types = true:none
80+
csharp_style_var_when_type_is_apparent = true:none
81+
csharp_style_var_elsewhere = true:none
82+
83+
# Expression-bodied members
84+
csharp_style_expression_bodied_methods = true:suggestion
85+
csharp_style_expression_bodied_constructors = true:suggestion
86+
csharp_style_expression_bodied_operators = true:suggestion
87+
csharp_style_expression_bodied_properties = true:suggestion
88+
csharp_style_expression_bodied_indexers = true:suggestion
89+
csharp_style_expression_bodied_accessors = true:suggestion
90+
91+
# Pattern-matching preferences
92+
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
93+
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
94+
95+
# Null-checking preferences
96+
csharp_style_throw_expression = true:suggestion
97+
csharp_style_conditional_delegate_call = true:suggestion
98+
99+
# Modifier preferences
100+
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
101+
102+
# Expression-level preferences
103+
csharp_prefer_braces = true:none
104+
csharp_prefer_simple_default_expression = true:suggestion
105+
csharp_style_deconstructed_variable_declaration = true:suggestion
106+
csharp_style_pattern_local_over_anonymous_function = true:suggestion
107+
csharp_style_inlined_variable_declaration = true:suggestion
108+
109+
###############################
110+
# C# Formatting Rules #
111+
###############################
112+
113+
# New line preferences
114+
csharp_new_line_before_open_brace = all
115+
csharp_new_line_before_else = true
116+
csharp_new_line_before_catch = true
117+
csharp_new_line_before_finally = true
118+
csharp_new_line_before_members_in_object_initializers = false
119+
csharp_new_line_before_members_in_anonymous_types = false
120+
csharp_new_line_between_query_expression_clauses = true
121+
122+
# Indentation preferences
123+
csharp_indent_case_contents = true
124+
csharp_indent_switch_labels = true
125+
csharp_indent_labels = flush_left
126+
127+
# Space preferences
128+
csharp_space_after_cast = false
129+
csharp_space_after_keywords_in_control_flow_statements = true
130+
csharp_space_between_method_declaration_parameter_list_parentheses = false
131+
csharp_space_between_method_call_parameter_list_parentheses = false
132+
csharp_space_before_colon_in_inheritance_clause = true
133+
csharp_space_after_colon_in_inheritance_clause = true
134+
csharp_space_around_binary_operators = before_and_after
135+
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
136+
csharp_space_between_method_call_name_and_opening_parenthesis = false
137+
csharp_space_between_method_call_empty_parameter_list_parentheses = false
138+
139+
# Wrapping preferences
140+
csharp_preserve_single_line_blocks = true
141+
csharp_preserve_single_line_statements = true
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
public enum StopwatchState
2+
{
3+
Ready,
4+
Running,
5+
Stopped
6+
}
7+
8+
public class SplitSecondStopwatch(TimeProvider time)
9+
{
10+
private List<TimeSpan> _previousLaps = [];
11+
private TimeSpan _currentLapTrackedTime = TimeSpan.Zero;
12+
private DateTimeOffset? _currentLapTrackingTimeSince;
13+
14+
private TimeSpan PreviousLapsTotal => _previousLaps.Aggregate(TimeSpan.Zero, (total, lap) => total + lap);
15+
private TimeSpan CurrentLapTrackingTime =>
16+
_currentLapTrackingTimeSince is null ? TimeSpan.Zero : time.GetUtcNow() - _currentLapTrackingTimeSince.Value;
17+
18+
public StopwatchState State { get; private set; } = StopwatchState.Ready;
19+
public TimeSpan CurrentLap => _currentLapTrackedTime + CurrentLapTrackingTime;
20+
public TimeSpan Total => CurrentLap + PreviousLapsTotal;
21+
public IReadOnlyCollection<TimeSpan> PreviousLaps => _previousLaps.AsReadOnly();
22+
23+
public void Start()
24+
{
25+
if (State == StopwatchState.Running)
26+
throw new InvalidOperationException();
27+
28+
_currentLapTrackingTimeSince = time.GetUtcNow();
29+
30+
State = StopwatchState.Running;
31+
}
32+
33+
public void Stop()
34+
{
35+
if (State != StopwatchState.Running)
36+
throw new InvalidOperationException();
37+
38+
_currentLapTrackedTime += CurrentLap;
39+
_currentLapTrackingTimeSince = null;
40+
41+
State = StopwatchState.Stopped;
42+
}
43+
44+
public void Reset()
45+
{
46+
if (State != StopwatchState.Stopped)
47+
throw new InvalidOperationException();
48+
49+
_previousLaps.Clear();
50+
_currentLapTrackingTimeSince = null;
51+
_currentLapTrackedTime = TimeSpan.Zero;
52+
53+
State = StopwatchState.Ready;
54+
}
55+
56+
public void Lap()
57+
{
58+
if (State != StopwatchState.Running)
59+
throw new InvalidOperationException();
60+
61+
_previousLaps.Add(CurrentLap);
62+
_currentLapTrackedTime = TimeSpan.Zero;
63+
_currentLapTrackingTimeSince = time.GetUtcNow();
64+
}
65+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{{ func to_timespan
2+
if $0 == "00:00:00"
3+
ret "TimeSpan.Zero"
4+
end
5+
6+
parts = string.split $0 ":"
7+
hours = parts[0] | string.to_int
8+
minutes = parts[1] | string.to_int
9+
seconds = parts[2] | string.to_int
10+
$"new TimeSpan({hours},{minutes},{seconds})"
11+
end }}
12+
13+
using Microsoft.Extensions.Time.Testing;
14+
15+
public class {{ testClass }}
16+
{
17+
{{- for test in tests }}
18+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
19+
public void {{ test.testMethod }}()
20+
{
21+
{{- for command in test.input.commands }}
22+
{{- if command.command == "new" }}
23+
var timeProvider = new FakeTimeProvider();
24+
var stopwatch = new {{ testedClass }}(timeProvider);
25+
{{- else if command.command == "state" }}
26+
Assert.Equal({{ command.expected | enum "StopwatchState" }}, stopwatch.State);
27+
{{- else if command.command == "currentLap" }}
28+
Assert.Equal({{ command.expected | to_timespan }}, stopwatch.CurrentLap);
29+
{{- else if command.command == "total" }}
30+
Assert.Equal({{ command.expected | to_timespan }}, stopwatch.Total);
31+
{{- else if command.command == "previousLaps" }}
32+
{{- if command.expected.empty? }}
33+
Assert.Empty(stopwatch.PreviousLaps);
34+
{{ else }}
35+
Assert.Equal([{{- for previousLap in command.expected }}{{ previousLap | to_timespan }}{{ if !for.last }}, {{ end }}{{ end -}}], stopwatch.PreviousLaps);
36+
{{ end -}}
37+
{{- else if command.command == "advanceTime" }}
38+
timeProvider.Advance({{ command.by | to_timespan }});
39+
{{- else }}
40+
{{- if command.expected && command.expected.error }}
41+
Assert.Throws<InvalidOperationException>(() => stopwatch.{{ command.command | pascalize }}());
42+
{{ else }}
43+
stopwatch.{{ command.command | pascalize }}();
44+
{{ end -}}
45+
{{ end -}}
46+
{{ end -}}
47+
}
48+
{{ end -}}
49+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"erikschierboom"
4+
],
5+
"files": {
6+
"solution": [
7+
"SplitSecondStopwatch.cs"
8+
],
9+
"test": [
10+
"SplitSecondStopwatchTests.cs"
11+
],
12+
"example": [
13+
".meta/Example.cs"
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+
}

0 commit comments

Comments
 (0)