some tests passed in clock

This commit is contained in:
rorikstr 2025-05-15 19:40:13 +03:00
parent 0ef500d9ef
commit e63c68efe8
21 changed files with 1181 additions and 0 deletions

View file

@ -0,0 +1,42 @@
{
"authors": [
"sacherjj"
],
"contributors": [
"attilahorvath",
"coriolinus",
"cwhakes",
"danieljl",
"eddyp",
"efx",
"ErikSchierboom",
"felix91gr",
"kunaltyagi",
"lutostag",
"nfiles",
"petertseng",
"rofrol",
"shaaraddalvi",
"stringparser",
"tmccombs",
"xakon",
"ZapAnton"
],
"files": {
"solution": [
"src/lib.rs",
"Cargo.toml"
],
"test": [
"tests/clock.rs"
],
"example": [
".meta/example.rs"
]
},
"blurb": "Implement a clock that handles times without dates.",
"source": "Pairing session with Erin Drummond",
"custom": {
"allowed-to-not-compile": "Stub doesn't compile because there is no to_string() implementation. This exercise is an introduction to derived and self-implemented traits, therefore adding template for a trait would reduce student learning."
}
}

2
rust/clock/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

9
rust/clock/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "clock"
version = "0.1.0"
edition = "2024"
# Not all libraries from crates.io are available in Exercism's test runner.
# The full list of available libraries is here:
# https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml
[dependencies]

86
rust/clock/HELP.md Normal file
View file

@ -0,0 +1,86 @@
# Help
## Running the tests
Execute the tests with:
```bash
$ cargo test
```
All but the first test have been ignored. After you get the first test to
pass, open the tests source file which is located in the `tests` directory
and remove the `#[ignore]` flag from the next test and get the tests to pass
again. Each separate test is a function with `#[test]` flag above it.
Continue, until you pass every test.
If you wish to run _only ignored_ tests without editing the tests source file, use:
```bash
$ cargo test -- --ignored
```
If you are using Rust 1.51 or later, you can run _all_ tests with
```bash
$ cargo test -- --include-ignored
```
To run a specific test, for example `some_test`, you can use:
```bash
$ cargo test some_test
```
If the specific test is ignored, use:
```bash
$ cargo test some_test -- --ignored
```
To learn more about Rust tests refer to the online [test documentation][rust-tests].
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
## Submitting your solution
You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
- The [Rust track's programming category on the forum](https://forum.exercism.org/c/programming/rust)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
## Rust Installation
Refer to the [exercism help page][help-page] for Rust installation and learning
resources.
## Submitting the solution
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.
## Feedback, Issues, Pull Requests
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!
If you want to know more about Exercism, take a look at the [contribution guide].
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
[help-page]: https://exercism.org/tracks/rust/learning
[github]: https://github.com/exercism/rust
[contribution guide]: https://exercism.org/docs/community/contributors

60
rust/clock/README.md Normal file
View file

@ -0,0 +1,60 @@
# Clock
Welcome to Clock on Exercism's Rust Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Implement a clock that handles times without dates.
You should be able to add and subtract minutes to it.
Two clocks that represent the same time should be equal to each other.
## Rust Traits for `.to_string()`
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][display-trait].
If so, try implementing the [Display trait][display-trait] for `Clock` instead.
Traits allow for a common way to implement functionality for various types.
For additional learning, consider how you might implement `String::from` for the `Clock` type.
You don't have to actually implement this—it's redundant with `Display`, which is generally the
better choice when the destination type is `String`—but it's useful to have a few type-conversion
traits in your toolkit.
[display-trait]: https://doc.rust-lang.org/std/fmt/trait.Display.html
## Source
### Created by
- @sacherjj
### Contributed to by
- @attilahorvath
- @coriolinus
- @cwhakes
- @danieljl
- @eddyp
- @efx
- @ErikSchierboom
- @felix91gr
- @kunaltyagi
- @lutostag
- @nfiles
- @petertseng
- @rofrol
- @shaaraddalvi
- @stringparser
- @tmccombs
- @xakon
- @ZapAnton
### Based on
Pairing session with Erin Drummond

