'Year to Century Codewars task
The first century spans from the year 1 up to and including the year 100, the second century - from the year 101 up to and including the year 200, etc.
Task Given a year, return the century it is in.
def century(year):
# Finish this :)
if year % 10 == 0:
return (year // 100)
else:
return (year // 100) +1
print(century(8110))
For some reason that I don't understand, some of random tests to this task fail. Some specific numbers such as 6580,3150,2560,8030,8460 are not passing the test cases. Could you explain what is wrong with my code?
Solution 1:[1]
Your program returns an incorrect answer for inputs that are multiples of 10, but not 100. (e.g. your program says that 2009 and 2011 are in the 21st century, but 2010 is in the 20th century.)
You should only avoid adding one to the century count if a year is a multiple of 100, not 10.
def century(year):
if year % 100 == 0:
return (year // 100)
else:
return (year // 100) +1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BrokenBenchmark |
