The first century spans from the year 1 up to and including the year 100, The second - from the year 101 up to and including the year 200, etc. Return the century of the input year.
Examples
"1999" --> "20th"
"2011" --> "21st"
"2154" --> "22nd"
"2259" --> "23rd"
"1124" --> "12th"
"2000" --> "21st"
"20000" --> "210th"
Tests
8120
30200
1601
2020
3030
1900
1776
Good luck!
This challenge comes from Cpt.ManlyPink on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (20)
Clean and elegant!
"1066 --> 11st" should be 11th
"10266 --> 103th" should be 103rd
But according to the math, 1 - 100 is 1st century. So I thought 1901 - 2000 is 20th century and 2001 - 2100 is 21st century. Is my understanding wrong ?
This question has conflicts.
"20000" --> "210th" (should it be 201st?)
In the question:
"The first century spans from the year 1 up to and including the year 100, The second - from the year 101 up to and including the year 200, etc. Return the century of the input year"
but toCentury(2000) -> 21st?
Good question. I didn't notice that in the test cases.
Here is my golang solution
what about the year 11122?
I think, it will be 112nd century, because, current, 2020 year is inside of 21st century :)
It should be 112th not 112nd, because 12 gets a th
ohhhh, it was grammatically mistake, sorry
Solution in Haskell:
Here's a typescript implementation, and a ✨verbose-and-pretty✨ reference to check it against.
It's funny, but I take much more care with these little easy tasks than I used to. There's always more nuance than you expect and the implementations end up sitting at the bottom of your library being used for everything.
(btw, I tend to use
~~
as an alternative to ofMath.floor
because it's more succinct, and most often a little quicker)I'll check my working by comparing with a naive implementation that's more readable and hopefully reliable out of the gate...
Py <3
The ruby example
And one-line style with
activesupport
gem installedThis might be a bit over-engineered, but here is another Ruby solution:
Which produces the following output: