I have a few questions regarding the following code from Codewar coding challenge and solution.
(我对Codewar编码挑战和解决方案中的以下代码有一些疑问。)
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.
(以下是上述挑战的Ruby代码。)
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
(问题-1-哈希-&运算符使用)
In the below method, allergen is referred to get the key value from the ALLERGY_SCORES allergy constant hash.
(在下面的方法中,过敏原被引用以从ALLERGY_SCORES过敏常数哈希中获取密钥值。)
Why @score & ALLERGY_SCORES[allergen] > 0
? (为什么@score & ALLERGY_SCORES[allergen] > 0
?)
In ALLERGY_SCORES hash, only values are integers. (在ALLERGY_SCORES哈希中,只有值是整数。)
Why is & operator used in there? (为什么在其中使用&运算符?)
Setting both @socre and ALLERGY_SCORES to greater than 0 - is it also for validation purpose?
(将@socre和ALLERGY_SCORES都设置为大于0-是否也用于验证?)
(in case users enter negative or other values) ((以防用户输入负值或其他值))
def is_allergic_to(allergen)
@score & ALLERGY_SCORES[allergen] > 0
end
Question - 2 - Error Handling
(问题-2-错误处理)
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. (因此,我尝试解决此问题,并在is_allergic_to方法中添加了以下代码。)
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)
(问题-3-创建新的密钥数组(过敏原))
.keys method is to create a new array which is the list of allergen.
(.keys方法是创建一个新数组,该数组是过敏原的列表。)
.select method is also used to to create a new array. (.select方法也用于创建新数组。)
Why are both .keys and .select method used for creating a new array of key from ALLERGY_SCORES hash?
(为什么同时使用.keys和.select方法从ALLERGY_SCORES哈希创建新的键数组?)
def allergies()
ALLERGY_SCORES.keys.select do |allergen|
is_allergic_to(allergen)
end
end
Question - 4 - Code Display Output to the Screen in Terminal
(问题-4-代码显示输出到终端的屏幕)
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
(问题-5-最高可接受分数验证)
Maximum acceptable score is 255. How should I verify the score to make sure the score they have entered is <= 255?
(最高可接受分数是255。如何验证分数以确保他们输入的分数小于或等于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
ask by FuriousCodeNinja translate from so