Challenge
Write a function that will return an inputted numerical year in century format. The output should have the appropriate written ending ('st','nd','rd','th') as well.
Examples
In: 2259
Out: 23rd
In: 1124
Out: 12th
In: 2000
Out: 21st
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 (19)
A reasonably short and reasonably Rusty solution:
Garna robota!
Good job!
Diakuyu :)
Thanks :)
A bit of functional JS
I like your approach, it looks very clean.
This is probably too fringe to matter in most contexts, but wouldn't your function return 111st for the year 11,092?
It would, indeed. Thanks for pointing out. Now I have fixed it and added a new test case.
OLD SOLUTION (Line 7)
UPDATED SOLUTION (Line 7)
F#
Yes, yes, I know, 2000 should return "21st" century. I don't know of anyone who counts centuries like that, so my function returns them according to normal use.
This would've been even more interesting using the strict usage of "century" ;-)
First handle the exceptions (i.e. 11 - 13), then just use the last digit to decide.
python
A length Javascript solution, but I did not want to divide by 100.
Tried it with a few different years including the edge cases.
Rust:
Insert Go Pun Here
century.go
century_test.go
Ah, this one was tricker than I thought because of the edge cases.
I choose a solution in JS that lists out all the endings in an object - but since they are almost all the same, maybe I should have done something else :)
Also, since I've started recording me solving these, you can check it out here: youtube.com/watch?v=ozws2mzhqkM