반응형
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
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
SQL Basics: Simple WHERE and ORDER BY (0) | 2022.05.12 |
---|---|
Sum of numbers from 0 to N (0) | 2022.05.11 |
Find the capitals (0) | 2022.05.09 |
Coding Meetup #5 - Higher-Order Functions Series - Prepare the count of languages (0) | 2022.05.08 |
Initialize my name (0) | 2022.05.07 |