Compare commits

..

3 commits

15 changed files with 255 additions and 61 deletions

View file

@ -1 +1 @@
{"track":"rust","exercise":"gigasecond","id":"46cf4dcc2c1d42f48213bc4054d72a04","url":"https://exercism.org/tracks/rust/exercises/gigasecond","handle":"Chomp1295","is_requester":true,"auto_approve":false} {"track":"rust","exercise":"gigasecond","id":"20e9d27ceb734a788023c5f555e0d88e","url":"https://exercism.org/tracks/rust/exercises/gigasecond","handle":"Chomp1295","is_requester":true,"auto_approve":false}

View file

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

View file

@ -1,48 +1,59 @@
use time::PrimitiveDateTime as DateTime; #[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);
}
/// Create a datetime from the given numeric point in time. #[test]
/// #[ignore]
/// Panics if any field is invalid. fn second_test_for_date_only_specification_of_time() {
fn dt(year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8) -> DateTime { let start = datetime(1977, 6, 13, 0, 0, 0);
use time::{Date, Time}; let actual = gigasecond::after(start);
let expected = datetime(2009, 2, 19, 1, 46, 40);
assert_eq!(actual, expected);
}
DateTime::new( #[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(), Date::from_calendar_date(year, month.try_into().unwrap(), day).unwrap(),
Time::from_hms(hour, minute, second).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));
}

View file

@ -1 +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} {"track":"rust","exercise":"hello-world","id":"bf8f90455cd64bda84afecac949ecd0d","url":"https://exercism.org/tracks/rust/exercises/hello-world","handle":"Chomp1295","is_requester":true,"auto_approve":false}

View file

@ -1 +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} {"track":"rust","exercise":"reverse-string","id":"f97879b6def647a5bf82751034c483e3","url":"https://exercism.org/tracks/rust/exercises/reverse-string","handle":"Chomp1295","is_requester":true,"auto_approve":false}

View file

@ -1,9 +1,10 @@
[dependencies]
[features]
grapheme = []
[package] [package]
edition = "2021" edition = "2021"
name = "reverse_string" name = "reverse_string"
version = "1.2.0" version = "1.2.0"
[features]
grapheme = []
[dependencies]
unicode-segmentation = "=1.11.0"

View file

@ -21,17 +21,20 @@ Some examples:
## Bonus ## Bonus
Test your function on this string: `uüu` and see what happens. Try to write a function that properly Test your function on this string: `uüu` and see what happens.
reverses this string. Hint: grapheme clusters 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 To get the bonus test to run, remove the ignore flag (`#[ignore]`) from the last test, and execute the tests with:
last test, and execute the tests with:
```bash ```bash
$ cargo test --features grapheme 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. 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. [Check the documentation](https://doc.rust-lang.org/cargo/guide/dependencies.html) for instructions on how to use external crates in your projects.

View file

@ -1,3 +1,5 @@
use unicode_segmentation::UnicodeSegmentation;
pub fn reverse(input: &str) -> String { pub fn reverse(input: &str) -> String {
input.chars().rev().collect() input.graphemes(true).rev().collect()
} }

View file

@ -9,6 +9,7 @@ fn an_empty_string() {
} }
#[test] #[test]
fn a_word() { fn a_word() {
let input = "robot"; let input = "robot";
let output = reverse(input); let output = reverse(input);
@ -17,6 +18,7 @@ fn a_word() {
} }
#[test] #[test]
fn a_capitalized_word() { fn a_capitalized_word() {
let input = "Ramen"; let input = "Ramen";
let output = reverse(input); let output = reverse(input);
@ -25,6 +27,7 @@ fn a_capitalized_word() {
} }
#[test] #[test]
fn a_sentence_with_punctuation() { fn a_sentence_with_punctuation() {
let input = "I'm hungry!"; let input = "I'm hungry!";
let output = reverse(input); let output = reverse(input);
@ -33,6 +36,7 @@ fn a_sentence_with_punctuation() {
} }
#[test] #[test]
fn a_palindrome() { fn a_palindrome() {
let input = "racecar"; let input = "racecar";
let output = reverse(input); let output = reverse(input);
@ -41,6 +45,7 @@ fn a_palindrome() {
} }
#[test] #[test]
fn an_even_sized_word() { fn an_even_sized_word() {
let input = "drawer"; let input = "drawer";
let output = reverse(input); let output = reverse(input);
@ -49,6 +54,7 @@ fn an_even_sized_word() {
} }
#[test] #[test]
fn wide_characters() { fn wide_characters() {
let input = "子猫"; let input = "子猫";
let output = reverse(input); let output = reverse(input);
@ -57,7 +63,6 @@ fn wide_characters() {
} }
#[test] #[test]
#[ignore]
#[cfg(feature = "grapheme")] #[cfg(feature = "grapheme")]
fn grapheme_cluster_with_pre_combined_form() { fn grapheme_cluster_with_pre_combined_form() {
let input = "Würstchenstand"; let input = "Würstchenstand";
@ -67,7 +72,6 @@ fn grapheme_cluster_with_pre_combined_form() {
} }
#[test] #[test]
#[ignore]
#[cfg(feature = "grapheme")] #[cfg(feature = "grapheme")]
fn grapheme_clusters() { fn grapheme_clusters() {
let input = "ผู้เขียนโปรแกรม"; let input = "ผู้เขียนโปรแกรม";

View file

@ -0,0 +1,19 @@
{
"authors": [
"massivelivefun"
],
"files": {
"solution": [
"resistor_color_duo.zig"
],
"test": [
"test_resistor_color_duo.zig"
],
"example": [
".meta/example.zig"
]
},
"blurb": "Convert color codes, as used on resistors, to a numeric value.",
"source": "Maud de Vries, Erik Schierboom",
"source_url": "https://github.com/exercism/problem-specifications/issues/1464"
}

View file

@ -0,0 +1 @@
{"track":"zig","exercise":"resistor-color-duo","id":"4904409bf2c744acb829962709a345ef","url":"https://exercism.org/tracks/zig/exercises/resistor-color-duo","handle":"Chomp1295","is_requester":true,"auto_approve":false}

View 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 resistor_color_duo.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

View file

@ -0,0 +1,48 @@
# Resistor Color Duo
Welcome to Resistor Color Duo on Exercism's Zig Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
For this exercise, you need to know two things about them:
- Each resistor has a resistance value.
- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.
To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
Each band has a position and a numeric value.
The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.
For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.
In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands.
The program will take color names as input and output a two digit number, even if the input is more than two colors!
The band colors are encoded as follows:
- black: 0
- brown: 1
- red: 2
- orange: 3
- yellow: 4
- green: 5
- blue: 6
- violet: 7
- grey: 8
- white: 9
From the example above:
brown-green should return 15, and
brown-green-violet should return 15 too, ignoring the third color.
## Source
### Created by
- @massivelivefun
### Based on
Maud de Vries, Erik Schierboom - https://github.com/exercism/problem-specifications/issues/1464

View file

@ -0,0 +1,5 @@
pub const ColorBand = enum(usize) { black = 0, brown = 1, red = 2, orange = 3, yellow = 4, green = 5, blue = 6, violet = 7, grey = 8, white = 9 };
pub fn colorCode(comptime colors: [2]ColorBand) usize {
return 10 * @intFromEnum(colors[0]) + @intFromEnum(colors[1]);
}

View file

@ -0,0 +1,47 @@
const std = @import("std");
const testing = std.testing;
const resistor_color_duo = @import("resistor_color_duo.zig");
const ColorBand = resistor_color_duo.ColorBand;
test "brown and black" {
const array = [_]ColorBand{ .brown, .black };
const expected: usize = 10;
const actual = resistor_color_duo.colorCode(array);
try testing.expectEqual(expected, actual);
}
test "blue and grey" {
const array = [_]ColorBand{ .blue, .grey };
const expected: usize = 68;
const actual = resistor_color_duo.colorCode(array);
try testing.expectEqual(expected, actual);
}
test "yellow and violet" {
const array = [_]ColorBand{ .yellow, .violet };
const expected: usize = 47;
const actual = resistor_color_duo.colorCode(array);
try testing.expectEqual(expected, actual);
}
test "white and red" {
const array = [_]ColorBand{ .white, .red };
const expected: usize = 92;
const actual = resistor_color_duo.colorCode(array);
try testing.expectEqual(expected, actual);
}
test "orange and orange" {
const array = [_]ColorBand{ .orange, .orange };
const expected: usize = 33;
const actual = resistor_color_duo.colorCode(array);
try testing.expectEqual(expected, actual);
}
test "black and brown, one-digit" {
const array = [_]ColorBand{ .black, .brown };
const expected: usize = 1;
const actual = resistor_color_duo.colorCode(array);
try testing.expectEqual(expected, actual);
}