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

Ruby day17 #290

Merged
merged 4 commits into from
Oct 28, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Exercism Workspace
| July | Jurassic | C (5/5), C++ (5/5), Fortran (0/5) |
| August | Apps | Dart (5/5), Java (0/5), Kotlin (0/5) |
| September | Slimline | Awk (5/5), Bash\* (5/5), jq (0/5), Perl (0/5) |
| October | Object-Oriented | Ruby (0/5), Java (0/5) |
| October | Object-Oriented | Ruby (1/5), Java (0/5) |
| November | Nibbly | |
| December | Diversions | |

Expand Down
2 changes: 2 additions & 0 deletions ruby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,5 @@
- [protein-translation](./protein-translation/README.md)
- [series](./series/README.md)
- [word-count](./word-count/README.md)
- [allergies](./allergies/README.md)
- [simple-cipher](./simple-cipher/README.md)
32 changes: 32 additions & 0 deletions ruby/allergies/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"authors": [
"kytrinyx"
],
"contributors": [
"ajwann",
"budmc29",
"cadwallion",
"hilary",
"iHiD",
"Insti",
"jpotts244",
"kotp",
"mikegehard",
"pendletons",
"tryantwit"
],
"files": {
"solution": [
"allergies.rb"
],
"test": [
"allergies_test.rb"
],
"example": [
".meta/example.rb"
]
},
"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": "Exercise by the JumpstartLab team for students at The Turing School of Software and Design.",
"source_url": "https://turing.edu"
}
1 change: 1 addition & 0 deletions ruby/allergies/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"ruby","exercise":"allergies","id":"fbd4b08f2e4b47b6830990e8560871b6","url":"https://exercism.org/tracks/ruby/exercises/allergies","handle":"vpayno","is_requester":true,"auto_approve":false}
54 changes: 54 additions & 0 deletions ruby/allergies/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Help

## Running the tests

For running the tests provided, you will need the Minitest gem. Open a
terminal window and run the following command to install minitest:

```
gem install minitest
```


Run the tests from the exercise directory using the following command:

```
ruby <snake-case-exercise>_test.rb
```

Please replace `<snake-case-exercise>` with your exercise name in snake_case.

## Color output

You can `require 'minitest/pride'` or run the following command to get colored output:

```
ruby -r minitest/pride <snake-case-exercise>_test.rb
```

## Submitting your solution

You can submit your solution using the `exercism submit allergies.rb` 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 [Ruby track's documentation](https://exercism.org/docs/tracks/ruby)
- The [Ruby track's programming category on the forum](https://forum.exercism.org/c/programming/ruby)
- [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.

To get help if you're having trouble, you can use one of the following resources:

- [Ruby Documentation](http://ruby-doc.org/)
- [StackOverflow](http://stackoverflow.com/questions/tagged/ruby)
- [/r/ruby](https://www.reddit.com/r/ruby) is the Ruby subreddit.
61 changes: 61 additions & 0 deletions ruby/allergies/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Allergies

Welcome to Allergies on Exercism's Ruby 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

- @kytrinyx

### Contributed to by

- @ajwann
- @budmc29
- @cadwallion
- @hilary
- @iHiD
- @Insti
- @jpotts244
- @kotp
- @mikegehard
- @pendletons
- @tryantwit

### Based on

Exercise by the JumpstartLab team for students at The Turing School of Software and Design. - https://turing.edu

### My Solution

- [my solution](./allergies.rb)
- [run-tests](./run-tests-ruby.txt)
34 changes: 34 additions & 0 deletions ruby/allergies/allergies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: false

# https://exercism.org/tracks/ruby/exercises/allergies
# Allergies exercise
class Allergies
def initialize(score)
@score = score

@allergies = {
'eggs' => 1,
'peanuts' => 2,
'shellfish' => 4,
'strawberries' => 8,
'tomatoes' => 16,
'chocolate' => 32,
'pollen' => 64,
'cats' => 128
}
end

def allergic_to?(allergen)
(@allergies.fetch(allergen, 0) & @score).positive?
end

def list
allergic_reactions = []

@allergies.each_pair do |key, value|
allergic_reactions.push(key) unless (value & @score).zero?
end

allergic_reactions
end
end
Loading