I have a few questions regarding the following code from Codewar coding challenge and solution. I have listed my questions under the coding solution. Many thanks in advance!
Coding challenge question is below.
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. Write a program that, given a person’s score can tell them: a. whether or not they’re allergic to a given item b. the full list of allergies.
You will be provided with a class Allergies which will have 2 methods
1. is_allergic_to Checks if Tom is allergic to a particular allergen.
Returns True if Tom is allergic, False otherwise allergies
2. Returns a list of what Tom is allergic to. This list must be sorted
alphabetically
3. Must Dos: Ensure that your function throws a TypeError for invalid
inputs such as None(Null), floats, strings, or any data type that is
not an integer.
The following is the Ruby code for the above challenge.
class Allergies
ALLERGY_SCORES = {
"eggs"=> 1,
"peanuts"=> 2,
"shellfish"=> 4,
"strawberries"=> 8,
"tomatoes"=> 16,
"chocolate"=> 32,
"pollen"=> 64,
"cats"=> 128
}
def initialize(score)
@score = score
end
def is_allergic_to(allergen)
@score & ALLERGY_SCORES[allergen] > 0
begin
@score = [/\D/]
rescue StandardError => e
puts "Error occured: #{e}."
puts "Please enter integer ONLY."
end
end
def allergies()
ALLERGY_SCORES.keys.select do |allergen|
is_allergic_to(allergen)
end
end
def acceptable_score()
max_score = 0
ALLERGY_SCORES.each do |allergen, score|
max_score += score
return max_score
end
end
puts "Please enter allergy score or "
score = gets.chomp
Allergies.new.initialize(score)
Allergies.is_allergic_to
# Allergies.allergies
Question - 1 - Hash - & operator use
In the below method, allergen is referred to get the key value from the ALLERGY_SCORES allergy constant hash. Why @score & ALLERGY_SCORES[allergen] > 0
? In ALLERGY_SCORES hash, only values are integers. Why is & operator used in there?
Setting both @socre and ALLERGY_SCORES to greater than 0 - is it also for validation purpose? (in case users enter negative or other values)
def is_allergic_to(allergen)
@score & ALLERGY_SCORES[allergen] > 0
end
Question - 2 - Error Handling
In the code, there was no error handling component. Therefore I have attempted to address this and have added the following code inside is_allergic_to method. However, I get an error.
def is_allergic_to(allergen)
@score & ALLERGY_SCORES[allergen] > 0
**begin
@score = [/\D/]
rescue StandardError => e
puts "Error occured: #{e}."
puts "Please enter integer ONLY."
end**
end
Question - 3 - Creating a new array of key (allergen)
.keys method is to create a new array which is the list of allergen. .select method is also used to to create a new array.
Why are both .keys and .select method used for creating a new array of key from ALLERGY_SCORES hash?
def allergies()
ALLERGY_SCORES.keys.select do |allergen|
is_allergic_to(allergen)
end
end
Question - 4 - Code Display Output to the Screen in Terminal
I want the user to be able to enter their allergy score. But the following code does not work.
puts "Please enter allergy score."
score = gets.chomp
Allergies.new.initialize(score)
Allergies.is_allergic_to
Allergies.allergies
Question - 5 - maximum acceptable score validation
Maximum acceptable score is 255. How should I verify the score to make sure the score they have entered is <= 255? How should I modify this part of code?
def acceptable_score()
max_score = 0
ALLERGY_SCORES.each do |allergen, score|
max_score += score
return max_score
end