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 assignment #86

Open
wants to merge 2 commits into
base: week2_assignment
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
8 changes: 8 additions & 0 deletions week_2/Gemfile
Original file line number Diff line number Diff line change
@@ -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"
150 changes: 150 additions & 0 deletions week_2/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
36 changes: 30 additions & 6 deletions week_2/pet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,24 @@ 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
end

# 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
Expand All @@ -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("Number of days should be positive")
end
return food_consumed_per_day * days
end

# This function takes the number of `days` as the input
Expand All @@ -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("Number of 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`
Expand All @@ -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
Expand All @@ -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)
}
Comment on lines +134 to +136
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong use of group_by, group_by is used for grouping items from an enumerable and return a hash. Since you are just iterating each itself can be used here.

You can also use a single each block to so this.

pets.each do |pet|
  if res[pet.animal_type_id]
      # ....
  else
      res[pet.animal_type_id] = [pet.name]
   end
end  

return res
end
end
36 changes: 34 additions & 2 deletions week_2/prime_numbers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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()}"