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 ?
"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"
Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
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 of Math.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...
// Stringifying the ordinal is an easier way to get individual the parts of the year...functionsuffixForOrdinal_slowButSure(ordinal:number):string{constordinalString=String(ordinal),tens=+ordinalString.slice(-2,-1),ones=+ordinalString.slice(-1);if(tens==1)return'th';// i.e. 11th, not 11stif(ones==1)return'st';// 81stif(ones==2)return'nd';// 82ndif(ones==3)return'rd';// 83rdreturn'th';// anything else is 'th'. 45th}functionnameOfCentury_slowButSure(yearOrdinal:string|number):string{// When extracting the century we need to be working with the index, not ordinal.// That's why the weirdness about 2000 -> 20th, 2001 -> 21st// (The year 2000 as an ordinal has index 1999)constyearIndex=+yearOrdinal-1,yearIndexString=String(yearIndex),centuryIndex=+yearIndexString.slice(0,-2)||0,centuryOrdinal=centuryIndex+1;return`${centuryOrdinal}${suffixForOrdinal_slowButSure(centuryOrdinal)}`;}// Now do some 'testing' by comparing the implementations// This ensures that either both are right or both are wrong,// I'm pretty sure that the verbose one is as right as any static fixtures I could make.(functionhareVsTortoise(startYearOrdinal=1,endYearOrdinal=100000){for(letyearOrdinal=startYearOrdinal;yearOrdinal<=endYearOrdinal;yearOrdinal++){if(nameOfCentury(yearOrdinal)!==nameOfCentury_slowButSure(yearOrdinal)){console.error(`Conflicting answers for year ${yearOrdinal}:\n Hare: "${nameOfCentury(yearOrdinal)}"\n Tortoise: "${nameOfCentury_slowButSure(yearOrdinal)}"`);return;}}console.log(`Checked ${endYearOrdinal+1-startYearOrdinal} years, all seems good`);})();/*-->
Checked 100000 years, all seems good
[1,99,100,101,200,300,400,1000,1100,1200,1300,1400,1900,2000,2001,2020,10000,10100].forEach(year=>{console.log(`Year ${year} --> ${nameOfCentury(year)}`);});/*-->
Year 1 --> 1st
Year 99 --> 1st
Year 100 --> 1st
Year 101 --> 2nd
Year 200 --> 2nd
Year 300 --> 3rd
Year 400 --> 4th
Year 1000 --> 10th
Year 1100 --> 11th
Year 1200 --> 12th
Year 1300 --> 13th
Year 1400 --> 14th
Year 1900 --> 19th
Year 2000 --> 20th
Year 2001 --> 21st
Year 2020 --> 21st
Year 10000 --> 100th
Year 10100 --> 101st
This might be a bit over-engineered, but here is another Ruby solution:
defto_century(year)(year.to_i/100+1).english.ordinalendclassIntegerdefenglishEnglish::Integer.new(self)endendmoduleEnglishclassIntegerdefinitialize(integer)@value=integerend### Returns ordinal notation of the current integer ("-2nd", "1st", "312th").defordinal"#{@value}#{ordinal_suffix}"end### Returns the ordinal suffix of the current integer ("st", "nd", "rd", "th").defordinal_suffixcase@value.abs%100when1,21,31,41,51,61,71,81,91then'st'when2,22,32,42,52,62,72,82,92then'nd'when3,23,33,43,53,63,73,83,93then'rd'else'th'endendendend
defmoduleYeardo@doc"""
Returns the stringified, suffixed form of a numeric year.
Use `to_century/1` if you need to do further numeric work on the
century number before (or in lieu of) using it as a string.
## Examples
iex> Year.centurify(8120)
"82nd"
iex> Year.centurify(30200)
"303rd"
iex> Year.centurify(1601)
"17th"
iex> Year.centurify(2020)
"21st"
iex> Year.centurify(3030)
"31st"
iex> Year.centurify(1900)
"20th"
iex> Year.centurify(1776)
"18th"
"""defcenturify(year)docentury=to_century(year)"#{century}#{suffix(century)}"enddefto_century(year),do:div(year,100)+1defpsuffix(number)whennumber>=100,do:number|>rem(100)|>suffixdefpsuffix(number)whennumberin11..13,do:"th"defpsuffix(number)docaserem(number,10)do1->"st"2->"nd"3->"rd"_->"th"endendenddefmoduleYearTestdoimportExUnit.CasedoctestYearend
Clean and elegant!
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 ?
"1066 --> 11st" should be 11th
"10266 --> 103th" should be 103rd
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.floorbecause 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
activesupportgem installedThis might be a bit over-engineered, but here is another Ruby solution:
Which produces the following output:
Here is my simple solution with Python: