-
Notifications
You must be signed in to change notification settings - Fork 0
/
7 kyu Weight of its Contents.py
42 lines (29 loc) · 1.91 KB
/
7 kyu Weight of its Contents.py
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
"""
https://www.codewars.com/kata/53921994d8f00b93df000bea/train/python
Welcome to the Mathematics gameshow. I'm your host, Apex Rhombus, and it's time for the lightning round!
Today we'll talk about a hypothetical bottle. This entire bottle weighs 120 grams. Its contents weigh twice as much as the bottle itself. What, may I ask, do the contents weigh?
...Did you guess 80 grams? Correct! Now that you've got that idea, I'm gonna ask you that question in 10 different ways so you'd better get ready!
Let's make a contentWeight function that takes in two parameters: bottleWeight and scale. This function will return the weight of the contents inside the bottle.
bottleWeight will be an integer representing the weight of the entire bottle (contents included).
scale will be a string that you will need to parse. It will tell you how the content weight compares to the weight of the bottle by itself. 2 times larger, 6 times larger, and 15 times smaller would all be valid strings (smaller and larger are the only comparison words).
The first test case has been filled out for you. Good luck!
"""
def content_weight(bottle_weight, scale):
scale = scale.split()
s = int(scale[0])
# long answer
# if scale[2] == 'larger':
# return bottle_weight * s / (s + 1)
# return bottle_weight / (s + 1)
# short answer
return bottle_weight * s / (s + 1) if scale[2] == 'larger' else bottle_weight / (s + 1)
print(content_weight(120, "2 times larger"), 80)
print(content_weight(180, "2 times larger"), 120)
print(content_weight(500, "9 times larger"), 450)
print(content_weight(1000, "49 times larger"), 980)
print(content_weight(1000, "999 times larger"), 999)
print(content_weight(120, "2 times smaller"), 40)
print(content_weight(300, "2 times smaller"), 100)
print(content_weight(1000, "4 times smaller"), 200)
print(content_weight(1000, "49 times smaller"), 20)
print(content_weight(10000, "999 times smaller"), 10)