61
rust/clock/src/lib.rs Normal file
View file

@ -0,0 +1,61 @@
#[derive(Debug)]
pub struct Clock {
pub hours: i32,
pub minutes: i32,
}
impl Clock {
pub fn new(hours: i32, minutes: i32) -> Self {
Clock { hours, minutes }
}
/*
40 => 0 (0*60 + 40 - 60*0)
60 => 1 (1*60 + 60 - 60*1)
90 => 1 (1*60 + 90 - 60*1)
150 => 150/60 = 2, (2*60 + 150 - 60*2)
23 => 23 (23 - 24 * 0)
26 => 2 (hours - 24 * hours_diff)
49 => 1 (hours - 24 * hours_diff)
*/
pub fn add_minutes(&self, minutes: i32) -> Self {
let hours_diff: i32 = (minutes as i32) / 60_i32;
let hours = self.hours + hours_diff;
let minutes = hours_diff*60 + minutes - 60*hours_diff;
let hours_diff = hours / 24_i32;
let hours = hours - 24 * hours_diff;
Clock { hours, minutes }
}
pub fn to_string(&self) -> String {
let hours = match self.hours.cmp(&10) {
std::cmp::Ordering::Less => format!("0{}",self.hours),
std::cmp::Ordering::Equal => format!("{}", self.hours),
std::cmp::Ordering::Greater => format!("{}", self.hours),
};
let minutes = match self.minutes.cmp(&10) {
std::cmp::Ordering::Less => format!("0{}",self.minutes),
std::cmp::Ordering::Equal => format!("{}", self.minutes),
std::cmp::Ordering::Greater => format!("{}", self.minutes),
};
format!("{}:{}", hours, minutes)
}
}
impl PartialEq for Clock {
fn eq(&self, other: &Self) -> bool {
match self.hours.cmp(&other.hours) {
std::cmp::Ordering::Less => false,
std::cmp::Ordering::Equal => {
match self.minutes.cmp(&other.minutes) {
std::cmp::Ordering::Less => false,
std::cmp::Ordering::Equal => true,
std::cmp::Ordering::Greater => false,
}
},
std::cmp::Ordering::Greater => false,
}
}
}

340
rust/clock/tests/clock.rs Normal file
View file

