fork download
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. )
  8.  
  9. func main() {
  10. fmt.Println(TalkingClock("00:00"))
  11. fmt.Println(TalkingClock("01:30"))
  12. fmt.Println(TalkingClock("12:05"))
  13. fmt.Println(TalkingClock("14:01"))
  14. fmt.Println(TalkingClock("20:29"))
  15. fmt.Println(TalkingClock("21:00"))
  16. }
  17.  
  18. func TalkingClock(time string) string {
  19. hour, minute := UnpackPair(time, ":")
  20.  
  21. period := "am"
  22. if i, _ := strconv.Atoi(hour); i >= 12 {
  23. hour = strconv.Itoa(i - 12)
  24. period = "pm"
  25. }
  26.  
  27. if minute == "00" {
  28. return "It's " + HourToWords(hour) + " " + period
  29. }
  30.  
  31. return "It's " + HourToWords(hour) + " " + MinuteToWords(minute) + " " + period
  32. }
  33.  
  34. func oneToTen(n string) string {
  35. i, _ := strconv.Atoi(n)
  36. switch i {
  37. case 1:
  38. return "one"
  39. case 2:
  40. return "two"
  41. case 3:
  42. return "three"
  43. case 4:
  44. return "four"
  45. case 5:
  46. return "five"
  47. case 6:
  48. return "six"
  49. case 7:
  50. return "seven"
  51. case 8:
  52. return "eight"
  53. case 9:
  54. return "nine"
  55. default:
  56. return "ten"
  57. }
  58. }
  59.  
  60. func HourToWords(hour string) string {
  61. i, _ := strconv.Atoi(hour)
  62. switch i {
  63. case 0:
  64. return "twelve"
  65. case 11:
  66. return "eleven"
  67. default:
  68. return oneToTen(hour)
  69. }
  70. }
  71.  
  72. func MinuteToWords(minute string) string {
  73. if minute == "00" {
  74. return ""
  75. }
  76.  
  77. if minute == "10" {
  78. return "ten"
  79. }
  80.  
  81. imin, _ := strconv.Atoi(minute)
  82.  
  83. if imin < 10 {
  84. return "oh " + oneToTen(minute)
  85. }
  86.  
  87. if imin > 10 && imin < 16 {
  88. switch imin {
  89. case 11:
  90. return "eleven"
  91. case 12:
  92. return "twelve"
  93. case 13:
  94. return "thirteen"
  95. case 14:
  96. return "fourteen"
  97. default:
  98. return "fifteen"
  99. }
  100. }
  101.  
  102. tens, ones := UnpackPair(minute, "")
  103.  
  104. if imin < 20 {
  105. return oneToTen(ones) + "teen"
  106. }
  107.  
  108. itens, _ := strconv.Atoi(tens)
  109. switch itens {
  110. case 2:
  111. tens = "twenty"
  112. case 3:
  113. tens = "thirty"
  114. case 4:
  115. tens = "fourty"
  116. default:
  117. tens = "fifty"
  118. }
  119.  
  120. if i, _ := strconv.Atoi(ones); i == 0 {
  121. return tens
  122. }
  123.  
  124. return tens + " " + oneToTen(ones)
  125. }
  126.  
  127. func UnpackPair(str string, sep string) (string, string) {
  128. split := strings.SplitN(str, sep, 2)
  129. if len(split) == 1 {
  130. return split[0], ""
  131. }
  132. return split[0], split[1]
  133. }
Success #stdin #stdout 0s 3088KB
stdin
Standard input is empty
stdout
It's twelve am
It's one thirty am
It's twelve oh five pm
It's two oh one pm
It's eight twenty nine pm
It's nine pm