This repository was archived by the owner on Aug 22, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +66
-0
lines changed
Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change 1+ package main
2+
3+ import "strconv"
4+
5+ func NumberToOrdinal (number int ) string {
6+ if number == 0 {
7+ return "0"
8+ }
9+
10+ special := []int {11 , 12 , 13 }
11+ _ , found := Find (special , number % 100 )
12+
13+ ending := "th"
14+
15+ if ! found {
16+ switch number % 10 {
17+ case 1 :
18+ ending = "st"
19+ case 2 :
20+ ending = "nd"
21+ case 3 :
22+ ending = "rd"
23+ }
24+ }
25+
26+ return strconv .Itoa (number ) + ending
27+ }
28+
29+ func Find (slice []int , val int ) (int , bool ) {
30+ for i , item := range slice {
31+ if item == val {
32+ return i , true
33+ }
34+ }
35+ return - 1 , false
36+ }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "reflect"
5+ "testing"
6+ )
7+
8+ func TestNumberToOrdinal (t * testing.T ) {
9+ type test struct {
10+ input int
11+ want string
12+ }
13+
14+ tests := []test {
15+ {input : 1 , want : "1st" },
16+ {input : 2 , want : "2nd" },
17+ {input : 3 , want : "3rd" },
18+ {input : 4 , want : "4th" },
19+ {input : 11 , want : "11th" },
20+ {input : 21 , want : "21st" },
21+ {input : 22 , want : "22nd" },
22+ }
23+
24+ for _ , tc := range tests {
25+ got := NumberToOrdinal (tc .input )
26+ if ! reflect .DeepEqual (tc .want , got ) {
27+ t .Fatalf ("expected: %v, got: %v" , tc .want , got )
28+ }
29+ }
30+ }
You can’t perform that action at this time.
0 commit comments