@ -0,0 +1,340 @@
use clock::*;
//
// Clock Creation
//
#[test]
fn on_the_hour() {
assert_eq!(Clock::new(8, 0).to_string(), "08:00");
}
#[test]
#[ignore]
fn past_the_hour() {
assert_eq!(Clock::new(11, 9).to_string(), "11:09");
}
#[test]
#[ignore]
fn midnight_is_zero_hours() {
assert_eq!(Clock::new(24, 0).to_string(), "00:00");
}
#[test]
#[ignore]
fn hour_rolls_over() {
assert_eq!(Clock::new(25, 0).to_string(), "01:00");
}
#[test]
#[ignore]
fn hour_rolls_over_continuously() {
assert_eq!(Clock::new(100, 0).to_string(), "04:00");
}
#[test]
#[ignore]
fn sixty_minutes_is_next_hour() {
assert_eq!(Clock::new(1, 60).to_string(), "02:00");
}
#[test]
#[ignore]
fn minutes_roll_over() {
assert_eq!(Clock::new(0, 160).to_string(), "02:40");
}
#[test]
#[ignore]
fn minutes_roll_over_continuously() {
assert_eq!(Clock::new(0, 1723).to_string(), "04:43");
}
#[test]
#[ignore]
fn hour_and_minutes_roll_over() {
assert_eq!(Clock::new(25, 160).to_string(), "03:40");
}
#[test]
#[ignore]
fn hour_and_minutes_roll_over_continuously() {
assert_eq!(Clock::new(201, 3001).to_string(), "11:01");
}
#[test]
#[ignore]
fn hour_and_minutes_roll_over_to_exactly_midnight() {
assert_eq!(Clock::new(72, 8640).to_string(), "00:00");
}
#[test]
#[ignore]
fn negative_hour() {
assert_eq!(Clock::new(-1, 15).to_string(), "23:15");
}
#[test]
#[ignore]
fn negative_hour_rolls_over() {
assert_eq!(Clock::new(-25, 0).to_string(), "23:00");
}
#[test]
#[ignore]
fn negative_hour_rolls_over_continuously() {
assert_eq!(Clock::new(-91, 0).to_string(), "05:00");
}
#[test]
#[ignore]
fn negative_minutes() {
assert_eq!(Clock::new(1, -40).to_string(), "00:20");
}
#[test]
#[ignore]
fn negative_minutes_roll_over() {
assert_eq!(Clock::new(1, -160).to_string(), "22:20");
}
#[test]
#[ignore]
fn negative_minutes_roll_over_continuously() {
assert_eq!(Clock::new(1, -4820).to_string(), "16:40");
}
#[test]
#[ignore]
fn negative_sixty_minutes_is_previous_hour() {
assert_eq!(Clock::new(2, -60).to_string(), "01:00");
}
#[test]
#[ignore]
fn negative_hour_and_minutes_both_roll_over() {
assert_eq!(Clock::new(-25, -160).to_string(), "20:20");
}
#[test]
#[ignore]
fn negative_hour_and_minutes_both_roll_over_continuously() {
assert_eq!(Clock::new(-121, -5810).to_string(), "22:10");
}
//
// Clock Math
//
#[test]
#[ignore]
fn add_minutes() {
let clock = Clock::new(10, 0).add_minutes(3);
assert_eq!(clock.to_string(), "10:03");
}
#[test]
#[ignore]
fn add_no_minutes() {
let clock = Clock::new(6, 41).add_minutes(0);
assert_eq!(clock.to_string(), "06:41");
}
#[test]
#[ignore]
fn add_to_next_hour() {
let clock = Clock::new(0, 45).add_minutes(40);
assert_eq!(clock.to_string(), "01:25");
}
#[test]
#[ignore]
fn add_more_than_one_hour() {
let clock = Clock::new(10, 0).add_minutes(61);
assert_eq!(clock.to_string(), "11:01");
}
#[test]
#[ignore]
fn add_more_than_two_hours_with_carry() {
let clock = Clock::new(0, 45).add_minutes(160);
assert_eq!(clock.to_string(), "03:25");
}
#[test]
#[ignore]
fn add_across_midnight() {
let clock = Clock::new(23, 59).add_minutes(2);
assert_eq!(clock.to_string(), "00:01");
}
#[test]
#[ignore]
fn add_more_than_one_day_1500_min_25_hrs() {
let clock = Clock::new(5, 32).add_minutes(1500);
assert_eq!(clock.to_string(), "06:32");
}
#[test]
#[ignore]
fn add_more_than_two_days() {
let clock = Clock::new(1, 1).add_minutes(3500);
assert_eq!(clock.to_string(), "11:21");
}
#[test]
#[ignore]
fn subtract_minutes() {
let clock = Clock::new(10, 3).add_minutes(-3);
assert_eq!(clock.to_string(), "10:00");
}
#[test]
#[ignore]
fn subtract_to_previous_hour() {
let clock = Clock::new(10, 3).add_minutes(-30);
assert_eq!(clock.to_string(), "09:33");
}
#[test]
#[ignore]
fn subtract_more_than_an_hour() {
let clock = Clock::new(10, 3).add_minutes(-70);
assert_eq!(clock.to_string(), "08:53");
}
#[test]
#[ignore]
fn subtract_across_midnight() {
let clock = Clock::new(0, 3).add_minutes(-4);
assert_eq!(clock.to_string(), "23:59");
}
#[test]
#[ignore]
fn subtract_more_than_two_hours() {
let clock = Clock::new(0, 0).add_minutes(-160);
assert_eq!(clock.to_string(), "21:20");
}
#[test]
#[ignore]
fn subtract_more_than_two_hours_with_borrow() {
let clock = Clock::new(6, 15).add_minutes(-160);
assert_eq!(clock.to_string(), "03:35");
}
#[test]
#[ignore]
fn subtract_more_than_one_day_1500_min_25_hrs() {
let clock = Clock::new(5, 32).add_minutes(-1500);
assert_eq!(clock.to_string(), "04:32");
}
#[test]
#[ignore]
fn subtract_more_than_two_days() {
let clock = Clock::new(2, 20).add_minutes(-3000);
assert_eq!(clock.to_string(), "00:20");
}
//
// Test Equality
//
#[test]
#[ignore]
fn clocks_with_same_time() {
assert_eq!(Clock::new(15, 37), Clock::new(15, 37));
}
#[test]
#[ignore]
fn clocks_a_minute_apart() {
assert_ne!(Clock::new(15, 36), Clock::new(15, 37));
}
#[test]
#[ignore]
fn clocks_an_hour_apart() {
assert_ne!(Clock::new(14, 37), Clock::new(15, 37));
}
#[test]
#[ignore]
fn clocks_with_hour_overflow() {
assert_eq!(Clock::new(10, 37), Clock::new(34, 37));
}
#[test]
#[ignore]
fn clocks_with_hour_overflow_by_several_days() {
assert_eq!(Clock::new(3, 11), Clock::new(99, 11));
}
#[test]
#[ignore]
fn clocks_with_negative_hour() {
assert_eq!(Clock::new(22, 40), Clock::new(-2, 40));
}
#[test]
#[ignore]
fn clocks_with_negative_hour_that_wraps() {
assert_eq!(Clock::new(17, 3), Clock::new(-31, 3));
}
#[test]
#[ignore]
fn clocks_with_negative_hour_that_wraps_multiple_times() {
assert_eq!(Clock::new(13, 49), Clock::new(-83, 49));
}
#[test]
#[ignore]
fn clocks_with_minute_overflow() {
assert_eq!(Clock::new(0, 1), Clock::new(0, 1441));
}
#[test]
#[ignore]
fn clocks_with_minute_overflow_by_several_days() {
assert_eq!(Clock::new(2, 2), Clock::new(2, 4322));
}
#[test]
#[ignore]
fn clocks_with_negative_minute() {
assert_eq!(Clock::new(2, 40), Clock::new(3, -20));
}
#[test]
#[ignore]
fn clocks_with_negative_minute_that_wraps() {
assert_eq!(Clock::new(4, 10), Clock::new(5, -1490));
}
#[test]
#[ignore]
fn clocks_with_negative_minute_that_wraps_multiple_times() {
assert_eq!(Clock::new(6, 15), Clock::new(6, -4305));
}
#[test]
#[ignore]
fn clocks_with_negative_hours_and_minutes() {
assert_eq!(Clock::new(7, 32), Clock::new(-12, -268));
}
#[test]
#[ignore]
fn clocks_with_negative_hours_and_minutes_that_wrap() {
assert_eq!(Clock::new(18, 7), Clock::new(-54, -11513));
}
#[test]
#[ignore]
fn full_clock_and_zeroed_clock() {
assert_eq!(Clock::new(24, 0), Clock::new(0, 0));
}

