diff --git a/week_1/week_1-ritapragya-biswas.html b/week_1/week_1-ritapragya-biswas.html new file mode 100644 index 00000000..e39e8195 --- /dev/null +++ b/week_1/week_1-ritapragya-biswas.html @@ -0,0 +1,168 @@ + + + + + + + + Document + + + + +

My Guest Book

+ Please sign my guest book. Thanks! + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+ +
+ +
+ +
+ +
+ +
+
+ +
+ +
+ +
+ +
+ +
+ +
+
+ +
+
+ +
+
+ +
+ + +
+
+ +
+
+ +
+
+
+ + +
+ + + diff --git a/week_2/pet.rb b/week_2/pet.rb index 954a713b..00b0914d 100644 --- a/week_2/pet.rb +++ b/week_2/pet.rb @@ -1,115 +1,73 @@ class Pet - ANIMAL_TYPE = { - 1 => 'Arthropod', - 2 => 'Fish', - 3 => 'Amphibia', - 4 => 'Reptiles', - 5 => 'Birds', - 6 => 'Mammals' - } + ANIMAL_TYPE = { + 1 => 'Arthropod', + 2 => 'Fish', + 3 => 'Amphibia', + 4 => 'Reptiles', + 5 => 'Birds', + 6 => 'Mammals' + } - # Each animal type requires a diffrent type of food - # The food cost per 1 kg is is stored in the `FOOD_COST_PER_KG` Hash - # where the `key` is the animal id and `value` is the cost - # - # Eg- The food of reptiles(animal_type id = 4) costs 300/kg. - # (FOOD_COST_PER_KG[4] = 300) - FOOD_COST_PER_KG = { - 1 => 100, - 2 => 600, - 3 => 1000, - 4 => 300, - 5 => 450, - 6 => 800 - } + FOOD_COST_PER_KG = { + 1 => 100, + 2 => 600, + 3 => 1000, + 4 => 300, + 5 => 450, + 6 => 800 + } - # Each animal requires its own habitat. - # - # The `HABITATS` hash stores the habitat required by each animal - # The `key` is the habitat and the `value` is the array of - # animal_type ids that live in that habitat. - # - # "habitat" => [animal_type_id] - HABITATS = { - "Aquatic" => [2, 1, 3], - "Terrariums" => [4], - "Cages" => [5, 6] - } + HABITATS = { + "Aquatic" => [2, 1, 3], + "Terrariums" => [4], + "Cages" => [5, 6] + } - attr_accessor :name, :animal_type_id, :food_consumed_per_day + attr_accessor :name, :animal_type_id, :food_consumed_per_day - def initialize(name: , animal_type_id: , food_consumed_per_day:) - @name = name - @animal_type_id = animal_type_id - @food_consumed_per_day = food_consumed_per_day - end + def initialize(name: , animal_type_id: , food_consumed_per_day:) + @name = name + @animal_type_id = animal_type_id + @food_consumed_per_day = food_consumed_per_day + end - # Return the habitat of the pet - def habitat - raise NotImplementedError # TODO - end + def habitat + if HABITATS["Aquatic"].include? animal_type_id + return "Aquatic" + end + if HABITATS["Terrariums"].include? animal_type_id + return "Terrariums" + end + if HABITATS["Cages"].include? animal_type_id + return "Cages" + end + end - # Returns the cost of food required to feed the animal - # per day - def food_cost_per_day - return FOOD_COST_PER_KG[animal_type_id] * food_consumed_per_day - end + def food_cost_per_day + return FOOD_COST_PER_KG[animal_type_id] * food_consumed_per_day + end - # This function takes the number of `days` as the input - # and returns the food required(in kgs) to feed the - # pet for that many number of days - # - # Eg - if the pet requires 0.5 kg of food per day - # (i.e, food_consumed_per_day = 0.5), then - # pet.food_required(5) will return 2.5 as the answer - # - # 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 - end + def food_required(days) + 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 - end + def food_cost(days) + return food_cost_per_day * days + end - # This function takes an array of pets and the `days` - # as input and returns the cost to feed them all - # for the specified number of `days` - # - # Eg - - # cat = Pet.new(name: 'cat', animal_type_id: 6, food_consumed_per_day: 0.4) - # dog = Pet.new(name: 'dog', animal_type_id: 6, food_consumed_per_day: 0.7) - # fish = Pet.new(name: 'clownfish', animal_type_id: 2, food_consumed_per_day: 0.1) - # 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 - end + def self.cost_to_feed(pets, days) + for animal in pets + total += animal.food_consumed_per_day * FOOD_COST_PER_KG[animal] + end + return total * days.to_i + end - # This function takes an array of pets as input - # and returns a hash with pets name grouped by their animal type - # - # Eg - - # cat = Pet.new(name: 'cat', animal_type_id: 6, food_consumed_per_day: 0.4) - # dog = Pet.new(name: 'dog', animal_type_id: 6, food_consumed_per_day: 0.7) - # fish = Pet.new(name: 'clownfish', animal_type_id: 2, food_consumed_per_day: 0.1) - # snake = Pet.new(name: 'python', animal_type_id: 4, food_consumed_per_day: 0.3) - # Pet.group_by_animal_type([cat, dog, fish, snake]) will return the follwing hash - # { - # 6 => ['cat', 'dog'], - # 2 => ['clownfish'], - # 4 => ['python'] - # } - # - # Note - Order is not important - def self.group_by_animal_type(pets) - raise NotImplementedError # TODO - end + def self.group_by_animal_type(pets) + ans = {} + for animal in pets + ans[animal.animal_type_id].push(name) #couldn't figure out how to make the values of the hash to be arrays + end + print pets + return ans + end end diff --git a/week_2/prime_numbers.rb b/week_2/prime_numbers.rb index 777a8cf5..f2287ede 100644 --- a/week_2/prime_numbers.rb +++ b/week_2/prime_numbers.rb @@ -1,18 +1,20 @@ -# A prime number is a whole number greater than 1 -# that cannot be exactly divided by any whole number -# other than itself and 1 -# (e.g. 2, 3, 5, 7, 11). +def prime(n) -# The function below takes a keyword arguments `n` and -# returns an array of prime numbers less than or equal to -# `n`. + for i in (2..n/2) + if n % i == 0 + return false + end + end + return true -# For example, prime_numbers(n: 20) should return the following: -# [2, 3, 5, 7, 11, 13, 17, 19] +end -# 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 + arr = Array.new + for i in (2..n) + if prime(i) + arr.push(i) + end + end + return arr end