나는 이렇게 학습한다/Algorithm & SQL
Round up to the next multiple of 5
daco2020
2022. 5. 10. 20:58
반응형
Given an integer as input, can you round it to the next (meaning, "higher") multiple of 5?
Examples:
input: output:
0 -> 0
2 -> 5
3 -> 5
12 -> 15
21 -> 25
30 -> 30
-2 -> 0
-5 -> -5
etc.
Input may be any positive or negative integer (including 0).
You can assume that all inputs are valid integers.
Solution:
1. Divide n by 5 and round up.
2. Multiply the rounded value by 5.
def round_to_next5(n):
import math
return math.ceil(n/5)*5
In addition to rounding up, the remainder can be used to obtain a value.
def round_to_next5(n):
return n + (5 - n) % 5
반응형