View file

@ -0,0 +1,45 @@
{
"authors": [
"IanWhitney"
],
"contributors": [
"andy5995",
"ashleygwilliams",
"cbzehner",
"coriolinus",
"cwhakes",
"EduardoBautista",
"efx",
"ErikSchierboom",
"houhoulis",
"IanWhitney",
"janczer",
"leoyvens",
"lutostag",
"mkantor",
"nfiles",
"NieDzejkob",
"ocstl",
"petertseng",
"rofrol",
"sacherjj",
"stringparser",
"xakon",
"ZapAnton"
],
"files": {
"solution": [
"src/lib.rs",
"Cargo.toml"
],
"test": [
"tests/gigasecond.rs"
],
"example": [
".meta/example.rs"
]
},
"blurb": "Given a moment, determine the moment that would be after a gigasecond has passed.",
"source": "Chapter 9 in Chris Pine's online Learn to Program tutorial.",
"source_url": "https://pine.fm/LearnToProgram/?Chapter=09"
}

2
rust/gigasecond/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

View file

@ -0,0 +1,10 @@
[package]
name = "gigasecond"
version = "0.1.0"
edition = "2024"
# Not all libraries from crates.io are available in Exercism's test runner.
# The full list of available libraries is here:
# https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml
[dependencies]
time = "0.3"

