Skip to content

Commit 1f86872

Browse files
committed
solve eggs
1 parent b20f17c commit 1f86872

File tree

9 files changed

+239
-0
lines changed

9 files changed

+239
-0
lines changed

rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ members = [
1212
"collatz-conjecture",
1313
"diffie-hellman",
1414
"difference-of-squares",
15+
"eliuds-eggs",
1516
"gigasecond",
1617
"grade-school",
1718
"grains",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"authors": ["senekor"],
3+
"files": {
4+
"solution": [
5+
"src/lib.rs",
6+
"Cargo.toml"
7+
],
8+
"test": [
9+
"tests/eliuds-eggs.rs"
10+
],
11+
"example": [
12+
".meta/example.rs"
13+
]
14+
},
15+
"blurb": "Help Eliud count the number of eggs in her chicken coop by counting the number of 1 bits in a binary representation.",
16+
"source": "Christian Willner, Eric Willigers",
17+
"source_url": "https://forum.exercism.org/t/new-exercise-suggestion-pop-count/7632/5"
18+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"track":"rust","exercise":"eliuds-eggs","id":"8d2ea1113ec148b88c80a49b91373f2f","url":"https://exercism.org/tracks/rust/exercises/eliuds-eggs","handle":"rootulp","is_requester":true,"auto_approve":false}

rust/eliuds-eggs/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
/Cargo.lock

rust/eliuds-eggs/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
edition = "2021"
3+
name = "eliuds_eggs"
4+
version = "1.0.0"

rust/eliuds-eggs/HELP.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Help
2+
3+
## Running the tests
4+
5+
Execute the tests with:
6+
7+
```bash
8+
$ cargo test
9+
```
10+
11+
All but the first test have been ignored. After you get the first test to
12+
pass, open the tests source file which is located in the `tests` directory
13+
and remove the `#[ignore]` flag from the next test and get the tests to pass
14+
again. Each separate test is a function with `#[test]` flag above it.
15+
Continue, until you pass every test.
16+
17+
If you wish to run _only ignored_ tests without editing the tests source file, use:
18+
19+
```bash
20+
$ cargo test -- --ignored
21+
```
22+
23+
If you are using Rust 1.51 or later, you can run _all_ tests with
24+
25+
```bash
26+
$ cargo test -- --include-ignored
27+
```
28+
29+
To run a specific test, for example `some_test`, you can use:
30+
31+
```bash
32+
$ cargo test some_test
33+
```
34+
35+
If the specific test is ignored, use:
36+
37+
```bash
38+
$ cargo test some_test -- --ignored
39+
```
40+
41+
To learn more about Rust tests refer to the online [test documentation][rust-tests].
42+
43+
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
44+
45+
## Submitting your solution
46+
47+
You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command.
48+
This command will upload your solution to the Exercism website and print the solution page's URL.
49+
50+
It's possible to submit an incomplete solution which allows you to:
51+
52+
- See how others have completed the exercise
53+
- Request help from a mentor
54+
55+
## Need to get help?
56+
57+
If you'd like help solving the exercise, check the following pages:
58+
59+
- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
60+
- The [Rust track's programming category on the forum](https://forum.exercism.org/c/programming/rust)
61+
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
62+
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
63+
64+
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
65+
66+
## Rust Installation
67+
68+
Refer to the [exercism help page][help-page] for Rust installation and learning
69+
resources.
70+
71+
## Submitting the solution
72+
73+
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.
74+
75+
## Feedback, Issues, Pull Requests
76+
77+
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!
78+
79+
If you want to know more about Exercism, take a look at the [contribution guide].
80+
81+
## Submitting Incomplete Solutions
82+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
83+
84+
[help-page]: https://exercism.org/tracks/rust/learning
85+
[github]: https://github.com/exercism/rust
86+
[contribution guide]: https://exercism.org/docs/community/contributors

rust/eliuds-eggs/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Eliud's Eggs
2+
3+
Welcome to Eliud's Eggs on Exercism's Rust Track.
4+
If you need help running the tests or submitting your code, check out `HELP.md`.
5+
6+
## Introduction
7+
8+
Your friend Eliud inherited a farm from her grandma Tigist.
9+
Her granny was an inventor and had a tendency to build things in an overly complicated manner.
10+
The chicken coop has a digital display showing an encoded number representing the positions of all eggs that could be picked up.
11+
12+
Eliud is asking you to write a program that shows the actual number of eggs in the coop.
13+
14+
The position information encoding is calculated as follows:
15+
16+
1. Scan the potential egg-laying spots and mark down a `1` for an existing egg or a `0` for an empty spot.
17+
2. Convert the number from binary to decimal.
18+
3. Show the result on the display.
19+
20+
Example 1:
21+
22+
```text
23+
Chicken Coop:
24+
_ _ _ _ _ _ _
25+
|E| |E|E| | |E|
26+
27+
Resulting Binary:
28+
1 0 1 1 0 0 1
29+
30+
Decimal number on the display:
31+
89
32+
33+
Actual eggs in the coop:
34+
4
35+
```
36+
37+
Example 2:
38+
39+
```text
40+
Chicken Coop:
41+
_ _ _ _ _ _ _ _
42+
| | | |E| | | | |
43+
44+
Resulting Binary:
45+
0 0 0 1 0 0 0 0
46+
47+
Decimal number on the display:
48+
16
49+
50+
Actual eggs in the coop:
51+
1
52+
```
53+
54+
## Instructions
55+
56+
Your task is to count the number of 1 bits in the binary representation of a number.
57+
58+
## Restrictions
59+
60+
Keep your hands off that bit-count functionality provided by your standard library!
61+
Solve this one yourself using other basic tools instead.
62+
63+
## Source
64+
65+
### Created by
66+
67+
- @senekor
68+
69+
### Based on
70+
71+
Christian Willner, Eric Willigers - https://forum.exercism.org/t/new-exercise-suggestion-pop-count/7632/5

rust/eliuds-eggs/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pub fn egg_count(decimal: u32) -> usize {
2+
let binary = decimal_to_binary(decimal);
3+
let eggs = count('1', binary);
4+
eggs
5+
}
6+
7+
fn decimal_to_binary(decimal: u32) -> String {
8+
let mut result = "".to_string();
9+
10+
let mut n = decimal;
11+
while n != 0 {
12+
let remainder = n % 2;
13+
n = n / 2;
14+
let c = char::from_digit(remainder, 10).unwrap();
15+
result.insert(0, c);
16+
}
17+
18+
result
19+
}
20+
21+
fn count(c: char, string: String) -> usize {
22+
string.chars().filter(|char| *char == c).count()
23+
}

rust/eliuds-eggs/tests/eliuds-eggs.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use eliuds_eggs::*;
2+
3+
#[test]
4+
fn test_0_eggs() {
5+
let input = 0;
6+
let output = egg_count(input);
7+
let expected = 0;
8+
assert_eq!(output, expected);
9+
}
10+
11+
#[test]
12+
fn test_1_egg() {
13+
let input = 16;
14+
let output = egg_count(input);
15+
let expected = 1;
16+
assert_eq!(output, expected);
17+
}
18+
19+
#[test]
20+
fn test_4_eggs() {
21+
let input = 89;
22+
let output = egg_count(input);
23+
let expected = 4;
24+
assert_eq!(output, expected);
25+
}
26+
27+
#[test]
28+
fn test_13_eggs() {
29+
let input = 2_000_000_000;
30+
let output = egg_count(input);
31+
let expected = 13;
32+
assert_eq!(output, expected);
33+
}

0 commit comments

Comments
 (0)