From da080a0331a5f8275ed6bb59460f870d7c0dc658 Mon Sep 17 00:00:00 2001 From: Gautam Sivakumar Date: Mon, 13 Mar 2023 22:33:26 +0530 Subject: [PATCH] Week 2 pet_store and prime_numbers assignment --- week_2/pet.rb | 41 +++++++++++++++++++++++++++++++++-------- week_2/prime_numbers.rb | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/week_2/pet.rb b/week_2/pet.rb index 954a713b..b162f4c6 100644 --- a/week_2/pet.rb +++ b/week_2/pet.rb @@ -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 @@ -66,9 +74,8 @@ 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 @@ -76,7 +83,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_KG[@animal_type_id] * days * @food_consumed_per_day end # This function takes an array of pets and the `days` @@ -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 @@ -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 diff --git a/week_2/prime_numbers.rb b/week_2/prime_numbers.rb index 777a8cf5..5c8be63a 100644 --- a/week_2/prime_numbers.rb +++ b/week_2/prime_numbers.rb @@ -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 @@ -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