86
rust/gigasecond/HELP.md Normal file
View file

@ -0,0 +1,86 @@
# Help
## Running the tests
Execute the tests with:
```bash
$ cargo test
```
All but the first test have been ignored. After you get the first test to
pass, open the tests source file which is located in the `tests` directory
and remove the `#[ignore]` flag from the next test and get the tests to pass
again. Each separate test is a function with `#[test]` flag above it.
Continue, until you pass every test.
If you wish to run _only ignored_ tests without editing the tests source file, use:
```bash
$ cargo test -- --ignored
```
If you are using Rust 1.51 or later, you can run _all_ tests with
```bash
$ cargo test -- --include-ignored
```
To run a specific test, for example `some_test`, you can use:
```bash
$ cargo test some_test
```
If the specific test is ignored, use:
```bash
$ cargo test some_test -- --ignored
```
To learn more about Rust tests refer to the online [test documentation][rust-tests].
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
## Submitting your solution
You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
- The [Rust track's programming category on the forum](https://forum.exercism.org/c/programming/rust)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
## Rust Installation
Refer to the [exercism help page][help-page] for Rust installation and learning
resources.
## Submitting the solution
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.
## Feedback, Issues, Pull Requests
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!
If you want to know more about Exercism, take a look at the [contribution guide].
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
[help-page]: https://exercism.org/tracks/rust/learning
[github]: https://github.com/exercism/rust
[contribution guide]: https://exercism.org/docs/community/contributors

76
rust/gigasecond/README.md Normal file
View file

@ -0,0 +1,76 @@
# Gigasecond
Welcome to Gigasecond on Exercism's Rust Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Introduction
The way we measure time is kind of messy.
We have 60 seconds in a minute, and 60 minutes in an hour.
This comes from ancient Babylon, where they used 60 as the basis for their number system.
We have 24 hours in a day, 7 days in a week, and how many days in a month?
Well, for days in a month it depends not only on which month it is, but also on what type of calendar is used in the country you live in.
What if, instead, we only use seconds to express time intervals?
Then we can use metric system prefixes for writing large numbers of seconds in more easily comprehensible quantities.
- A food recipe might explain that you need to let the brownies cook in the oven for two kiloseconds (that's two thousand seconds).
- Perhaps you and your family would travel to somewhere exotic for two megaseconds (that's two million seconds).
- And if you and your spouse were married for _a thousand million_ seconds, you would celebrate your one gigasecond anniversary.
~~~~exercism/note
If we ever colonize Mars or some other planet, measuring time is going to get even messier.
If someone says "year" do they mean a year on Earth or a year on Mars?
The idea for this exercise came from the science fiction novel ["A Deepness in the Sky"][vinge-novel] by author Vernor Vinge.
In it the author uses the metric system as the basis for time measurements.
[vinge-novel]: https://www.tor.com/2017/08/03/science-fiction-with-something-for-everyone-a-deepness-in-the-sky-by-vernor-vinge/
~~~~
## Instructions
Your task is to determine the date and time one gigasecond after a certain date.
A gigasecond is one thousand million seconds.
That is a one with nine zeros after it.
If you were born on _January 24th, 2015 at 22:00 (10:00:00pm)_, then you would be a gigasecond old on _October 2nd, 2046 at 23:46:40 (11:46:40pm)_.
If you're unsure what operations you can perform on `PrimitiveDateTime` take a look at the [time crate](https://docs.rs/time) which is listed as a dependency in the `Cargo.toml` file for this exercise.
## Source
### Created by
- @IanWhitney
### Contributed to by
- @andy5995
- @ashleygwilliams
- @cbzehner
- @coriolinus
- @cwhakes
- @EduardoBautista
- @efx
- @ErikSchierboom
- @houhoulis
- @IanWhitney
- @janczer
- @leoyvens
- @lutostag
- @mkantor
- @nfiles
- @NieDzejkob
- @ocstl
- @petertseng
- @rofrol
- @sacherjj
- @stringparser
- @xakon
- @ZapAnton
### Based on
Chapter 9 in Chris Pine's online Learn to Program tutorial. - https://pine.fm/LearnToProgram/?Chapter=09

View file

@ -0,0 +1,7 @@
use time::PrimitiveDateTime as DateTime;
use time::Duration;
// Returns a DateTime one billion seconds after start.
pub fn after(start: DateTime) -> DateTime {
start + Duration::seconds(1_000_000_000)
}

View file

@ -0,0 +1,59 @@
#[test]
fn date_only_specification_of_time() {
let start = datetime(2011, 4, 25, 0, 0, 0);
let actual = gigasecond::after(start);
let expected = datetime(2043, 1, 1, 1, 46, 40);
assert_eq!(actual, expected);
}
#[test]
#[ignore]
fn second_test_for_date_only_specification_of_time() {
let start = datetime(1977, 6, 13, 0, 0, 0);
let actual = gigasecond::after(start);
let expected = datetime(2009, 2, 19, 1, 46, 40);
assert_eq!(actual, expected);
}
#[test]
#[ignore]
fn third_test_for_date_only_specification_of_time() {
let start = datetime(1959, 7, 19, 0, 0, 0);
let actual = gigasecond::after(start);
let expected = datetime(1991, 3, 27, 1, 46, 40);
assert_eq!(actual, expected);
}
#[test]
#[ignore]
fn full_time_specified() {
let start = datetime(2015, 1, 24, 22, 0, 0);
let actual = gigasecond::after(start);
let expected = datetime(2046, 10, 2, 23, 46, 40);
assert_eq!(actual, expected);
}
#[test]
#[ignore]
fn full_time_with_day_roll_over() {
let start = datetime(2015, 1, 24, 23, 59, 59);
let actual = gigasecond::after(start);
let expected = datetime(2046, 10, 3, 1, 46, 39);
assert_eq!(actual, expected);
}
fn datetime(
year: i32,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
) -> time::PrimitiveDateTime {
use time::{Date, PrimitiveDateTime, Time};
PrimitiveDateTime::new(
Date::from_calendar_date(year, month.try_into().unwrap(), day).unwrap(),
Time::from_hms(hour, minute, second).unwrap(),
)
}

View file

@ -0,0 +1,38 @@
{
"authors": [
"coriolinus"
],
"contributors": [
"cbzehner",
"ccouzens",
"cwhakes",
"efx",
"ErikSchierboom",
"hunger",
"lutostag",
"ocstl",
"PaulT89",
"petertseng",
"rofrol",
"rrredface",
"stringparser",
"TheDarkula",
"xakon",
"ZapAnton"
],
"files": {
"solution": [
"src/lib.rs",
"Cargo.toml"
],
"test": [
"tests/reverse_string.rs"
],
"example": [
".meta/example.rs"
]
},
"blurb": "Reverse a given string.",
"source": "Introductory challenge to reverse an input string",
"source_url": "https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb"
}

2
rust/reverse-string/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

View file

@ -0,0 +1,12 @@
[package]
name = "reverse_string"
version = "0.1.0"
edition = "2024"
# Not all libraries from crates.io are available in Exercism's test runner.
# The full list of available libraries is here:
# https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml
[dependencies]
[features]
grapheme = []

View file

@ -0,0 +1,86 @@
# Help
## Running the tests
Execute the tests with:
```bash
$ cargo test
```
All but the first test have been ignored. After you get the first test to
pass, open the tests source file which is located in the `tests` directory
and remove the `#[ignore]` flag from the next test and get the tests to pass
again. Each separate test is a function with `#[test]` flag above it.
Continue, until you pass every test.
If you wish to run _only ignored_ tests without editing the tests source file, use:
```bash
$ cargo test -- --ignored
```
If you are using Rust 1.51 or later, you can run _all_ tests with
```bash
$ cargo test -- --include-ignored
```
To run a specific test, for example `some_test`, you can use:
```bash
$ cargo test some_test
```
If the specific test is ignored, use:
```bash
$ cargo test some_test -- --ignored
```
To learn more about Rust tests refer to the online [test documentation][rust-tests].
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
## Submitting your solution
You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
- The [Rust track's programming category on the forum](https://forum.exercism.org/c/programming/rust)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
## Rust Installation
Refer to the [exercism help page][help-page] for Rust installation and learning
resources.
## Submitting the solution
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.
## Feedback, Issues, Pull Requests
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!
If you want to know more about Exercism, take a look at the [contribution guide].
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
[help-page]: https://exercism.org/tracks/rust/learning
[github]: https://github.com/exercism/rust
[contribution guide]: https://exercism.org/docs/community/contributors

View file

@ -0,0 +1,68 @@
# Reverse String
Welcome to Reverse String on Exercism's Rust Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Introduction
Reversing strings (reading them from right to left, rather than from left to right) is a surprisingly common task in programming.
For example, in bioinformatics, reversing the sequence of DNA or RNA strings is often important for various analyses, such as finding complementary strands or identifying palindromic sequences that have biological significance.
## Instructions
Your task is to reverse a given string.
Some examples:
- Turn `"stressed"` into `"desserts"`.
- Turn `"strops"` into `"sports"`.
- Turn `"racecar"` into `"racecar"`.
## Bonus
Test your function on this string: `uüu` and see what happens.
Try to write a function that properly reverses this string.
Hint: grapheme clusters
To get the bonus test to run, remove the ignore flag (`#[ignore]`) from the last test, and execute the tests with:
```bash
cargo test --features grapheme
```
You will need to use external libraries (a `crate` in rust lingo) for the bonus task.
A good place to look for those is [crates.io](https://crates.io/), the official repository of crates.
Please remember that only a limited set of crates is supported by our test runner.
The full list is in [this file](https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml) under the section `[dependencies]`.
[Check the documentation](https://doc.rust-lang.org/cargo/guide/dependencies.html) for instructions on how to use external crates in your projects.
## Source
### Created by
- @coriolinus
### Contributed to by
- @cbzehner
- @ccouzens
- @cwhakes
- @efx
- @ErikSchierboom
- @hunger
- @lutostag
- @ocstl
- @PaulT89
- @petertseng
- @rofrol
- @rrredface
- @stringparser
- @TheDarkula
- @xakon
- @ZapAnton
### Based on
Introductory challenge to reverse an input string - https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb

View file

@ -0,0 +1,7 @@
pub fn reverse(input: &str) -> String {
let mut res = String::new();
for ch in input.chars().rev() {
res.push(ch);
}
res
}

View file

@ -0,0 +1,83 @@
use reverse_string::*;
#[test]
fn an_empty_string() {
let input = "";
let output = reverse(input);
let expected = "";
assert_eq!(output, expected);
}
#[test]
// #[ignore]
fn a_word() {
let input = "robot";
let output = reverse(input);
let expected = "tobor";
assert_eq!(output, expected);
}
#[test]
// #[ignore]
fn a_capitalized_word() {
let input = "Ramen";
let output = reverse(input);
let expected = "nemaR";
assert_eq!(output, expected);
}
#[test]
// #[ignore]
fn a_sentence_with_punctuation() {
let input = "I'm hungry!";
let output = reverse(input);
let expected = "!yrgnuh m'I";
assert_eq!(output, expected);
}
#[test]
// #[ignore]
fn a_palindrome() {
let input = "racecar";
let output = reverse(input);
let expected = "racecar";
assert_eq!(output, expected);
}
#[test]
// #[ignore]
fn an_even_sized_word() {
let input = "drawer";
let output = reverse(input);
let expected = "reward";
assert_eq!(output, expected);
}
#[test]
// #[ignore]
fn wide_characters() {
let input = "子猫";
let output = reverse(input);
let expected = "猫子";
assert_eq!(output, expected);
}
#[test]
// #[ignore]
#[cfg(feature = "grapheme")]
fn grapheme_cluster_with_pre_combined_form() {
let input = "Würstchenstand";
let output = reverse(input);
let expected = "dnatsnehctsrüW";
assert_eq!(output, expected);
}
#[test]
// #[ignore]
#[cfg(feature = "grapheme")]
fn grapheme_clusters() {
let input = "ผู้เขียนโปรแกรม";
let output = reverse(input);
let expected = "มรกแรปโนยขีเผู้";
assert_eq!(output, expected);
}