-
Notifications
You must be signed in to change notification settings - Fork 68
/
Licks.rb
52 lines (41 loc) · 2.24 KB
/
Licks.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
=begin
How many licks does it take to get to the tootsie roll center of a tootsie pop?
A group of engineering students from Purdue University reported that its licking
machine, modeled after a human tongue, took an average of 364 licks to get to
the center of a Tootsie Pop. Twenty of the group's volunteers assumed the
licking challenge-unassisted by machinery-and averaged 252 licks each to the
center.
Your task, if you choose to accept it, is to write a function that will return
the number of licks it took to get to the tootsie roll center of a tootsie pop,
given some environmental variables.
Everyone knows it's harder to lick a tootsie pop in cold weather but it's easier
if the sun is out. You will be given an object of environmental conditions for
each trial paired with a value that will increase or decrease the number of
licks. The environmental conditions all apply to the same trial.
Assuming that it would normally take 252 licks to get to the tootsie roll center
of a tootsie pop, return the new total of licks along with the condition that
proved to be most challenging (causing the most added licks) in that trial.
Example:
totalLicks({ "freezing temps": 10, "clear skies": -2 });
Should return:
"It took 260 licks to get to the tootsie roll center of a tootsie pop. The
toughest challenge was freezing temps."
Other cases: If there are no challenges, the toughest challenge sentence should
be omitted. If there are multiple challenges with the highest toughest amount,
the first one presented will be the toughest. If an environment variable is
present, it will be either a positive or negative integer. No need to validate.
=end
# My Solution
def total_licks(env)
return "It took 252 licks to get to the tootsie roll center of a tootsie pop." if env == {}
sum = 0
env.each {|k,v| sum += v}
result = "It took #{252+sum} licks to get to the tootsie roll center of a tootsie pop."
result += " The toughest challenge was #{env.max_by{|k,v| v}[0]}." if env.max_by{|k,v| v}[1] > 0
result
end
# Better Solution
def total_licks(env)
env.any? && env.values.max > 0 ? str = " The toughest challenge was #{env.key(env.values.max)}." : str = ''
"It took #{252 + env.values.inject(0, :+)} licks to get to the tootsie roll center of a tootsie pop." + str
end