2022-02-10 22:06:57 +00:00
|
|
|
//go:build !solution
|
2021-02-18 16:00:15 +00:00
|
|
|
|
|
|
|
package speller
|
|
|
|
|
2024-06-04 00:24:20 +00:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-06-04 14:26:02 +00:00
|
|
|
var digits = []string{
|
2024-06-04 00:24:20 +00:00
|
|
|
"", "one", "two", "three", "four", "five",
|
|
|
|
"six", "seven", "eight", "nine",
|
|
|
|
}
|
|
|
|
|
|
|
|
var teens = []string{
|
|
|
|
"ten", "eleven", "twelve", "thirteen", "fourteen",
|
|
|
|
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
|
|
|
|
}
|
|
|
|
|
|
|
|
var decs = []string{
|
|
|
|
"", "", "twenty", "thirty", "forty", "fifty",
|
|
|
|
"sixty", "seventy", "eighty", "ninety",
|
|
|
|
}
|
|
|
|
|
|
|
|
var large = []string{
|
|
|
|
"billion", "million", "thousand",
|
|
|
|
}
|
|
|
|
|
|
|
|
func digitsReversedSlice(n int64) []uint8 {
|
|
|
|
digits := make([]uint8, 12)
|
|
|
|
for i := 0; i < len(digits); i, n = i+1, n/10 {
|
|
|
|
digits[i] = uint8(n % 10)
|
|
|
|
}
|
|
|
|
return digits
|
|
|
|
}
|
|
|
|
|
2021-02-18 16:00:15 +00:00
|
|
|
func Spell(n int64) string {
|
2024-06-04 00:24:20 +00:00
|
|
|
if n == 0 {
|
|
|
|
return "zero"
|
|
|
|
}
|
|
|
|
sign := n < 0
|
|
|
|
if sign {
|
|
|
|
n = -n
|
|
|
|
}
|
2024-06-04 14:26:02 +00:00
|
|
|
num, numDigits := strings.Builder{}, digitsReversedSlice(n)
|
2024-06-04 00:24:20 +00:00
|
|
|
// 35 is the optimal number which gives approx needed
|
|
|
|
// space for the number in 1 allocation
|
2024-06-04 14:26:02 +00:00
|
|
|
num.Grow(len(numDigits) * 35)
|
2024-06-04 00:24:20 +00:00
|
|
|
if sign {
|
|
|
|
num.WriteString("minus ")
|
|
|
|
}
|
2024-06-04 14:26:02 +00:00
|
|
|
for i, largeIndex := len(numDigits)-1, 0; i >= 0; i, largeIndex = i-3, largeIndex+1 {
|
|
|
|
hundreds, tens, ones := numDigits[i], numDigits[i-1], numDigits[i-2]
|
|
|
|
hundredsPresent, tensPesent, onesPresent := hundreds != 0, tens != 0, ones != 0
|
|
|
|
if !hundredsPresent && !tensPesent && !onesPresent {
|
2024-06-04 00:24:20 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-06-04 14:26:02 +00:00
|
|
|
if hundredsPresent {
|
|
|
|
num.WriteString(digits[hundreds])
|
2024-06-04 00:24:20 +00:00
|
|
|
num.WriteString(" hundred")
|
2024-06-04 14:26:02 +00:00
|
|
|
if tensPesent || onesPresent {
|
2024-06-04 00:24:20 +00:00
|
|
|
num.WriteRune(' ')
|
|
|
|
}
|
|
|
|
}
|
2024-06-04 14:26:02 +00:00
|
|
|
if tensPesent {
|
|
|
|
if tens == 1 {
|
|
|
|
num.WriteString(teens[ones])
|
|
|
|
} else {
|
|
|
|
num.WriteString(decs[tens])
|
|
|
|
if onesPresent {
|
|
|
|
num.WriteRune('-')
|
|
|
|
}
|
2024-06-04 00:24:20 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-04 14:26:02 +00:00
|
|
|
if onesPresent && tens != 1 {
|
|
|
|
num.WriteString(digits[ones])
|
|
|
|
}
|
2024-06-04 00:24:20 +00:00
|
|
|
if largeIndex < len(large) {
|
|
|
|
num.WriteRune(' ')
|
|
|
|
num.WriteString(large[largeIndex])
|
|
|
|
}
|
2024-06-04 14:26:02 +00:00
|
|
|
num.WriteRune(' ')
|
2024-06-04 00:24:20 +00:00
|
|
|
}
|
2024-06-04 14:26:02 +00:00
|
|
|
output := num.String()
|
|
|
|
output = output[:len(output)-1]
|
|
|
|
return output
|
2021-02-18 16:00:15 +00:00
|
|
|
}
|