-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathround_sum.py
29 lines (23 loc) · 955 Bytes
/
round_sum.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
#CodingBat - Python
#round_sum
#For this problem, we'll round an int value up to the next multiple of 10 if
#its rightmost digit is 5 or more, so 15 rounds up to 20. Alternately, round
#down to the previous multiple of 10 if its rightmost digit is less than 5, so
#12 rounds down to 10. Given 3 ints, a b c, return the sum of their rounded
#values. To avoid code repetition, write a separate helper "def round10(num):"
#and call it 3 times. Write the helper entirely below and at the same indent
#level as round_sum().
# round_sum(16, 17, 18) → 60
# round_sum(12, 13, 14) → 30
# round_sum(6, 4, 4) → 10
def round_sum(a, b, c):
return round10(a) + round10(b) + round10(c)
def round10(num):
if num % 10 >= 5:
return num + (10 - num % 10)
elif num % 10 < 5:
return num - (num % 10)
#To Check:
#print(round_sum(16, 17, 18))
#print(round_sum(12, 13, 14))
#print(round_sum(6, 4, 4))