Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@
"strings"
]
},
{
"slug": "split-second-stopwatch",
"name": "Split-Second Stopwatch",
"uuid": "bc1fc50f-36ed-4a16-92bd-3001c8cd53d3",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "secret-handshake",
"name": "Secret Handshake",
Expand Down
22 changes: 22 additions & 0 deletions exercises/practice/split-second-stopwatch/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Instructions

Your task is to build a stopwatch to keep precise track of lap times.

The stopwatch uses four commands (start, stop, lap, and reset) to keep track of:

1. The current lap's tracked time
2. Previously recorded lap times

What commands can be used depends on which state the stopwatch is in:

1. Ready: initial state
2. Running: tracking time
3. Stopped: not tracking time

| Command | Begin state | End state | Effect |
| ------- | ----------- | --------- | -------------------------------------------------------- |
| Start | Ready | Running | Start tracking time |
| Start | Stopped | Running | Resume tracking time |
| Stop | Running | Stopped | Stop tracking time |
| Lap | Running | Running | Add current lap to previous laps, then reset current lap |
| Reset | Stopped | Ready | Reset current lap and clear previous laps |
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Introduction

You've always run for the thrill of it — no schedules, no timers, just the sound of your feet on the pavement.
But now that you've joined a competitive running crew, things are getting serious.
Training sessions are timed to the second, and every split second counts.
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.
19 changes: 19 additions & 0 deletions exercises/practice/split-second-stopwatch/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"split-second-stopwatch.ts"
],
"test": [
"split-second-stopwatch.test.ts"
],
"example": [
".meta/proof.ci.ts"
]
},
"blurb": "Keep track of time through a digital stopwatch.",
"source": "Erik Schierboom",
"source_url": "https://github.com/exercism/problem-specifications/pull/2547"
}
77 changes: 77 additions & 0 deletions exercises/practice/split-second-stopwatch/.meta/proof.ci.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
export type State = 'ready' | 'running' | 'stopped'

export class SplitSecondStopwatch {
private _state: State = 'ready'
private totalSeconds = 0
private currentLapSeconds = 0
private previousLapSeconds: number[] = []

public get state(): State {
return this._state
}

public get currentLap(): string {
return this.formatTime(this.currentLapSeconds)
}

public get total(): string {
return this.formatTime(this.totalSeconds)
}

public get previousLaps(): string[] {
return this.previousLapSeconds.map((s) => this.formatTime(s))
}

public start(): void {
if (this._state === 'running') {
throw new Error('cannot start an already running stopwatch')
}
this._state = 'running'
}

public stop(): void {
if (this._state !== 'running') {
throw new Error('cannot stop a stopwatch that is not running')
}
this._state = 'stopped'
}

public lap(): void {
if (this._state !== 'running') {
throw new Error('cannot lap a stopwatch that is not running')
}
this.previousLapSeconds.push(this.currentLapSeconds)
this.currentLapSeconds = 0
}

public reset(): void {
if (this._state !== 'stopped') {
throw new Error('cannot reset a stopwatch that is not stopped')
}
this._state = 'ready'
this.totalSeconds = 0
this.currentLapSeconds = 0
this.previousLapSeconds = []
}

public advanceTime(duration: string): void {
if (this._state === 'running') {
const seconds = this.toSeconds(duration)
this.currentLapSeconds += seconds
this.totalSeconds += seconds
}
}

private toSeconds(duration: string): number {
const [h, m, s] = duration.split(':').map(Number)
return h * 3600 + m * 60 + s
}

private formatTime(seconds: number): string {
const h = Math.floor(seconds / 3600)
const m = Math.floor((seconds % 3600) / 60)
const s = seconds % 60

return `${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}:${String(s).padStart(2, '0')}`
}
}
97 changes: 97 additions & 0 deletions exercises/practice/split-second-stopwatch/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[ddb238ea-99d4-4eaa-a81d-3c917a525a23]
description = "new stopwatch starts in ready state"

[b19635d4-08ad-4ac3-b87f-aca10e844071]
description = "new stopwatch's current lap has no elapsed time"

[492eb532-268d-43ea-8a19-2a032067d335]
description = "new stopwatch's total has no elapsed time"

[8a892c1e-9ef7-4690-894e-e155a1fe4484]
description = "new stopwatch does not have previous laps"

[5b2705b6-a584-4042-ba3a-4ab8d0ab0281]
description = "start from ready state changes state to running"

[748235ce-1109-440b-9898-0a431ea179b6]
description = "start does not change previous laps"

[491487b1-593d-423e-a075-aa78d449ff1f]
description = "start initiates time tracking for current lap"

[a0a7ba2c-8db6-412c-b1b6-cb890e9b72ed]
description = "start initiates time tracking for total"

[7f558a17-ef6d-4a5b-803a-f313af7c41d3]
description = "start cannot be called from running state"

[32466eef-b2be-4d60-a927-e24fce52dab9]
description = "stop from running state changes state to stopped"

[621eac4c-8f43-4d99-919c-4cad776d93df]
description = "stop pauses time tracking for current lap"

[465bcc82-7643-41f2-97ff-5e817cef8db4]
description = "stop pauses time tracking for total"

[b1ba7454-d627-41ee-a078-891b2ed266fc]
description = "stop cannot be called from ready state"

