mirror of
https://codeberg.org/andyscott/exercism.git
synced 2024-11-09 05:10:47 -05:00
Compare commits
2 commits
c8ef7df34c
...
9b61bc3783
Author | SHA1 | Date | |
---|---|---|---|
9b61bc3783 | |||
ddebf2f85b |
37 changed files with 1202 additions and 0 deletions
45
rust/gigasecond/.exercism/config.json
Normal file
45
rust/gigasecond/.exercism/config.json
Normal 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"
|
||||||
|
}
|
1
rust/gigasecond/.exercism/metadata.json
Normal file
1
rust/gigasecond/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"track":"rust","exercise":"gigasecond","id":"46cf4dcc2c1d42f48213bc4054d72a04","url":"https://exercism.org/tracks/rust/exercises/gigasecond","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
8
rust/gigasecond/.gitignore
vendored
Normal file
8
rust/gigasecond/.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# Generated by Cargo
|
||||||
|
# will have compiled files and executables
|
||||||
|
/target/
|
||||||
|
**/*.rs.bk
|
||||||
|
|
||||||
|
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||||
|
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
|
||||||
|
Cargo.lock
|
7
rust/gigasecond/Cargo.toml
Normal file
7
rust/gigasecond/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
edition = "2021"
|
||||||
|
name = "gigasecond"
|
||||||
|
version = "2.0.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
time = "0.3"
|
86
rust/gigasecond/HELP.md
Normal file
86
rust/gigasecond/HELP.md
Normal 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
76
rust/gigasecond/README.md
Normal 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
|
6
rust/gigasecond/src/lib.rs
Normal file
6
rust/gigasecond/src/lib.rs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
use time::{Duration, PrimitiveDateTime as DateTime};
|
||||||
|
|
||||||
|
// Returns a DateTime one billion seconds after start.
|
||||||
|
pub fn after(start: DateTime) -> DateTime {
|
||||||
|
start + Duration::seconds(1_000_000_000)
|
||||||
|
}
|
48
rust/gigasecond/tests/gigasecond.rs
Normal file
48
rust/gigasecond/tests/gigasecond.rs
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
use time::PrimitiveDateTime as DateTime;
|
||||||
|
|
||||||
|
/// Create a datetime from the given numeric point in time.
|
||||||
|
///
|
||||||
|
/// Panics if any field is invalid.
|
||||||
|
fn dt(year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8) -> DateTime {
|
||||||
|
use time::{Date, Time};
|
||||||
|
|
||||||
|
DateTime::new(
|
||||||
|
Date::from_calendar_date(year, month.try_into().unwrap(), day).unwrap(),
|
||||||
|
Time::from_hms(hour, minute, second).unwrap(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn date() {
|
||||||
|
let start_date = dt(2011, 4, 25, 0, 0, 0);
|
||||||
|
|
||||||
|
assert_eq!(gigasecond::after(start_date), dt(2043, 1, 1, 1, 46, 40));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn another_date() {
|
||||||
|
let start_date = dt(1977, 6, 13, 0, 0, 0);
|
||||||
|
|
||||||
|
assert_eq!(gigasecond::after(start_date), dt(2009, 2, 19, 1, 46, 40));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn third_date() {
|
||||||
|
let start_date = dt(1959, 7, 19, 0, 0, 0);
|
||||||
|
|
||||||
|
assert_eq!(gigasecond::after(start_date), dt(1991, 3, 27, 1, 46, 40));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn datetime() {
|
||||||
|
let start_date = dt(2015, 1, 24, 22, 0, 0);
|
||||||
|
|
||||||
|
assert_eq!(gigasecond::after(start_date), dt(2046, 10, 2, 23, 46, 40));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn another_datetime() {
|
||||||
|
let start_date = dt(2015, 1, 24, 23, 59, 59);
|
||||||
|
|
||||||
|
assert_eq!(gigasecond::after(start_date), dt(2046, 10, 3, 1, 46, 39));
|
||||||
|
}
|
42
rust/hello-world/.exercism/config.json
Normal file
42
rust/hello-world/.exercism/config.json
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"EduardoBautista"
|
||||||
|
],
|
||||||
|
"contributors": [
|
||||||
|
"ashleygwilliams",
|
||||||
|
"ClashTheBunny",
|
||||||
|
"coriolinus",
|
||||||
|
"cwhakes",
|
||||||
|
"dvoytik",
|
||||||
|
"EduardoBautista",
|
||||||
|
"efx",
|
||||||
|
"ErikSchierboom",
|
||||||
|
"hydhknn",
|
||||||
|
"IanWhitney",
|
||||||
|
"ijanos",
|
||||||
|
"kytrinyx",
|
||||||
|
"lutostag",
|
||||||
|
"nfiles",
|
||||||
|
"petertseng",
|
||||||
|
"regnerjr",
|
||||||
|
"rofrol",
|
||||||
|
"stringparser",
|
||||||
|
"xakon",
|
||||||
|
"ZapAnton"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"src/lib.rs",
|
||||||
|
"Cargo.toml"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"tests/hello-world.rs"
|
||||||
|
],
|
||||||
|
"example": [
|
||||||
|
".meta/example.rs"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"blurb": "Exercism's classic introductory exercise. Just say \"Hello, World!\".",
|
||||||
|
"source": "This is an exercise to introduce users to using Exercism",
|
||||||
|
"source_url": "https://en.wikipedia.org/wiki/%22Hello,_world!%22_program"
|
||||||
|
}
|
1
rust/hello-world/.exercism/metadata.json
Normal file
1
rust/hello-world/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"track":"rust","exercise":"hello-world","id":"8fc7d235b9714b939df049625c414d2c","url":"https://exercism.org/tracks/rust/exercises/hello-world","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
8
rust/hello-world/.gitignore
vendored
Normal file
8
rust/hello-world/.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# Generated by Cargo
|
||||||
|
# will have compiled files and executables
|
||||||
|
/target/
|
||||||
|
**/*.rs.bk
|
||||||
|
|
||||||
|
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||||
|
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
|
||||||
|
Cargo.lock
|
4
rust/hello-world/Cargo.toml
Normal file
4
rust/hello-world/Cargo.toml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
[package]
|
||||||
|
edition = "2021"
|
||||||
|
name = "hello-world"
|
||||||
|
version = "1.1.0"
|
92
rust/hello-world/GETTING_STARTED.md
Normal file
92
rust/hello-world/GETTING_STARTED.md
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
# Getting Started
|
||||||
|
|
||||||
|
These exercises lean on Test-Driven Development (TDD), but they're not
|
||||||
|
an exact match.
|
||||||
|
|
||||||
|
The following steps assume that you are in the same directory as the exercise.
|
||||||
|
|
||||||
|
You must have Rust installed.
|
||||||
|
Follow the [Installation chapter in the Rust book](https://doc.rust-lang.org/book/ch01-01-installation.html).
|
||||||
|
The [Rust language section](http://exercism.org/languages/rust)
|
||||||
|
section from exercism is also useful.
|
||||||
|
|
||||||
|
## Step 1
|
||||||
|
|
||||||
|
Run the test suite. It can be run with `cargo`, which is installed with Rust.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ cargo test
|
||||||
|
```
|
||||||
|
|
||||||
|
This will compile the `hello-world` crate and run the test, which fails.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
running 1 test
|
||||||
|
test hello_world ... FAILED
|
||||||
|
|
||||||
|
failures:
|
||||||
|
|
||||||
|
---- hello_world stdout ----
|
||||||
|
thread 'hello_world' panicked at 'assertion failed: `(left == right)`
|
||||||
|
(left: `"Hello, World!"`, right: `"Goodbye, Mars!"`)', tests/hello-world.rs:5
|
||||||
|
|
||||||
|
failures:
|
||||||
|
hello_world
|
||||||
|
|
||||||
|
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
|
||||||
|
```
|
||||||
|
|
||||||
|
### Understanding Test Failures
|
||||||
|
|
||||||
|
The `hello_world` failure states that it is expecting the value,
|
||||||
|
`"Hello, World!"`, to be returned from `hello()`.
|
||||||
|
The left side of the assertion (at line 5) should be equal to the right side.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
---- hello_world stdout ----
|
||||||
|
thread 'hello_world' panicked at 'assertion failed: `(left == right)`
|
||||||
|
(left: `"Hello, World!"`, right: `"Goodbye, Mars!"`)', tests/hello-world.rs:5
|
||||||
|
```
|
||||||
|
|
||||||
|
### Fixing the Error
|
||||||
|
|
||||||
|
To fix it, open up `src/lib.rs` and change the `hello` function to return
|
||||||
|
`"Hello, World!"` instead of `"Goodbye, Mars!"`.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
pub fn hello() -> &'static str {
|
||||||
|
"Hello, World!"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Step 2
|
||||||
|
|
||||||
|
Run the test again. This time, it will pass.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
running 0 tests
|
||||||
|
|
||||||
|
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
|
||||||
|
|
||||||
|
Running target/debug/deps/hello_world-bd1f06dc726ef14f
|
||||||
|
|
||||||
|
running 1 test
|
||||||
|
test hello_world ... ok
|
||||||
|
|
||||||
|
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
|
||||||
|
|
||||||
|
Doc-tests hello-world
|
||||||
|
|
||||||
|
running 0 tests
|
||||||
|
|
||||||
|
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
|
||||||
|
```
|
||||||
|
|
||||||
|
## Submit
|
||||||
|
|
||||||
|
Once the test is passing, you can submit your code with the following
|
||||||
|
command:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ exercism submit
|
||||||
|
```
|
86
rust/hello-world/HELP.md
Normal file
86
rust/hello-world/HELP.md
Normal 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
|
56
rust/hello-world/README.md
Normal file
56
rust/hello-world/README.md
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
# Hello World
|
||||||
|
|
||||||
|
Welcome to Hello World on Exercism's Rust Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
The classical introductory exercise.
|
||||||
|
Just say "Hello, World!".
|
||||||
|
|
||||||
|
["Hello, World!"][hello-world] is the traditional first program for beginning programming in a new language or environment.
|
||||||
|
|
||||||
|
The objectives are simple:
|
||||||
|
|
||||||
|
- Modify the provided code so that it produces the string "Hello, World!".
|
||||||
|
- Run the test suite and make sure that it succeeds.
|
||||||
|
- Submit your solution and check it at the website.
|
||||||
|
|
||||||
|
If everything goes well, you will be ready to fetch your first real exercise.
|
||||||
|
|
||||||
|
[hello-world]: https://en.wikipedia.org/wiki/%22Hello,_world!%22_program
|
||||||
|
|
||||||
|
In the Rust track, tests are run using the command `cargo test`.
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @EduardoBautista
|
||||||
|
|
||||||
|
### Contributed to by
|
||||||
|
|
||||||
|
- @ashleygwilliams
|
||||||
|
- @ClashTheBunny
|
||||||
|
- @coriolinus
|
||||||
|
- @cwhakes
|
||||||
|
- @dvoytik
|
||||||
|
- @EduardoBautista
|
||||||
|
- @efx
|
||||||
|
- @ErikSchierboom
|
||||||
|
- @hydhknn
|
||||||
|
- @IanWhitney
|
||||||
|
- @ijanos
|
||||||
|
- @kytrinyx
|
||||||
|
- @lutostag
|
||||||
|
- @nfiles
|
||||||
|
- @petertseng
|
||||||
|
- @regnerjr
|
||||||
|
- @rofrol
|
||||||
|
- @stringparser
|
||||||
|
- @xakon
|
||||||
|
- @ZapAnton
|
||||||
|
|
||||||
|
### Based on
|
||||||
|
|
||||||
|
This is an exercise to introduce users to using Exercism - https://en.wikipedia.org/wiki/%22Hello,_world!%22_program
|
4
rust/hello-world/src/lib.rs
Normal file
4
rust/hello-world/src/lib.rs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
// &'static is a "lifetime specifier", something you'll learn more about later
|
||||||
|
pub fn hello() -> &'static str {
|
||||||
|
"Hello, World!"
|
||||||
|
}
|
4
rust/hello-world/tests/hello-world.rs
Normal file
4
rust/hello-world/tests/hello-world.rs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
#[test]
|
||||||
|
fn hello_world() {
|
||||||
|
assert_eq!("Hello, World!", hello_world::hello());
|
||||||
|
}
|
38
rust/reverse-string/.exercism/config.json
Normal file
38
rust/reverse-string/.exercism/config.json
Normal 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"
|
||||||
|
}
|
1
rust/reverse-string/.exercism/metadata.json
Normal file
1
rust/reverse-string/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"track":"rust","exercise":"reverse-string","id":"aeec5dd024f94c60b89e66907f4e90b9","url":"https://exercism.org/tracks/rust/exercises/reverse-string","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
8
rust/reverse-string/.gitignore
vendored
Normal file
8
rust/reverse-string/.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# Generated by Cargo
|
||||||
|
# will have compiled files and executables
|
||||||
|
/target/
|
||||||
|
**/*.rs.bk
|
||||||
|
|
||||||
|
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||||
|
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
|
||||||
|
Cargo.lock
|
9
rust/reverse-string/Cargo.toml
Normal file
9
rust/reverse-string/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
[dependencies]
|
||||||
|
|
||||||
|
[features]
|
||||||
|
grapheme = []
|
||||||
|
|
||||||
|
[package]
|
||||||
|
edition = "2021"
|
||||||
|
name = "reverse_string"
|
||||||
|
version = "1.2.0"
|
86
rust/reverse-string/HELP.md
Normal file
86
rust/reverse-string/HELP.md
Normal 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
|
65
rust/reverse-string/README.md
Normal file
65
rust/reverse-string/README.md
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
# 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.
|
||||||
|
|
||||||
|
[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
|
3
rust/reverse-string/src/lib.rs
Normal file
3
rust/reverse-string/src/lib.rs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
pub fn reverse(input: &str) -> String {
|
||||||
|
input.chars().rev().collect()
|
||||||
|
}
|
77
rust/reverse-string/tests/reverse-string.rs
Normal file
77
rust/reverse-string/tests/reverse-string.rs
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
use reverse_string::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn an_empty_string() {
|
||||||
|
let input = "";
|
||||||
|
let output = reverse(input);
|
||||||
|
let expected = "";
|
||||||
|
assert_eq!(output, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_word() {
|
||||||
|
let input = "robot";
|
||||||
|
let output = reverse(input);
|
||||||
|
let expected = "tobor";
|
||||||
|
assert_eq!(output, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_capitalized_word() {
|
||||||
|
let input = "Ramen";
|
||||||
|
let output = reverse(input);
|
||||||
|
let expected = "nemaR";
|
||||||
|
assert_eq!(output, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_sentence_with_punctuation() {
|
||||||
|
let input = "I'm hungry!";
|
||||||
|
let output = reverse(input);
|
||||||
|
let expected = "!yrgnuh m'I";
|
||||||
|
assert_eq!(output, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_palindrome() {
|
||||||
|
let input = "racecar";
|
||||||
|
let output = reverse(input);
|
||||||
|
let expected = "racecar";
|
||||||
|
assert_eq!(output, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn an_even_sized_word() {
|
||||||
|
let input = "drawer";
|
||||||
|
let output = reverse(input);
|
||||||
|
let expected = "reward";
|
||||||
|
assert_eq!(output, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
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);
|
||||||
|
}
|
19
zig/difference-of-squares/.exercism/config.json
Normal file
19
zig/difference-of-squares/.exercism/config.json
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"massivelivefun"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"difference_of_squares.zig"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test_difference_of_squares.zig"
|
||||||
|
],
|
||||||
|
"example": [
|
||||||
|
".meta/example.zig"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"blurb": "Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.",
|
||||||
|
"source": "Problem 6 at Project Euler",
|
||||||
|
"source_url": "https://projecteuler.net/problem=6"
|
||||||
|
}
|
1
zig/difference-of-squares/.exercism/metadata.json
Normal file
1
zig/difference-of-squares/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"track":"zig","exercise":"difference-of-squares","id":"05fde6d09bb749e38afa56a478926999","url":"https://exercism.org/tracks/zig/exercises/difference-of-squares","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
53
zig/difference-of-squares/HELP.md
Normal file
53
zig/difference-of-squares/HELP.md
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
# Help
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
Write your code in `<exercise_name>.zig`.
|
||||||
|
|
||||||
|
To run the tests for an exercise, run:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
zig test test_exercise_name.zig
|
||||||
|
```
|
||||||
|
|
||||||
|
in the exercise's root directory (replacing `exercise_name` with the name of the exercise).
|
||||||
|
|
||||||
|
## Submitting your solution
|
||||||
|
|
||||||
|
You can submit your solution using the `exercism submit difference_of_squares.zig` 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 [Zig track's documentation](https://exercism.org/docs/tracks/zig)
|
||||||
|
- The [Zig track's programming category on the forum](https://forum.exercism.org/c/programming/zig)
|
||||||
|
- [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.
|
||||||
|
|
||||||
|
- [The Zig Programming Language Documentation][documentation] is a great overview of all of the language features that Zig provides to those who use it.
|
||||||
|
- [Zig Guide][zig-guide] is an excellent primer that explains the language features that Zig has to offer.
|
||||||
|
- [Ziglings][ziglings] is highly recommended.
|
||||||
|
Learn Zig by fixing tiny broken programs.
|
||||||
|
- [The Zig Programming Language Discord][discord-zig] is the main [Discord][discord].
|
||||||
|
It provides a great way to get in touch with the Zig community at large, and get some quick, direct help for any Zig related problem.
|
||||||
|
- [#zig][irc] on irc.freenode.net is the main Zig IRC channel.
|
||||||
|
- [/r/Zig][reddit] is the main Zig subreddit.
|
||||||
|
- [Stack Overflow][stack-overflow] can be used to discover code snippets and solutions to problems that may have already asked and maybe solved by others.
|
||||||
|
|
||||||
|
[discord]: https://discordapp.com
|
||||||
|
[discord-zig]: https://discord.com/invite/gxsFFjE
|
||||||
|
[documentation]: https://ziglang.org/documentation/master
|
||||||
|
[irc]: https://webchat.freenode.net/?channels=%23zig
|
||||||
|
[reddit]: https://www.reddit.com/r/Zig
|
||||||
|
[stack-overflow]: https://stackoverflow.com/questions/tagged/zig
|
||||||
|
[zig-guide]: https://zig.guide/
|
||||||
|
[ziglings]: https://codeberg.org/ziglings/exercises
|
29
zig/difference-of-squares/README.md
Normal file
29
zig/difference-of-squares/README.md
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
# Difference of Squares
|
||||||
|
|
||||||
|
Welcome to Difference of Squares on Exercism's Zig Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.
|
||||||
|
|
||||||
|
The square of the sum of the first ten natural numbers is
|
||||||
|
(1 + 2 + ... + 10)² = 55² = 3025.
|
||||||
|
|
||||||
|
The sum of the squares of the first ten natural numbers is
|
||||||
|
1² + 2² + ... + 10² = 385.
|
||||||
|
|
||||||
|
Hence the difference between the square of the sum of the first ten natural numbers and the sum of the squares of the first ten natural numbers is 3025 - 385 = 2640.
|
||||||
|
|
||||||
|
You are not expected to discover an efficient solution to this yourself from first principles; research is allowed, indeed, encouraged.
|
||||||
|
Finding the best algorithm for the problem is a key skill in software engineering.
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @massivelivefun
|
||||||
|
|
||||||
|
### Based on
|
||||||
|
|
||||||
|
Problem 6 at Project Euler - https://projecteuler.net/problem=6
|
19
zig/difference-of-squares/difference_of_squares.zig
Normal file
19
zig/difference-of-squares/difference_of_squares.zig
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
pub fn squareOfSum(number: usize) usize {
|
||||||
|
var sum: usize = 0;
|
||||||
|
for (0..number + 1) |i| {
|
||||||
|
sum += i;
|
||||||
|
}
|
||||||
|
return sum * sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sumOfSquares(number: usize) usize {
|
||||||
|
var sum: usize = 0;
|
||||||
|
for (1..number + 1) |i| {
|
||||||
|
sum += i * i;
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn differenceOfSquares(number: usize) usize {
|
||||||
|
return squareOfSum(number) - sumOfSquares(number);
|
||||||
|
}
|
58
zig/difference-of-squares/test_difference_of_squares.zig
Normal file
58
zig/difference-of-squares/test_difference_of_squares.zig
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const testing = std.testing;
|
||||||
|
|
||||||
|
const difference_of_squares = @import("difference_of_squares.zig");
|
||||||
|
|
||||||
|
test "square of sum up to 1" {
|
||||||
|
const expected: usize = 1;
|
||||||
|
const actual = difference_of_squares.squareOfSum(1);
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "square of sum up to 5" {
|
||||||
|
const expected: usize = 225;
|
||||||
|
const actual = difference_of_squares.squareOfSum(5);
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "square of sum up to 100" {
|
||||||
|
const expected: usize = 25_502_500;
|
||||||
|
const actual = difference_of_squares.squareOfSum(100);
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "sum of squares up to 1" {
|
||||||
|
const expected: usize = 1;
|
||||||
|
const actual = difference_of_squares.sumOfSquares(1);
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "sum of squares up to 5" {
|
||||||
|
const expected: usize = 55;
|
||||||
|
const actual = difference_of_squares.sumOfSquares(5);
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "sum of squares up to 100" {
|
||||||
|
const expected: usize = 338_350;
|
||||||
|
const actual = difference_of_squares.sumOfSquares(100);
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "difference of squares up to 1" {
|
||||||
|
const expected: usize = 0;
|
||||||
|
const actual = difference_of_squares.differenceOfSquares(1);
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "difference of squares up to 5" {
|
||||||
|
const expected: usize = 170;
|
||||||
|
const actual = difference_of_squares.differenceOfSquares(5);
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "difference of squares up to 100" {
|
||||||
|
const expected: usize = 25_164_150;
|
||||||
|
const actual = difference_of_squares.differenceOfSquares(100);
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
19
zig/leap/.exercism/config.json
Normal file
19
zig/leap/.exercism/config.json
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"massivelivefun"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"leap.zig"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test_leap.zig"
|
||||||
|
],
|
||||||
|
"example": [
|
||||||
|
".meta/example.zig"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"blurb": "Given a year, report if it is a leap year.",
|
||||||
|
"source": "CodeRanch Cattle Drive, Assignment 3",
|
||||||
|
"source_url": "https://coderanch.com/t/718816/Leap"
|
||||||
|
}
|
1
zig/leap/.exercism/metadata.json
Normal file
1
zig/leap/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"track":"zig","exercise":"leap","id":"c311a440c14b4ef084adf8482af46d12","url":"https://exercism.org/tracks/zig/exercises/leap","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
53
zig/leap/HELP.md
Normal file
53
zig/leap/HELP.md
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
# Help
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
Write your code in `<exercise_name>.zig`.
|
||||||
|
|
||||||
|
To run the tests for an exercise, run:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
zig test test_exercise_name.zig
|
||||||
|
```
|
||||||
|
|
||||||
|
in the exercise's root directory (replacing `exercise_name` with the name of the exercise).
|
||||||
|
|
||||||
|
## Submitting your solution
|
||||||
|
|
||||||
|
You can submit your solution using the `exercism submit leap.zig` 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 [Zig track's documentation](https://exercism.org/docs/tracks/zig)
|
||||||
|
- The [Zig track's programming category on the forum](https://forum.exercism.org/c/programming/zig)
|
||||||
|
- [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.
|
||||||
|
|
||||||
|
- [The Zig Programming Language Documentation][documentation] is a great overview of all of the language features that Zig provides to those who use it.
|
||||||
|
- [Zig Learn][zig-learn] is an excellent primer that explains the language features that Zig has to offer.
|
||||||
|
- [Ziglings][ziglings] is highly recommended.
|
||||||
|
Learn Zig by fixing tiny broken programs.
|
||||||
|
- [The Zig Programming Language Discord][discord-zig] is the main [Discord][discord].
|
||||||
|
It provides a great way to get in touch with the Zig community at large, and get some quick, direct help for any Zig related problem.
|
||||||
|
- [#zig][irc] on irc.freenode.net is the main Zig IRC channel.
|
||||||
|
- [/r/Zig][reddit] is the main Zig subreddit.
|
||||||
|
- [Stack Overflow][stack-overflow] can be used to discover code snippets and solutions to problems that may have already asked and maybe solved by others.
|
||||||
|
|
||||||
|
[discord]: https://discordapp.com
|
||||||
|
[discord-zig]: https://discord.com/invite/gxsFFjE
|
||||||
|
[documentation]: https://ziglang.org/documentation/master
|
||||||
|
[irc]: https://webchat.freenode.net/?channels=%23zig
|
||||||
|
[reddit]: https://www.reddit.com/r/Zig
|
||||||
|
[stack-overflow]: https://stackoverflow.com/questions/tagged/zig
|
||||||
|
[zig-learn]: https://ziglearn.org/
|
||||||
|
[ziglings]: https://github.com/ratfactor/ziglings
|
37
zig/leap/README.md
Normal file
37
zig/leap/README.md
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
# Leap
|
||||||
|
|
||||||
|
Welcome to Leap on Exercism's Zig Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
Given a year, report if it is a leap year.
|
||||||
|
|
||||||
|
The tricky thing here is that a leap year in the Gregorian calendar occurs:
|
||||||
|
|
||||||
|
```text
|
||||||
|
on every year that is evenly divisible by 4
|
||||||
|
except every year that is evenly divisible by 100
|
||||||
|
unless the year is also evenly divisible by 400
|
||||||
|
```
|
||||||
|
|
||||||
|
For example, 1997 is not a leap year, but 1996 is.
|
||||||
|
1900 is not a leap year, but 2000 is.
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
Though our exercise adopts some very simple rules, there is more to learn!
|
||||||
|
|
||||||
|
For a delightful, four minute explanation of the whole leap year phenomenon, go watch [this youtube video][video].
|
||||||
|
|
||||||
|
[video]: https://www.youtube.com/watch?v=xX96xng7sAE
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @massivelivefun
|
||||||
|
|
||||||
|
### Based on
|
||||||
|
|
||||||
|
CodeRanch Cattle Drive, Assignment 3 - https://coderanch.com/t/718816/Leap
|
12
zig/leap/leap.zig
Normal file
12
zig/leap/leap.zig
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
pub fn isLeapYear(year: u32) bool {
|
||||||
|
if (year % 4 == 0) {
|
||||||
|
if (year % 100 == 0) {
|
||||||
|
if (year % 400 == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
40
zig/leap/test_leap.zig
Normal file
40
zig/leap/test_leap.zig
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const testing = std.testing;
|
||||||
|
|
||||||
|
const leap = @import("leap.zig");
|
||||||
|
|
||||||
|
test "year not divisible by 4 in common year" {
|
||||||
|
try testing.expect(!leap.isLeapYear(2015));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "year divisible by 2, not divisible by 4 in common year" {
|
||||||
|
try testing.expect(!leap.isLeapYear(1970));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "year divisible by 4, not divisible by 100 in leap year" {
|
||||||
|
try testing.expect(leap.isLeapYear(1996));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "year divisible by 4 and 5 is still a leap year" {
|
||||||
|
try testing.expect(leap.isLeapYear(1960));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "year divisible by 100, not divisible by 400 in common year" {
|
||||||
|
try testing.expect(!leap.isLeapYear(2100));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "year divisible by 100 but not by 3 is still not a leap year" {
|
||||||
|
try testing.expect(!leap.isLeapYear(1900));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "year divisible by 400 is leap year" {
|
||||||
|
try testing.expect(leap.isLeapYear(2000));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "year divisible by 400 but not by 125 is still a leap year" {
|
||||||
|
try testing.expect(leap.isLeapYear(2400));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "year divisible by 200, not divisible by 400 in common year" {
|
||||||
|
try testing.expect(!leap.isLeapYear(1800));
|
||||||
|
}
|
Loading…
Reference in a new issue