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

Week2 Assignment prashant kumar choudhary #88

Open
wants to merge 5 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
30 changes: 22 additions & 8 deletions week_2/pet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ def initialize(name: , animal_type_id: , food_consumed_per_day:)

# Return the habitat of the pet
def habitat
raise NotImplementedError # TODO
HABITATS.each do |k,v|
if v.include?(animal_type_id)
return k
end
end
end

# Returns the cost of food required to feed the animal
# 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
Expand All @@ -66,7 +70,7 @@ 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
food_consumed_per_day*days
end

# This function takes the number of `days` as the input
Expand All @@ -76,11 +80,11 @@ 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
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
# as input and returns the cost to feed them all
# for the specified number of `days`
#
# Eg -
Expand All @@ -90,13 +94,13 @@ 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
pets.reduce(0) { |tot,pet| tot + pet.food_cost(days) }
end

# This function takes an array of pets as input
# and returns a hash with pets name grouped by their animal type
#
# Eg -
# 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)
Expand All @@ -110,6 +114,16 @@ def self.cost_to_feed(pets, days)
#
# Note - Order is not important
def self.group_by_animal_type(pets)
raise NotImplementedError # TODO
temp=pets.group_by { |i| i.animal_type_id }

pet_group = {}

temp.each do |id,objs|
names = []
objs.each { |obj| names<<obj.name }
Comment on lines +122 to +123
Copy link
Collaborator

Choose a reason for hiding this comment

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

map can also be used here.

pet_group[id]=names
end

pet_group
end
end
4 changes: 2 additions & 2 deletions week_2/pet_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_cost_to_feed_pets
end

[4, 5, 7, 50, 0, 18, 400].each do |days|
assert_equal Pet.cost_to_feed(rand_pets, days),
assert_equal Pet.cost_to_feed(rand_pets, days),
expected_cost.reduce(0) { |sum, c| sum + c * days },
"Cost to feed animals do not match"
end
Expand Down Expand Up @@ -109,7 +109,7 @@ def test_group_by_animal_type

assert_equal Set.new(expected.keys), Set.new(answer.keys),
"No extra animal type ids should be present"

expected.each do |k, pet_names|
assert_equal Set.new(pet_names), Set.new(answer[k]),
"Group of animal_type_id = #{k}, doesn't match"
Expand Down
32 changes: 16 additions & 16 deletions week_2/prime_numbers.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# 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).

# The function below takes a keyword arguments `n` and
# returns an array of prime numbers less than or equal to
# `n`.

# For example, prime_numbers(n: 20) should return the following:
# [2, 3, 5, 7, 11, 13, 17, 19]

# 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
if n>1
primes=[]
(2..n).each{ |n|
prime=true
(2..n/2).each{ |d|
if n%d==0
prime=false
break
end
}
primes << n if prime
}
return primes
else
raise ArgumentError
end
end