Skip to content

Commit 411833b

Browse files
committed
clock: Add exercise
1 parent c70ab23 commit 411833b

File tree

8 files changed

+442
-1
lines changed

8 files changed

+442
-1
lines changed

config.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
"string concatenation",
114114
"modulus"
115115
]
116-
},
116+
},
117117
{
118118
"uuid": "4dc9b165-792a-4438-be80-df9aab6f6a9c",
119119
"slug": "run-length-encoding",
@@ -127,6 +127,18 @@
127127
"loop"
128128
]
129129
},
130+
{
131+
"uuid": "543a3ca2-fb9b-4f20-a873-ff23595d41df",
132+
"slug": "clock",
133+
"core": false,
134+
"unlocked_by": null,
135+
"difficulty": 4,
136+
"topics": [
137+
"traits",
138+
"derive",
139+
"struct"
140+
]
141+
},
130142
{
131143
"uuid": "2874216a-0822-4ec2-892e-d451fd89646a",
132144
"slug": "hamming",

exercises/clock/.meta/hints.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Rust Traits for .to_string()
2+
3+
Did you implement .to_string() for the Clock struct?
4+
5+
If so, try implementing the
6+
[Display trait](https://doc.rust-lang.org/std/fmt/trait.Display.html) for Clock instead.
7+
8+
Traits allow for a common way to implement functionality for various types.
9+
10+
For additional learning, how would you implement String::from for the Clock type?

exercises/clock/Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exercises/clock/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "clock"
3+
version = "1.0.0"
4+
authors = ["sacherjj <[email protected]>"]
5+
6+
[dependencies]

exercises/clock/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Clock
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.
8+
9+
## Rust Installation
10+
11+
Refer to the [exercism help page][help-page] for Rust installation and learning
12+
resources.
13+
14+
## Writing the Code
15+
16+
Execute the tests with:
17+
18+
```bash
19+
$ cargo test
20+
```
21+
22+
All but the first test have been ignored. After you get the first test to
23+
pass, remove the ignore flag (`#[ignore]`) from the next test and get the tests
24+
to pass again. The test file is located in the `tests` directory. You can
25+
also remove the ignore flag from all the tests to get them to run all at once
26+
if you wish.
27+
28+
Make sure to read the [Crates and Modules](https://doc.rust-lang.org/stable/book/crates-and-modules.html) chapter if you
29+
haven't already, it will help you with organizing your files.
30+
31+
## Feedback, Issues, Pull Requests
32+
33+
The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the [rust track team](https://github.com/orgs/exercism/teams/rust) are happy to help!
34+
35+
If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md).
36+
37+
[help-page]: http://exercism.io/languages/rust
38+
[crates-and-modules]: http://doc.rust-lang.org/stable/book/crates-and-modules.html
39+
40+
## Source
41+
42+
Pairing session with Erin Drummond
43+
[https://twitter.com/ebdrummond](https://twitter.com/ebdrummond)
44+
45+
## Submitting Incomplete Solutions
46+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

exercises/clock/example.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::fmt;
2+
3+
#[derive(Eq,PartialEq,Debug)]
4+
pub struct Clock {
5+
minutes: i32
6+
}
7+
8+
impl fmt::Display for Clock {
9+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10+
let hours = self.minutes / 60;
11+
let mins = self.minutes % 60;
12+
write!(f, "{:02}:{:02}", hours, mins)
13+
}
14+
}
15+
16+
impl Clock {
17+
pub fn new(hour: i32, minute: i32) -> Self {
18+
Clock::build(hour * 60 + minute)
19+
}
20+
21+
fn build(minutes: i32) -> Self {
22+
let mut mins = minutes;
23+
while mins < 0 {
24+
mins += 1440;
25+
}
26+
Clock { minutes: mins % 1440 }
27+
}
28+
29+
pub fn add_minutes(&mut self, minutes: i32) -> Self {
30+
Clock::build(self.minutes + minutes)
31+
}
32+
}

exercises/clock/src/lib.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)