Exercism track submissions
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

clock

+615
+39
rust/clock/.exercism/config.json
··· 1 + { 2 + "blurb": "Implement a clock that handles times without dates.", 3 + "authors": [ 4 + "sacherjj" 5 + ], 6 + "contributors": [ 7 + "attilahorvath", 8 + "coriolinus", 9 + "cwhakes", 10 + "danieljl", 11 + "eddyp", 12 + "efx", 13 + "ErikSchierboom", 14 + "felix91gr", 15 + "kunaltyagi", 16 + "lutostag", 17 + "nfiles", 18 + "petertseng", 19 + "rofrol", 20 + "shaaraddalvi", 21 + "stringparser", 22 + "tmccombs", 23 + "xakon", 24 + "ZapAnton" 25 + ], 26 + "files": { 27 + "solution": [ 28 + "src/lib.rs" 29 + ], 30 + "test": [ 31 + "tests/clock.rs" 32 + ], 33 + "example": [ 34 + ".meta/example.rs" 35 + ] 36 + }, 37 + "source": "Pairing session with Erin Drummond", 38 + "source_url": "https://twitter.com/ebdrummond" 39 + }
+8
rust/clock/.gitignore
··· 1 + # Generated by Cargo 2 + # will have compiled files and executables 3 + /target/ 4 + **/*.rs.bk 5 + 6 + # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 7 + # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock 8 + Cargo.lock
+6
rust/clock/Cargo.toml
··· 1 + [package] 2 + edition = "2021" 3 + name = "clock" 4 + version = "2.4.0" 5 + 6 + [dependencies]
+85
rust/clock/HELP.md
··· 1 + # Help 2 + 3 + ## Running the tests 4 + 5 + Execute the tests with: 6 + 7 + ```bash 8 + $ cargo test 9 + ``` 10 + 11 + All but the first test have been ignored. After you get the first test to 12 + pass, open the tests source file which is located in the `tests` directory 13 + and remove the `#[ignore]` flag from the next test and get the tests to pass 14 + again. Each separate test is a function with `#[test]` flag above it. 15 + Continue, until you pass every test. 16 + 17 + If you wish to run _only ignored_ tests without editing the tests source file, use: 18 + 19 + ```bash 20 + $ cargo test -- --ignored 21 + ``` 22 + 23 + If you are using Rust 1.51 or later, you can run _all_ tests with 24 + 25 + ```bash 26 + $ cargo test -- --include-ignored 27 + ``` 28 + 29 + To run a specific test, for example `some_test`, you can use: 30 + 31 + ```bash 32 + $ cargo test some_test 33 + ``` 34 + 35 + If the specific test is ignored, use: 36 + 37 + ```bash 38 + $ cargo test some_test -- --ignored 39 + ``` 40 + 41 + To learn more about Rust tests refer to the online [test documentation][rust-tests]. 42 + 43 + [rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html 44 + 45 + ## Submitting your solution 46 + 47 + You can submit your solution using the `exercism submit src/lib.rs` command. 48 + This command will upload your solution to the Exercism website and print the solution page's URL. 49 + 50 + It's possible to submit an incomplete solution which allows you to: 51 + 52 + - See how others have completed the exercise 53 + - Request help from a mentor 54 + 55 + ## Need to get help? 56 + 57 + If you'd like help solving the exercise, check the following pages: 58 + 59 + - The [Rust track's documentation](https://exercism.org/docs/tracks/rust) 60 + - [Exercism's support channel on gitter](https://gitter.im/exercism/support) 61 + - The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) 62 + 63 + Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. 64 + 65 + ## Rust Installation 66 + 67 + Refer to the [exercism help page][help-page] for Rust installation and learning 68 + resources. 69 + 70 + ## Submitting the solution 71 + 72 + Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer. 73 + 74 + ## Feedback, Issues, Pull Requests 75 + 76 + The GitHub [track repository][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 are happy to help! 77 + 78 + If you want to know more about Exercism, take a look at the [contribution guide]. 79 + 80 + ## Submitting Incomplete Solutions 81 + It's possible to submit an incomplete solution so you can see how others have completed the exercise. 82 + 83 + [help-page]: https://exercism.io/tracks/rust/learning 84 + [github]: https://github.com/exercism/rust 85 + [contribution guide]: https://exercism.io/docs/community/contributors
+57
rust/clock/README.md
··· 1 + # Clock 2 + 3 + Welcome to Clock on Exercism's Rust Track. 4 + If you need help running the tests or submitting your code, check out `HELP.md`. 5 + 6 + ## Instructions 7 + 8 + Implement a clock that handles times without dates. 9 + 10 + You should be able to add and subtract minutes to it. 11 + 12 + Two clocks that represent the same time should be equal to each other. 13 + 14 + You will also need to implement `.to_string()` for the `Clock` struct. We will be using this to display the Clock's state. You can either do it via implementing it directly or using the [Display trait](https://doc.rust-lang.org/std/fmt/trait.Display.html). 15 + 16 + Did you implement `.to_string()` for the `Clock` struct? 17 + 18 + If so, try implementing the 19 + [Display trait](https://doc.rust-lang.org/std/fmt/trait.Display.html) for `Clock` instead. 20 + 21 + Traits allow for a common way to implement functionality for various types. 22 + 23 + For additional learning, consider how you might implement `String::from` for the `Clock` type. 24 + You don't have to actually implement this—it's redundant with `Display`, which is generally the 25 + better choice when the destination type is `String`—but it's useful to have a few type-conversion 26 + traits in your toolkit. 27 + 28 + ## Source 29 + 30 + ### Created by 31 + 32 + - @sacherjj 33 + 34 + ### Contributed to by 35 + 36 + - @attilahorvath 37 + - @coriolinus 38 + - @cwhakes 39 + - @danieljl 40 + - @eddyp 41 + - @efx 42 + - @ErikSchierboom 43 + - @felix91gr 44 + - @kunaltyagi 45 + - @lutostag 46 + - @nfiles 47 + - @petertseng 48 + - @rofrol 49 + - @shaaraddalvi 50 + - @stringparser 51 + - @tmccombs 52 + - @xakon 53 + - @ZapAnton 54 + 55 + ### Based on 56 + 57 + Pairing session with Erin Drummond - https://twitter.com/ebdrummond
+80
rust/clock/src/lib.rs
··· 1 + use std::fmt::Display; 2 + 3 + #[derive(Copy, Clone, PartialEq, Debug)] 4 + pub struct Clock(i32, i32); 5 + 6 + impl Display for Clock { 7 + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { 8 + write!(f, "{:02}:{:02}", self.0, self.1) 9 + } 10 + } 11 + 12 + fn adjust_hour(hrs: i32) -> i32 { 13 + match hrs.abs() { 14 + 24 => 0, 15 + 0..=23 => hrs, 16 + _ => hrs % 24, 17 + } 18 + } 19 + 20 + fn adjust_min(mins: i32) -> (i32, i32) { 21 + match mins.abs() { 22 + 60 => (mins / 60, 0), 23 + 0..=59 => (0, mins), 24 + _ => (adjust_hour(mins / 60), mins % 60), 25 + } 26 + } 27 + 28 + fn check_hr_neg(hr: i32) -> i32 { 29 + if hr.is_negative() { 30 + 24 - hr.abs() 31 + } else { 32 + hr 33 + } 34 + } 35 + 36 + fn check_min_neg(min: i32, hr: i32) -> (i32, i32) { 37 + if min.is_negative() { 38 + let h = check_hr_neg(adjust_hour(hr - 1)); 39 + let m = 60 - min.abs(); 40 + (h, m) 41 + } else { 42 + (hr, min) 43 + } 44 + } 45 + 46 + fn proc(hr: i32, min: i32) -> (i32, i32) { 47 + let (mut h, m) = adjust_min(min); 48 + h = check_hr_neg(adjust_hour(hr + h)); 49 + 50 + check_min_neg(m, h) 51 + } 52 + 53 + impl Clock { 54 + pub fn new(hours: i32, minutes: i32) -> Self { 55 + let mut x = Clock(0, 0); 56 + 57 + x.0 = check_hr_neg(adjust_hour(hours)); 58 + 59 + let (a, b) = proc(x.0, minutes); 60 + 61 + x.0 = a; 62 + x.1 = b; 63 + x 64 + } 65 + 66 + pub fn add_minutes(&self, minutes: i32) -> Self { 67 + let mut x = *self; 68 + 69 + let (h, m) = adjust_min(minutes); 70 + 71 + x.0 += h; 72 + x.1 += m; 73 + 74 + let (a, b) = proc(x.0, x.1); 75 + 76 + x.0 = a; 77 + x.1 = b; 78 + x 79 + } 80 + }
+340
rust/clock/tests/clock.rs
··· 1 + use clock::Clock; 2 + 3 + // 4 + // Clock Creation 5 + // 6 + 7 + #[test] 8 + fn test_on_the_hour() { 9 + assert_eq!(Clock::new(8, 0).to_string(), "08:00"); 10 + } 11 + 12 + #[test] 13 + fn test_past_the_hour() { 14 + assert_eq!(Clock::new(11, 9).to_string(), "11:09"); 15 + } 16 + 17 + #[test] 18 + fn test_midnight_is_zero_hours() { 19 + assert_eq!(Clock::new(24, 0).to_string(), "00:00"); 20 + } 21 + 22 + #[test] 23 + fn test_hour_rolls_over() { 24 + assert_eq!(Clock::new(25, 0).to_string(), "01:00"); 25 + } 26 + 27 + #[test] 28 + fn test_hour_rolls_over_continuously() { 29 + assert_eq!(Clock::new(100, 0).to_string(), "04:00"); 30 + } 31 + 32 + #[test] 33 + fn test_sixty_minutes_is_next_hour() { 34 + assert_eq!(Clock::new(1, 60).to_string(), "02:00"); 35 + } 36 + 37 + #[test] 38 + fn test_minutes_roll_over() { 39 + assert_eq!(Clock::new(0, 160).to_string(), "02:40"); 40 + } 41 + 42 + #[test] 43 + fn test_minutes_roll_over_continuously() { 44 + assert_eq!(Clock::new(0, 1723).to_string(), "04:43"); 45 + } 46 + 47 + #[test] 48 + 49 + fn test_hours_and_minutes_roll_over() { 50 + assert_eq!(Clock::new(25, 160).to_string(), "03:40"); 51 + } 52 + 53 + #[test] 54 + 55 + fn test_hours_and_minutes_roll_over_continuously() { 56 + assert_eq!(Clock::new(201, 3001).to_string(), "11:01"); 57 + } 58 + 59 + #[test] 60 + 61 + fn test_hours_and_minutes_roll_over_to_exactly_midnight() { 62 + assert_eq!(Clock::new(72, 8640).to_string(), "00:00"); 63 + } 64 + 65 + #[test] 66 + fn test_negative_hour() { 67 + assert_eq!(Clock::new(-1, 15).to_string(), "23:15"); 68 + } 69 + 70 + #[test] 71 + fn test_negative_hour_roll_over() { 72 + assert_eq!(Clock::new(-25, 00).to_string(), "23:00"); 73 + } 74 + 75 + #[test] 76 + fn test_negative_hour_roll_over_continuously() { 77 + assert_eq!(Clock::new(-91, 00).to_string(), "05:00"); 78 + } 79 + 80 + #[test] 81 + fn test_negative_minutes() { 82 + assert_eq!(Clock::new(1, -40).to_string(), "00:20"); 83 + } 84 + 85 + #[test] 86 + fn test_negative_minutes_roll_over() { 87 + assert_eq!(Clock::new(1, -160).to_string(), "22:20"); 88 + } 89 + 90 + #[test] 91 + 92 + fn test_negative_minutes_roll_over_continuously() { 93 + assert_eq!(Clock::new(1, -4820).to_string(), "16:40"); 94 + } 95 + 96 + #[test] 97 + 98 + fn test_negative_sixty_minutes_is_prev_hour() { 99 + assert_eq!(Clock::new(2, -60).to_string(), "01:00"); 100 + } 101 + 102 + #[test] 103 + 104 + fn test_negative_one_twenty_minutes_is_two_prev_hours() { 105 + assert_eq!(Clock::new(1, -120).to_string(), "23:00"); 106 + } 107 + 108 + #[test] 109 + 110 + fn test_negative_hour_and_minutes_both_roll_over() { 111 + assert_eq!(Clock::new(-25, -160).to_string(), "20:20"); 112 + } 113 + 114 + #[test] 115 + 116 + fn test_negative_hour_and_minutes_both_roll_over_continuously() { 117 + assert_eq!(Clock::new(-121, -5810).to_string(), "22:10"); 118 + } 119 + 120 + #[test] 121 + 122 + fn test_zero_hour_and_negative_minutes() { 123 + assert_eq!(Clock::new(0, -22).to_string(), "23:38"); 124 + } 125 + 126 + // 127 + // Clock Math 128 + // 129 + 130 + #[test] 131 + 132 + fn test_add_minutes() { 133 + let clock = Clock::new(10, 0).add_minutes(3); 134 + assert_eq!(clock.to_string(), "10:03"); 135 + } 136 + 137 + #[test] 138 + 139 + fn test_add_no_minutes() { 140 + let clock = Clock::new(6, 41).add_minutes(0); 141 + assert_eq!(clock.to_string(), "06:41"); 142 + } 143 + 144 + #[test] 145 + 146 + fn test_add_to_next_hour() { 147 + let clock = Clock::new(0, 45).add_minutes(40); 148 + assert_eq!(clock.to_string(), "01:25"); 149 + } 150 + 151 + #[test] 152 + 153 + fn test_add_more_than_one_hour() { 154 + let clock = Clock::new(10, 0).add_minutes(61); 155 + assert_eq!(clock.to_string(), "11:01"); 156 + } 157 + 158 + #[test] 159 + 160 + fn test_add_more_than_two_hours_with_carry() { 161 + let clock = Clock::new(0, 45).add_minutes(160); 162 + assert_eq!(clock.to_string(), "03:25"); 163 + } 164 + 165 + #[test] 166 + 167 + fn test_add_across_midnight() { 168 + let clock = Clock::new(23, 59).add_minutes(2); 169 + assert_eq!(clock.to_string(), "00:01"); 170 + } 171 + 172 + #[test] 173 + 174 + fn test_add_more_than_one_day() { 175 + let clock = Clock::new(5, 32).add_minutes(1500); 176 + assert_eq!(clock.to_string(), "06:32"); 177 + } 178 + 179 + #[test] 180 + 181 + fn test_add_more_than_two_days() { 182 + let clock = Clock::new(1, 1).add_minutes(3500); 183 + assert_eq!(clock.to_string(), "11:21"); 184 + } 185 + 186 + #[test] 187 + 188 + fn test_subtract_minutes() { 189 + let clock = Clock::new(10, 3).add_minutes(-3); 190 + assert_eq!(clock.to_string(), "10:00"); 191 + } 192 + 193 + #[test] 194 + 195 + fn test_subtract_to_previous_hour() { 196 + let clock = Clock::new(10, 3).add_minutes(-30); 197 + assert_eq!(clock.to_string(), "09:33"); 198 + } 199 + 200 + #[test] 201 + 202 + fn test_subtract_more_than_an_hour() { 203 + let clock = Clock::new(10, 3).add_minutes(-70); 204 + assert_eq!(clock.to_string(), "08:53"); 205 + } 206 + 207 + #[test] 208 + 209 + fn test_subtract_across_midnight() { 210 + let clock = Clock::new(0, 3).add_minutes(-4); 211 + assert_eq!(clock.to_string(), "23:59"); 212 + } 213 + 214 + #[test] 215 + 216 + fn test_subtract_more_than_two_hours() { 217 + let clock = Clock::new(0, 0).add_minutes(-160); 218 + assert_eq!(clock.to_string(), "21:20"); 219 + } 220 + 221 + #[test] 222 + 223 + fn test_subtract_more_than_two_hours_with_borrow() { 224 + let clock = Clock::new(6, 15).add_minutes(-160); 225 + assert_eq!(clock.to_string(), "03:35"); 226 + } 227 + 228 + #[test] 229 + 230 + fn test_subtract_more_than_one_day() { 231 + let clock = Clock::new(5, 32).add_minutes(-1500); 232 + assert_eq!(clock.to_string(), "04:32"); 233 + } 234 + 235 + #[test] 236 + 237 + fn test_subtract_more_than_two_days() { 238 + let clock = Clock::new(2, 20).add_minutes(-3000); 239 + assert_eq!(clock.to_string(), "00:20"); 240 + } 241 + 242 + // 243 + // Test Equality 244 + // 245 + 246 + #[test] 247 + 248 + fn test_compare_clocks_for_equality() { 249 + assert_eq!(Clock::new(15, 37), Clock::new(15, 37)); 250 + } 251 + 252 + #[test] 253 + 254 + fn test_compare_clocks_a_minute_apart() { 255 + assert_ne!(Clock::new(15, 36), Clock::new(15, 37)); 256 + } 257 + 258 + #[test] 259 + 260 + fn test_compare_clocks_an_hour_apart() { 261 + assert_ne!(Clock::new(14, 37), Clock::new(15, 37)); 262 + } 263 + 264 + #[test] 265 + 266 + fn test_compare_clocks_with_hour_overflow() { 267 + assert_eq!(Clock::new(10, 37), Clock::new(34, 37)); 268 + } 269 + 270 + #[test] 271 + 272 + fn test_compare_clocks_with_hour_overflow_by_several_days() { 273 + assert_eq!(Clock::new(99, 11), Clock::new(3, 11)); 274 + } 275 + 276 + #[test] 277 + 278 + fn test_compare_clocks_with_negative_hour() { 279 + assert_eq!(Clock::new(-2, 40), Clock::new(22, 40)); 280 + } 281 + 282 + #[test] 283 + 284 + fn test_compare_clocks_with_negative_hour_that_wraps() { 285 + assert_eq!(Clock::new(-31, 3), Clock::new(17, 3)); 286 + } 287 + 288 + #[test] 289 + 290 + fn test_compare_clocks_with_negative_hour_that_wraps_multiple_times() { 291 + assert_eq!(Clock::new(-83, 49), Clock::new(13, 49)); 292 + } 293 + 294 + #[test] 295 + 296 + fn test_compare_clocks_with_minutes_overflow() { 297 + assert_eq!(Clock::new(0, 1441), Clock::new(0, 1)); 298 + } 299 + 300 + #[test] 301 + 302 + fn test_compare_clocks_with_minutes_overflow_by_several_days() { 303 + assert_eq!(Clock::new(2, 4322), Clock::new(2, 2)); 304 + } 305 + 306 + #[test] 307 + 308 + fn test_compare_clocks_with_negative_minute() { 309 + assert_eq!(Clock::new(3, -20), Clock::new(2, 40)) 310 + } 311 + 312 + #[test] 313 + 314 + fn test_compare_clocks_with_negative_minute_that_wraps() { 315 + assert_eq!(Clock::new(5, -1490), Clock::new(4, 10)) 316 + } 317 + 318 + #[test] 319 + 320 + fn test_compare_clocks_with_negative_minute_that_wraps_multiple() { 321 + assert_eq!(Clock::new(6, -4305), Clock::new(6, 15)) 322 + } 323 + 324 + #[test] 325 + 326 + fn test_compare_clocks_with_negative_hours_and_minutes() { 327 + assert_eq!(Clock::new(-12, -268), Clock::new(7, 32)) 328 + } 329 + 330 + #[test] 331 + 332 + fn test_compare_clocks_with_negative_hours_and_minutes_that_wrap() { 333 + assert_eq!(Clock::new(-54, -11_513), Clock::new(18, 7)) 334 + } 335 + 336 + #[test] 337 + 338 + fn test_compare_full_clock_and_zeroed_clock() { 339 + assert_eq!(Clock::new(24, 0), Clock::new(0, 0)) 340 + }