Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
"blurb": "Rust is a compiled programming language designed for speed, concurrency, and memory safety. Rust programs can run almost anywhere, from low-power embedded devices to web servers.",
"foregone": [
"binary",
"hexadecimal",
"nucleotide-codons",
"octal",
"trinary",
"two-fer"
"trinary"
],
"exercises": [
{
Expand Down Expand Up @@ -1069,6 +1066,36 @@
"topics": [
"parser_reimplementation"
]
},
{
"slug": "two-fer",
"uuid": "c6631a2c-4632-11e8-842f-0ed5f89f718b",
"core": false,
"unlocked_by": null,
"difficulty": 1,
"topics": [
"match",
"strings"
],
"deprecated": true
},
{
"slug": "nucleotide-codons",
"uuid": "8dae8f4d-368d-477d-907e-bf746921bfbf",
"core": false,
"unlocked_by": null,
"difficulty": 0,
"topics": null,
"deprecated": true
},
{
"slug": "hexadecimal",
"uuid": "496fd79f-1678-4aa2-8110-c32c6aaf545e",
"core": false,
"unlocked_by": null,
"difficulty": 0,
"topics": null,
"deprecated": true
}
]
}
8 changes: 8 additions & 0 deletions exercises/hexadecimal/.gitignore
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions exercises/hexadecimal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "hexadecimal"
version = "0.0.0"
88 changes: 88 additions & 0 deletions exercises/hexadecimal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Hexadecimal

Convert a hexadecimal number, represented as a string (e.g. "10af8c"), to its decimal equivalent using first principles (i.e. no, you may not use built-in or external libraries to accomplish the conversion).

On the web we use hexadecimal to represent colors, e.g. green: 008000,
teal: 008080, navy: 000080).

The program should handle invalid hexadecimal strings.

## Rust Installation

Refer to the [exercism help page][help-page] for Rust installation and learning
resources.

## Writing the Code

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 all tests without editing the tests source file, use:

