Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust allergies #247

Merged
merged 3 commits into from
Sep 16, 2023
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
1 change: 1 addition & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@
- [armstrong-numbers](./armstrong-numbers/README.md)
- [luhn](./luhn/README.md)
- [triangle](./triangle/README.md)
- [allergies](./allergies/README.md)
40 changes: 40 additions & 0 deletions rust/allergies/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"authors": [
"EduardoBautista"
],
"contributors": [
"ashleygwilliams",
"coriolinus",
"cwhakes",
"EduardoBautista",
"efx",
"ErikSchierboom",
"IanWhitney",
"kytrinyx",
"lutostag",
"mkantor",
"navossoc",
"nfiles",
"petertseng",
"rofrol",
"stringparser",
"taravancil",
"xakon",
"ZapAnton"
],
"files": {
"solution": [
"src/lib.rs",
"Cargo.toml"
],
"test": [
"tests/allergies.rs"
],
"example": [
".meta/example.rs"
]
},
"blurb": "Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies.",
"source": "Jumpstart Lab Warm-up",
"source_url": "http://jumpstartlab.com"
}
1 change: 1 addition & 0 deletions rust/allergies/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"rust","exercise":"allergies","id":"a089d96b45364de886ab85b076c4782d","url":"https://exercism.org/tracks/rust/exercises/allergies","handle":"vpayno","is_requester":true,"auto_approve":false}
8 changes: 8 additions & 0 deletions rust/allergies/.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
4 changes: 4 additions & 0 deletions rust/allergies/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
edition = "2021"
name = "allergies"
version = "1.1.0"
85 changes: 85 additions & 0 deletions rust/allergies/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# 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)
- [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
71 changes: 71 additions & 0 deletions rust/allergies/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Allergies

Welcome to Allergies on Exercism's Rust Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Instructions

Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies.

An allergy test produces a single numeric score which contains the
information about all the allergies the person has (that they were
tested for).

The list of items (and their value) that were tested are:

* eggs (1)
* peanuts (2)
* shellfish (4)
* strawberries (8)
* tomatoes (16)
* chocolate (32)
* pollen (64)
* cats (128)

So if Tom is allergic to peanuts and chocolate, he gets a score of 34.

Now, given just that score of 34, your program should be able to say:

- Whether Tom is allergic to any one of those allergens listed above.
- All the allergens Tom is allergic to.

Note: a given score may include allergens **not** listed above (i.e.
allergens that score 256, 512, 1024, etc.). Your program should
ignore those components of the score. For example, if the allergy
score is 257, your program should only report the eggs (1) allergy.

## Source

### Created by

- @EduardoBautista

### Contributed to by

- @ashleygwilliams
- @coriolinus
- @cwhakes
- @EduardoBautista
- @efx
- @ErikSchierboom
- @IanWhitney
- @kytrinyx
- @lutostag
- @mkantor
- @navossoc
- @nfiles
- @petertseng
- @rofrol
- @stringparser
- @taravancil
- @xakon
- @ZapAnton

### Based on

Jumpstart Lab Warm-up - http://jumpstartlab.com

### My Solution

- [my solution](./src/lib.rs)
- [run-tests](./run-tests-rust.txt)
108 changes: 108 additions & 0 deletions rust/allergies/coverage-annotations.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
1| |pub struct Allergies {
2| | score: u32,
3| |}
4| |
5| 264|#[derive(Debug, Clone, Copy, PartialEq, Eq)]
^0 ^0 ^0
------------------
| Unexecuted instantiation: <allergies::Allergen as core::fmt::Debug>::fmt
------------------
| Unexecuted instantiation: <allergies::Allergen as core::fmt::Debug>::fmt
------------------
| Unexecuted instantiation: <allergies::Allergen as core::clone::Clone>::clone
------------------
| Unexecuted instantiation: <allergies::Allergen as core::clone::Clone>::clone
------------------
| <allergies::Allergen as core::cmp::PartialEq>::eq:
| 5| 264|#[derive(Debug, Clone, Copy, PartialEq, Eq)]
------------------
| Unexecuted instantiation: <allergies::Allergen as core::cmp::PartialEq>::eq
------------------
| Unexecuted instantiation: <allergies::Allergen as core::cmp::PartialEq>::eq
------------------
6| |pub enum Allergen {
7| | Eggs = 0b0000_0001,
8| | Peanuts = 0b0000_0010,
9| | Shellfish = 0b0000_0100,
10| | Strawberries = 0b0000_1000,
11| | Tomatoes = 0b0001_0000,
12| | Chocolate = 0b0010_0000,
13| | Pollen = 0b0100_0000,
14| | Cats = 0b1000_0000,
15| |}
16| |
17| |impl Allergies {
18| 57| pub fn new(score: u32) -> Self {
19| 57| Allergies { score }
20| 57| }
------------------
| <allergies::Allergies>::new:
| 18| 57| pub fn new(score: u32) -> Self {
| 19| 57| Allergies { score }
| 20| 57| }
------------------
| Unexecuted instantiation: <allergies::Allergies>::new
------------------
21| |
22| 258| pub fn is_allergic_to(&self, allergen: &Allergen) -> bool {
23| 258| self.score & *allergen as u32 == *allergen as u32
24| 258| }
------------------
| <allergies::Allergies>::is_allergic_to:
| 22| 258| pub fn is_allergic_to(&self, allergen: &Allergen) -> bool {
| 23| 258| self.score & *allergen as u32 == *allergen as u32
| 24| 258| }
------------------
| Unexecuted instantiation: <allergies::Allergies>::is_allergic_to
------------------
25| |
26| 27| pub fn allergies(&self) -> Vec<Allergen> {
27| 27| let allergens: [Allergen; 8] = [
28| 27| Allergen::Eggs,
29| 27| Allergen::Peanuts,
30| 27| Allergen::Shellfish,
31| 27| Allergen::Strawberries,
32| 27| Allergen::Tomatoes,
33| 27| Allergen::Chocolate,
34| 27| Allergen::Pollen,
35| 27| Allergen::Cats,
36| 27| ];
37| 27| let mut result: Vec<Allergen> = Vec::new();
38| |
39| 243| for allergen in allergens {
^216
40| 216| if self.is_allergic_to(&allergen) {
41| 81| result.push(allergen);
42| 135| }
43| | }
44| |
45| 27| result
46| 27| }
------------------
| <allergies::Allergies>::allergies:
| 26| 27| pub fn allergies(&self) -> Vec<Allergen> {
| 27| 27| let allergens: [Allergen; 8] = [
| 28| 27| Allergen::Eggs,
| 29| 27| Allergen::Peanuts,
| 30| 27| Allergen::Shellfish,
| 31| 27| Allergen::Strawberries,
| 32| 27| Allergen::Tomatoes,
| 33| 27| Allergen::Chocolate,
| 34| 27| Allergen::Pollen,
| 35| 27| Allergen::Cats,
| 36| 27| ];
| 37| 27| let mut result: Vec<Allergen> = Vec::new();
| 38| |
| 39| 243| for allergen in allergens {
| ^216
| 40| 216| if self.is_allergic_to(&allergen) {
| 41| 81| result.push(allergen);
| 42| 135| }
| 43| | }
| 44| |
| 45| 27| result
| 46| 27| }
------------------
| Unexecuted instantiation: <allergies::Allergies>::allergies
------------------
47| |}
8 changes: 8 additions & 0 deletions rust/allergies/coverage-missing-lines.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Running: cargo llvm-cov --no-clean --all-features --workspace --show-missing-lines

Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/home/vpayno/git_vpayno/exercism-workspace/rust/allergies/src/lib.rs 13 3 76.92% 6 2 66.67% 27 2 92.59% 0 0 -
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL 13 3 76.92% 6 2 66.67% 27 2 92.59% 0 0 -

Loading