Last active
May 21, 2018 17:32
-
-
Save possen/47b5357923d1e4f0baec8dc80b26e6ab to your computer and use it in GitHub Desktop.
Print English Names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
func convertToEnglish(number: Int) -> (Int, String) { | |
let unitsMap = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] | |
let tensMap = ["zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] | |
let denom = [ "", " hundred", " thousand", "", " hundred", " million", "", " hundred", " billion"] | |
if number == 0 { | |
return (number, unitsMap[number]) | |
} | |
var digits: [Int] = [] | |
var num = number | |
while num > 0 { | |
digits += [num % 10] | |
num = num / 10 | |
} | |
func group(ones: Int, tens: Int) -> [String] { | |
let first2 = ones + (tens * 10) | |
var arr: [String] = [] | |
if first2 == 0 { | |
arr += [""] | |
} else if first2 < 20 { | |
arr += [unitsMap[first2]] | |
} else { | |
arr += [unitsMap[ones]] | |
arr += [tensMap[tens]] | |
} | |
return arr | |
} | |
digits = digits.reversed() | |
var resultArr: [String] = [] | |
repeat { | |
let ones = digits.popLast() ?? 0 | |
let tens = digits.popLast() ?? 0 | |
resultArr += group(ones: ones, tens: tens) | |
if let hundreds = digits.popLast() { | |
resultArr += [hundreds == 0 ? "" : unitsMap[hundreds]] | |
} | |
} while !digits.isEmpty | |
let mappedResults = zip(resultArr, denom) | |
let result = mappedResults.reversed().reduce("") { | |
$0 + ($1.0 == unitsMap[0] ? "" : $1.0) + ($1.0.isEmpty ? "" : $1.1) + " " | |
} | |
return (number, result.trimmingCharacters(in: .whitespaces)) | |
} | |
Swift.print(convertToEnglish(number: 54_443_000)) | |
Swift.print(convertToEnglish(number: 5_443_213)) | |
Swift.print(convertToEnglish(number: 5_443_000)) | |
Swift.print(convertToEnglish(number: 5_440_000)) | |
Swift.print(convertToEnglish(number: 429_100)) | |
Swift.print(convertToEnglish(number: 42_900)) | |
Swift.print(convertToEnglish(number: 42_200)) | |
Swift.print(convertToEnglish(number: 42_000)) | |
Swift.print(convertToEnglish(number: 2_414)) | |
Swift.print(convertToEnglish(number: 2_000)) | |
Swift.print(convertToEnglish(number: 110)) | |
Swift.print(convertToEnglish(number: 100)) | |
Swift.print(convertToEnglish(number: 114)) | |
Swift.print(convertToEnglish(number: 1)) | |
Swift.print(convertToEnglish(number: 0)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(54443000, "fifty four million four hundred forty three thousand")
(5443213, "five million four hundred forty three thousand two hundred thirteen")
(5443000, "five million four hundred forty three thousand")
(5440000, "five million four hundred forty thousand")
(429100, "four hundred twenty nine thousand one hundred")
(42900, "forty two thousand nine hundred")
(42200, "forty two thousand two hundred")
(42000, "forty two thousand")
(2414, "two thousand four hundred fourteen")
(2000, "two thousand")
(110, "one hundred ten")
(100, "one hundred")
(114, "one hundred fourteen")
(1, "one")
(0, "zero")