Created
September 23, 2018 12:16
-
-
Save harrisonmalone/356cd15576c836602131167f2c65ed96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Allergies | |
def initialize(num) | |
@allergy_num = num | |
@allergy_list = ['eggs', 'peanuts', 'shellfish', 'strawberries', 'tomatoes', 'chocolate', 'pollen', 'cats'] | |
@exp = @allergy_list.count | |
@my_allergies = [] | |
get_allergies(num) | |
end | |
def list | |
return @my_allergies | |
end | |
def get_allergies(num) | |
if num > (2 ** @exp) | |
@my_allergies << @allergy_list[@exp] | |
@exp -= 1 | |
get_allergies(num - (2 ** (@exp+1))) | |
elsif (num < (2 ** @exp)) | |
@exp -= 1 | |
get_allergies(num) | |
else | |
@my_allergies << @allergy_list[@exp] | |
end | |
end | |
def allergic_to?(str) | |
@my_allergies.include?(str) | |
end | |
end | |
# instances | |
allergies1 = Allergies.new(34) | |
print allergies1.list | |
puts | |
allergies2 = Allergies.new(129) | |
print allergies2.list | |
puts | |
puts allergies1.allergic_to?('shellfish') | |
puts allergies1.allergic_to?('cats') | |
puts allergies1.allergic_to?('chocolate') | |
# explanation | |
# we initialize num with a score of 34 | |
# we call the get_allergies method within .new | |
# we enter the get allergies method | |
# the first condition fails (34 is less than 2**8) | |
# the second condition is true | |
# we reduce the exp (to 7) and we call the method again | |
# we enter the get allergies method | |
# the first condition fails (34 is less than 2**7) | |
# the second condition is true | |
# we reduce the exp (to 6) and we call the method again | |
# we enter the get allergies method | |
# the first condition fails (34 is less than 2**6) | |
# the second condition is true | |
# we reduce the exp (to 5) and we call the method again | |
# we enter the get allergies method | |
# the first condition is true (34 is more than 2**5) | |
# we pass in the 5th element of our allergy_list (chocolate) into the new array | |
# we reduce the exp (to 4) | |
# we call the method again, the num is minus by 2 to the power exp(4 + 1) | |
# sum = 34 - 32 | |
# num = 2 | |
# num is reassigned to become 2 | |
# we enter the get allergies method | |
# the first condition fails (2 is less than 2**4) | |
# the second condition is true | |
# we reduce the exp (to 3) and we call the method again | |
# we enter the get allergies method | |
# the first condition fails (2 is less than 2**3) | |
# we reduce the exp (to 2) and we call the method again | |
# we enter the get allergies method | |
# the first condition fails (2 is less than 2**2) | |
# we reduce the exp (to 1) and we call the method again | |
# we enter the get allergies method | |
# the first condition is false (2 is not less than 2**1) | |
# the second condition is false (2 is not more than 2**1) | |
# we run the else condition | |
# we pass the 1st element of our allergies list (peanuts) into the new array | |
# we exit the conditional |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment