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

week2_Mardav_Gandhi #120

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 22 additions & 5 deletions week_2/pet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ def initialize(name: , animal_type_id: , food_consumed_per_day:)

# Return the habitat of the pet
def habitat
raise NotImplementedError # TODO
for (key, value) in HABITATS
if value.include?(animal_type_id)
return key
end
end
end

# Returns the cost of food required to feed the animal
Expand All @@ -66,7 +70,7 @@ def food_cost_per_day
# cat = Pet.new(name: 'cat', animal_type_id: 6, food_consumed_per_day: 0.4)
# cat.food_required(28) = 11.2 (0.4 * 28)
def food_required(days)
raise NotImplementedError # TODO
return food_consumed_per_day * days
end

# This function takes the number of `days` as the input
Expand All @@ -76,7 +80,7 @@ def food_required(days)
# cat = Pet.new(name: 'cat', animal_type_id: 6, food_consumed_per_day: 0.4)
# cat.food_cost(28) = 8960
def food_cost(days)
raise NotImplementedError # TODO
return food_cost_per_day * days
end

# This function takes an array of pets and the `days`
Expand All @@ -90,7 +94,11 @@ def food_cost(days)
# snake = Pet.new(name: 'python', animal_type_id: 4, food_consumed_per_day: 0.3)
# Pet.cost_to_feed([cat, dog, fish, snake], 6) will return 6180.0
def self.cost_to_feed(pets, days)
raise NotImplementedError # TODO
cost = 0
for pet in pets
cost += pet.food_cost(days)
end
return cost
end

# This function takes an array of pets as input
Expand All @@ -110,6 +118,15 @@ def self.cost_to_feed(pets, days)
#
# Note - Order is not important
def self.group_by_animal_type(pets)
raise NotImplementedError # TODO
obj = Hash.new()
for pet in pets
if !obj[pet.animal_type_id]
obj[pet.animal_type_id] = [pet.name]
else
obj[pet.animal_type_id].push(pet.name)
end
end
return obj
end

end
18 changes: 17 additions & 1 deletion week_2/prime_numbers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@
# If the user gives a invalid input like -4
# We will raise an `ArgumentError` exception to let the caller know that
# their function arguments were incorrect.

def prime_numbers(n:)
raise NotImplementedError # TODO
raise ArgumentError, "n must be positive int" if n <= 0
primes = Array.new()

for i in 2..n
is_prime = true
for j in 2..(i - 1)
if i % j == 0
is_prime = false
break
end
end
primes << i if is_prime
i+=1
end
return primes
end