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)}}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Insert Go Pun Here
century.go
century_test.go