From d454dd14da4d413784b17c02560b80697d89e5f2 Mon Sep 17 00:00:00 2001 From: Suchit <78025461+suchit07-git@users.noreply.github.com> Date: Sat, 31 Dec 2022 10:07:56 +0000 Subject: [PATCH 1/2] Week 2 assignment --- week_2/Gemfile | 8 +++ week_2/Gemfile.lock | 150 ++++++++++++++++++++++++++++++++++++++++ week_2/pet.rb | 36 ++++++++-- week_2/prime_numbers.rb | 36 +++++++++- 4 files changed, 222 insertions(+), 8 deletions(-) create mode 100644 week_2/Gemfile create mode 100644 week_2/Gemfile.lock diff --git a/week_2/Gemfile b/week_2/Gemfile new file mode 100644 index 00000000..4ba6ffca --- /dev/null +++ b/week_2/Gemfile @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } + +gem "rails" +gem "minitest" diff --git a/week_2/Gemfile.lock b/week_2/Gemfile.lock new file mode 100644 index 00000000..a5ad138b --- /dev/null +++ b/week_2/Gemfile.lock @@ -0,0 +1,150 @@ +GEM + remote: https://rubygems.org/ + specs: + actioncable (7.0.4) + actionpack (= 7.0.4) + activesupport (= 7.0.4) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + actionmailbox (7.0.4) + actionpack (= 7.0.4) + activejob (= 7.0.4) + activerecord (= 7.0.4) + activestorage (= 7.0.4) + activesupport (= 7.0.4) + mail (>= 2.7.1) + net-imap + net-pop + net-smtp + actionmailer (7.0.4) + actionpack (= 7.0.4) + actionview (= 7.0.4) + activejob (= 7.0.4) + activesupport (= 7.0.4) + mail (~> 2.5, >= 2.5.4) + net-imap + net-pop + net-smtp + rails-dom-testing (~> 2.0) + actionpack (7.0.4) + actionview (= 7.0.4) + activesupport (= 7.0.4) + rack (~> 2.0, >= 2.2.0) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.2.0) + actiontext (7.0.4) + actionpack (= 7.0.4) + activerecord (= 7.0.4) + activestorage (= 7.0.4) + activesupport (= 7.0.4) + globalid (>= 0.6.0) + nokogiri (>= 1.8.5) + actionview (7.0.4) + activesupport (= 7.0.4) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.1, >= 1.2.0) + activejob (7.0.4) + activesupport (= 7.0.4) + globalid (>= 0.3.6) + activemodel (7.0.4) + activesupport (= 7.0.4) + activerecord (7.0.4) + activemodel (= 7.0.4) + activesupport (= 7.0.4) + activestorage (7.0.4) + actionpack (= 7.0.4) + activejob (= 7.0.4) + activerecord (= 7.0.4) + activesupport (= 7.0.4) + marcel (~> 1.0) + mini_mime (>= 1.1.0) + activesupport (7.0.4) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 1.6, < 2) + minitest (>= 5.1) + tzinfo (~> 2.0) + builder (3.2.4) + concurrent-ruby (1.1.10) + crass (1.0.6) + date (3.3.3) + erubi (1.12.0) + globalid (1.0.0) + activesupport (>= 5.0) + i18n (1.12.0) + concurrent-ruby (~> 1.0) + loofah (2.19.1) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + mail (2.8.0) + mini_mime (>= 0.1.1) + net-imap + net-pop + net-smtp + marcel (1.0.2) + method_source (1.0.0) + mini_mime (1.1.2) + minitest (5.16.3) + net-imap (0.3.4) + date + net-protocol + net-pop (0.1.2) + net-protocol + net-protocol (0.2.1) + timeout + net-smtp (0.3.3) + net-protocol + nio4r (2.5.8) + nokogiri (1.13.10-x86_64-linux) + racc (~> 1.4) + racc (1.6.2) + rack (2.2.5) + rack-test (2.0.2) + rack (>= 1.3) + rails (7.0.4) + actioncable (= 7.0.4) + actionmailbox (= 7.0.4) + actionmailer (= 7.0.4) + actionpack (= 7.0.4) + actiontext (= 7.0.4) + actionview (= 7.0.4) + activejob (= 7.0.4) + activemodel (= 7.0.4) + activerecord (= 7.0.4) + activestorage (= 7.0.4) + activesupport (= 7.0.4) + bundler (>= 1.15.0) + railties (= 7.0.4) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.4.4) + loofah (~> 2.19, >= 2.19.1) + railties (7.0.4) + actionpack (= 7.0.4) + activesupport (= 7.0.4) + method_source + rake (>= 12.2) + thor (~> 1.0) + zeitwerk (~> 2.5) + rake (13.0.6) + thor (1.2.1) + timeout (0.3.1) + tzinfo (2.0.5) + concurrent-ruby (~> 1.0) + websocket-driver (0.7.5) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.5) + zeitwerk (2.6.6) + +PLATFORMS + x86_64-linux + +DEPENDENCIES + minitest + rails + +BUNDLED WITH + 2.2.33 diff --git a/week_2/pet.rb b/week_2/pet.rb index 954a713b..6d2cc985 100644 --- a/week_2/pet.rb +++ b/week_2/pet.rb @@ -38,7 +38,7 @@ class Pet attr_accessor :name, :animal_type_id, :food_consumed_per_day - def initialize(name: , animal_type_id: , food_consumed_per_day:) + def initialize(name: "CAT", animal_type_id: 6, food_consumed_per_day: 0.4) @name = name @animal_type_id = animal_type_id @food_consumed_per_day = food_consumed_per_day @@ -46,7 +46,16 @@ def initialize(name: , animal_type_id: , food_consumed_per_day:) # Return the habitat of the pet def habitat - raise NotImplementedError # TODO + isPresent = false + HABITATS.each { |h, at| + if at.include?animal_type_id + isPresent = true + return h + end + } + if !isPresent + raise ArgumentError("Habitat not found") + end end # Returns the cost of food required to feed the animal @@ -66,7 +75,10 @@ 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 + if days < 0 + raise ArgumentError.new("Days should be positive") + end + return food_consumed_per_day * days end # This function takes the number of `days` as the input @@ -76,7 +88,10 @@ 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 + if days < 0 + raise ArgumentError.new("Days should be positive") + end + return food_required(days) * FOOD_COST_PER_KG[animal_type_id] end # This function takes an array of pets and the `days` @@ -90,7 +105,9 @@ 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 + return pets.reduce(0) { + |sum, pet| sum + pet.food_cost(days) + } end # This function takes an array of pets as input @@ -110,6 +127,13 @@ def self.cost_to_feed(pets, days) # # Note - Order is not important def self.group_by_animal_type(pets) - raise NotImplementedError # TODO + res = {} + pets.each { |pet| + res[pet.animal_type_id] = [] + } + pets.group_by { |pet| + res[pet.animal_type_id].push(pet.name) + } + return res end end diff --git a/week_2/prime_numbers.rb b/week_2/prime_numbers.rb index 777a8cf5..efba51a9 100644 --- a/week_2/prime_numbers.rb +++ b/week_2/prime_numbers.rb @@ -13,6 +13,38 @@ # 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 + +def sieve(num) + is_prime = Array.new(num + 1, true) + is_prime[0] = is_prime[1] = false + i = 2 + while i * i <= num + if is_prime[i] + j = i * i + while j <= num + is_prime[j] = false + j += i + end + end + i += 1 + end + return is_prime +end + +def prime_numbers(n:200) + if n <= 0 + raise ArgumentError.new("Positive value expected") + end + res = Array.new() + p = sieve(n) + i = 2 + while i <= n + if p[i] + res.append(i) + end + i += 1 + end + return res end + +# puts "#{prime_numbers()}" \ No newline at end of file From 66d97fc72f96d91cf59f7b3f9ca9d969f28041bf Mon Sep 17 00:00:00 2001 From: Suchit <78025461+suchit07-git@users.noreply.github.com> Date: Sat, 31 Dec 2022 10:16:30 +0000 Subject: [PATCH 2/2] Week 2 assignment --- week_2/pet.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/week_2/pet.rb b/week_2/pet.rb index 6d2cc985..dc8378dd 100644 --- a/week_2/pet.rb +++ b/week_2/pet.rb @@ -76,7 +76,7 @@ def food_cost_per_day # cat.food_required(28) = 11.2 (0.4 * 28) def food_required(days) if days < 0 - raise ArgumentError.new("Days should be positive") + raise ArgumentError.new("Number of days should be positive") end return food_consumed_per_day * days end @@ -89,7 +89,7 @@ def food_required(days) # cat.food_cost(28) = 8960 def food_cost(days) if days < 0 - raise ArgumentError.new("Days should be positive") + raise ArgumentError.new("Number of days should be positive") end return food_required(days) * FOOD_COST_PER_KG[animal_type_id] end