19 yo student *and* a full-time developer.
Building with Rails currently, exploring new frontiers.
I enjoy functional programming, Linux, FOSS, refactoring and mentoring :)
19 yo student *and* a full-time developer.
Building with Rails currently, exploring new frontiers.
I enjoy functional programming, Linux, FOSS, refactoring and mentoring :)
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.
fncentury_name(year:u32)->String{letcentury=(year/100)+1;format!("{}{}",century,matchcentury%10{_ifcentury>10&¢ury<14=>"th",// teen numbers exception1=>"st",2=>"nd",3=>"rd",_=>"th",})}
packagecenturyimport("strconv")// Century gives the text representation of what century the given date belongs tofuncCentury(dateint)string{// if date is negative (BC), convert to positiveifdate<0{date*=-1}prefix:=date/100+1varsuffixstring// if its one the weird teens centuries, suffix is "th"switchprefix%100{case11,12,13:suffix="th"default:switchprefix%10{case1:suffix="st"case2:suffix="nd"case3:suffix="rd"default:suffix="th"}}returnstrconv.Itoa(prefix)+suffix}
century_test.go
packagecenturyimport"testing"funcTestCentury(t*testing.T){testCases:=[]struct{descriptionstringinputintexpectedstring}{{"twenty third centry",2259,"23rd",},{"twelfth century",1124,"12th",},{"twenty first century",2000,"21st",},{"small centry",24,"1st",},{"odd centry name",1013,"11th",},{"large odd century name",11013,"111th",},{"negative century",-2000,"21st",},}for_,test:=rangetestCases{ifresult:=Century(test.input);result!=test.expected{t.Fatalf("FAIL: %s - Centry(%d): %s - expected '%s'",test.description,test.input,result,test.expected)}t.Logf("PASS: %s",test.description)}}
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 :)
19 yo student *and* a full-time developer.
Building with Rails currently, exploring new frontiers.
I enjoy functional programming, Linux, FOSS, refactoring and mentoring :)
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.
python
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
A length Javascript solution, but I did not want to divide by 100.
Tried it with a few different years including the edge cases.
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.
Oh, good catch
It's fixed now (I think). It should also support BC centuries.
I used your answer as a cheatsheet of sorts. Good job! :)
Thanks
2000 must 20th century
Link: en.wikipedia.org/wiki/20th_century