```bash
$ cargo test -- --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]

Make sure to read the [Modules](https://doc.rust-lang.org/book/ch07-02-modules-and-use-to-control-scope-and-privacy.html) chapter if you
haven't already, it will help you with organizing your files.

## Further improvements

After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution.

To format your solution, inside the solution directory use

```bash
cargo fmt
```

To see, if your solution contains some common ineffective use cases, inside the solution directory use

```bash
cargo clippy --all-targets
```

## 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 [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!

If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md).

[help-page]: https://exercism.io/tracks/rust/learning
[modules]: https://doc.rust-lang.org/book/ch07-02-modules-and-use-to-control-scope-and-privacy.html
[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html

## Source

All of Computer Science [http://www.wolframalpha.com/examples/NumberBases.html](http://www.wolframalpha.com/examples/NumberBases.html)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
33 changes: 33 additions & 0 deletions exercises/hexadecimal/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
fn parse_hex_digit(c: char) -> Option<i64> {
match c {
'0' => Some(0),
'1' => Some(1),
'2' => Some(2),
'3' => Some(3),
'4' => Some(4),
'5' => Some(5),
'6' => Some(6),
'7' => Some(7),
'8' => Some(8),
'9' => Some(9),
'a' => Some(10),
'b' => Some(11),
'c' => Some(12),
'd' => Some(13),
'e' => Some(14),
'f' => Some(15),
_ => None,
}
}

pub fn hex_to_int(string: &str) -> Option<i64> {
let base: i64 = 16;

string
.chars()
.rev()
.enumerate()
.fold(Some(0), |acc, (pos, c)| {
parse_hex_digit(c).and_then(|n| acc.map(|acc| acc + n * base.pow(pos as u32)))
})
}
33 changes: 33 additions & 0 deletions exercises/hexadecimal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
fn parse_hex_digit(c: char) -> Option<i64> {
match c {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

although it does not hurt to have it since it's deprecated, this lib.rs doesn't necessarily need to have a complete implementation (and we would want it not to have one if it were non-deprecated)

'0' => Some(0),
'1' => Some(1),
'2' => Some(2),
'3' => Some(3),
'4' => Some(4),
'5' => Some(5),
'6' => Some(6),
'7' => Some(7),
'8' => Some(8),
'9' => Some(9),
'a' => Some(10),
'b' => Some(11),
'c' => Some(12),
'd' => Some(13),
'e' => Some(14),
'f' => Some(15),
_ => None,
}
}

pub fn hex_to_int(string: &str) -> Option<i64> {
let base: i64 = 16;

string
.chars()
.rev()
.enumerate()
.fold(Some(0), |acc, (pos, c)| {
parse_hex_digit(c).and_then(|n| acc.map(|acc| acc + n * base.pow(pos as u32)))
})
}
60 changes: 60 additions & 0 deletions exercises/hexadecimal/tests/hexadecimal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
extern crate hexadecimal;

#[test]
fn test_hex_1_is_decimal_1() {
assert_eq!(Some(1), hexadecimal::hex_to_int("1"));
}

#[test]
#[ignore]
fn test_hex_c_is_decimal_12() {
assert_eq!(Some(12), hexadecimal::hex_to_int("c"));
}

#[test]
#[ignore]
fn test_hex_10_is_decimal_16() {
assert_eq!(Some(16), hexadecimal::hex_to_int("10"));
}

#[test]
#[ignore]
fn test_hex_af_is_decimal_175() {
assert_eq!(Some(175), hexadecimal::hex_to_int("af"));
}

#[test]
#[ignore]
fn test_hex_100_is_decimal_256() {
assert_eq!(Some(256), hexadecimal::hex_to_int("100"));
}

#[test]
#[ignore]
fn test_hex_19ace_is_decimal_105166() {
assert_eq!(Some(105166), hexadecimal::hex_to_int("19ace"));
}

#[test]
#[ignore]
fn test_invalid_hex_is_none() {
assert_eq!(None, hexadecimal::hex_to_int("carrot"));
}

#[test]
#[ignore]
fn test_black() {
assert_eq!(Some(0), hexadecimal::hex_to_int("0000000"));
}

#[test]
#[ignore]
fn test_white() {
assert_eq!(Some(16777215), hexadecimal::hex_to_int("ffffff"));
}

#[test]
#[ignore]
fn test_yellow() {
assert_eq!(Some(16776960), hexadecimal::hex_to_int("ffff00"));
}
8 changes: 8 additions & 0 deletions exercises/nucleotide-codons/.gitignore
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions exercises/nucleotide-codons/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "nucleotide_codons"
version = "0.1.0"
95 changes: 95 additions & 0 deletions exercises/nucleotide-codons/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Nucleotide Codons

Write a function that returns the name of an amino acid a particular codon,
possibly using shorthand, encodes for.

In DNA sequences of 3 nucleotides, called codons, encode for amino acids. Often
several codons encode for the same amino acid. The International Union of Pure
and Applied Chemistry developed a shorthand system for designating groups of
codons that encode for the same amino acid.

Simply put they've expanded the four letters A, C, G and T with a bunch of
letters that stand for different possibilities. For example R means A and G.
So TAR stands for TAA and TAG (think of "TAR" as "TA[AG]" in regex notation).

Write some code that given a codon, which may use shorthand, returns the
name of the amino acid that that codon encodes for. You will be given
a list of non-shorthand-codon/name pairs to base your computation on.

See: [wikipedia](https://en.wikipedia.org/wiki/DNA_codon_table).

## Rust Installation

Refer to the [exercism help page][help-page] for Rust installation and learning
resources.

## Writing the Code

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 all tests without editing the tests source file, use:

```bash
$ cargo test -- --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]

Make sure to read the [Modules](https://doc.rust-lang.org/book/ch07-02-modules-and-use-to-control-scope-and-privacy.html) chapter if you
haven't already, it will help you with organizing your files.

## Further improvements

After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution.

To format your solution, inside the solution directory use

```bash
cargo fmt
```

To see, if your solution contains some common ineffective use cases, inside the solution directory use

```bash
cargo clippy --all-targets
```

## 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 [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!

If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md).

[help-page]: https://exercism.io/tracks/rust/learning
[modules]: https://doc.rust-lang.org/book/ch07-02-modules-and-use-to-control-scope-and-privacy.html
[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
Loading