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

Week 2 Gautam_S #151

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
41 changes: 33 additions & 8 deletions week_2/pet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ def initialize(name: , animal_type_id: , food_consumed_per_day:)

# Return the habitat of the pet
def habitat
raise NotImplementedError # TODO

HABITATS.each do |key, value|

# print "Animal id #{@animal_type_id} key is #{key} value is #{value}"

if (value.include?(@animal_type_id)) then
return key
end
end
end

# Returns the cost of food required to feed the animal
Expand All @@ -66,17 +74,16 @@ 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
# and returns the cost incurred to feed the pet for
# that many number of 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_KG[@animal_type_id] * days * @food_consumed_per_day
end

# This function takes an array of pets and the `days`
Expand All @@ -90,7 +97,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
total_cost = 0
pets.each do |pet|
total_cost = total_cost + pet.food_consumed_per_day*days*FOOD_COST_PER_KG[pet.animal_type_id]
end
return total_cost
end

# This function takes an array of pets as input
Expand All @@ -109,7 +120,21 @@ def self.cost_to_feed(pets, days)
# }
#
# Note - Order is not important
def self.group_by_animal_type(pets)
raise NotImplementedError # TODO
end



def self.group_by_animal_type(pets)
result = {}

pets.each do |pet|
if result[pet.animal_type_id]
result[pet.animal_type_id] << pet.name
else
result[pet.animal_type_id] = [pet.name]
end
end

result
end

end
39 changes: 38 additions & 1 deletion week_2/prime_numbers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@

def is_prime num

if num <= 1 then
return false
end

s = Math.sqrt(num)

s = s.floor()

# print "sqrt of #{num} is #{s} "

while s > 1
if (num % s == 0) then
return false
else
s = s - 1
end
end
return true
end

# A prime number is a whole number greater than 1
# that cannot be exactly divided by any whole number
# other than itself and 1
Expand All @@ -14,5 +37,19 @@
# We will raise an `ArgumentError` exception to let the caller know that
# their function arguments were incorrect.
def prime_numbers(n:)
raise NotImplementedError # TODO

if (n <= 1) then
raise ArgumentError
end
i = 0
result = []

while (i <= n)
if (is_prime i) then
result.push(i)
end
i = i +1
end

return result
end