[5c041078-0898-44dc-9d5b-8ebb5352626c]
description = "stop cannot be called from stopped state"

[3f32171d-8fbf-46b6-bc2b-0810e1ec53b7]
description = "start from stopped state changes state to running"

[626997cb-78d5-4fe8-b501-29fdef804799]
description = "start from stopped state resumes time tracking for current lap"

[58487c53-ab26-471c-a171-807ef6363319]
description = "start from stopped state resumes time tracking for total"

[091966e3-ed25-4397-908b-8bb0330118f8]
description = "lap adds current lap to previous laps"

[1aa4c5ee-a7d5-4d59-9679-419deef3c88f]
description = "lap resets current lap and resumes time tracking"

[4b46b92e-1b3f-46f6-97d2-0082caf56e80]
description = "lap continues time tracking for total"

[ea75d36e-63eb-4f34-97ce-8c70e620bdba]
description = "lap cannot be called from ready state"

[63731154-a23a-412d-a13f-c562f208eb1e]
description = "lap cannot be called from stopped state"

[e585ee15-3b3f-4785-976b-dd96e7cc978b]
description = "stop does not change previous laps"

[fc3645e2-86cf-4d11-97c6-489f031103f6]
description = "reset from stopped state changes state to ready"

[20fbfbf7-68ad-4310-975a-f5f132886c4e]
description = "reset resets current lap"

[00a8f7bb-dd5c-43e5-8705-3ef124007662]
description = "reset clears previous laps"

[76cea936-6214-4e95-b6d1-4d4edcf90499]
description = "reset cannot be called from ready state"

[ba4d8e69-f200-4721-b59e-90d8cf615153]
description = "reset cannot be called from running state"

[0b01751a-cb57-493f-bb86-409de6e84306]
description = "supports very long laps"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"recommendations": [
"arcanis.vscode-zipfs",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"cSpell.words": ["exercism"],
"search.exclude": {
"**/.yarn": true,
"**/.pnp.*": true
}
}
3 changes: 3 additions & 0 deletions exercises/practice/split-second-stopwatch/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
compressionLevel: mixed

enableGlobalCache: true
5 changes: 5 additions & 0 deletions exercises/practice/split-second-stopwatch/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
// eslint-disable-next-line @typescript-eslint/no-require-imports
presets: [[require('@exercism/babel-preset-typescript'), { corejs: '3.38' }]],
plugins: [],
}
26 changes: 26 additions & 0 deletions exercises/practice/split-second-stopwatch/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @ts-check

import tsEslint from 'typescript-eslint'
import config from '@exercism/eslint-config-typescript'
import maintainersConfig from '@exercism/eslint-config-typescript/maintainers.mjs'

export default [
...tsEslint.config(...config, {
files: ['.meta/proof.ci.ts', '.meta/exemplar.ts', '*.test.ts'],
extends: maintainersConfig,
}),
{
ignores: [
// # Protected or generated
'.git/**/*',
'.vscode/**/*',

//# When using npm
'node_modules/**/*',

// # Configuration files
'babel.config.cjs',
'jest.config.cjs',
],
},
]
22 changes: 22 additions & 0 deletions exercises/practice/split-second-stopwatch/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
verbose: true,
projects: ['<rootDir>'],
testMatch: [
'**/__tests__/**/*.[jt]s?(x)',
'**/test/**/*.[jt]s?(x)',
'**/?(*.)+(spec|test).[jt]s?(x)',
],
testPathIgnorePatterns: [
'/(?:production_)?node_modules/',
'.d.ts$',
'<rootDir>/test/fixtures',
'<rootDir>/test/helpers',
'__mocks__',
],
transform: {
'^.+\\.[jt]sx?$': 'babel-jest',
},
moduleNameMapper: {
'^(\\.\\/.+)\\.js$': '$1',
},
}
38 changes: 38 additions & 0 deletions exercises/practice/split-second-stopwatch/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@exercism/typescript-split-second-stopwatch",
"version": "1.0.0",
"description": "Exercism exercises in Typescript.",
"private": true,
"repository": {
"type": "git",
"url": "https://github.com/exercism/typescript"
},
"type": "module",
"engines": {
"node": "^18.16.0 || >=20.0.0"
},
"devDependencies": {
"@exercism/babel-preset-typescript": "^0.6.0",
"@exercism/eslint-config-typescript": "^0.8.0",
"@jest/globals": "^29.7.0",
"@types/node": "~22.7.6",
"babel-jest": "^29.7.0",
"core-js": "~3.38.1",
"eslint": "^9.12.0",
"expect": "^29.7.0",
"jest": "^29.7.0",
"prettier": "^3.3.3",
"tstyche": "^2.1.1",
"typescript": "~5.6.3",
"typescript-eslint": "^8.10.0"
},
"scripts": {
"test": "corepack yarn node test-runner.mjs",
"test:types": "corepack yarn tstyche",
"test:implementation": "corepack yarn jest --no-cache --passWithNoTests",
"lint": "corepack yarn lint:types && corepack yarn lint:ci",
"lint:types": "corepack yarn tsc --noEmit -p .",
"lint:ci": "corepack yarn eslint . --ext .tsx,.ts"
},
"packageManager": "yarn@4.5.1"
}
Loading
Loading