Skip to content

Instantly share code, notes, and snippets.

@lingoslinger
Created April 1, 2024 23:51
Show Gist options
  • Save lingoslinger/de916e545ca2c8ecbdce60f423ae279e to your computer and use it in GitHub Desktop.
Save lingoslinger/de916e545ca2c8ecbdce60f423ae279e to your computer and use it in GitHub Desktop.
Given an array of integers: find the first value with the largest character count when spelled out
// I publicly share any answers to "coding challenges" I receive as a part of the interview process
// use this in an Xcode playground or the code challenge repl of choice
// Given an array of integers: find the first value with the largest character count when spelled out
// format the result like this example: "one hundred seventy-eight (178)
import UIKit
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .spellOut // TIL this exists...
let testArray = Array(0...200)
func largestCharCount(integers: [Int]) -> String {
var largestInt = 0
var largestString = ""
for i in integers {
let testString = numberFormatter.string(from: NSNumber(value: i)) ?? "not a number"
if testString.count > largestString.count {
largestInt = i
largestString = testString
}
}
return "\(largestString) (\(largestInt))"
}
print ("Initial problem: \(largestCharCount(integers: testArray))")
print("")
// If there are multiple values with the same max character count, return the value that is alphabetically
// first (for ["forty one", "sixty two"] return "forty-one (41)"
func largestCharCountAlpha(integers: [Int]) -> String {
var largestInt = 0
var largestString = ""
for i in integers {
let testString = numberFormatter.string(from: NSNumber(value: i)) ?? "not a number"
if testString.count > largestString.count {
largestInt = i
largestString = testString
} else if testString.count == largestString.count {
if testString < largestString {
largestString = testString
largestInt = i
}
}
}
return "\(largestString) (\(largestInt))"
}
print("Alphabetically sorted: \(largestCharCountAlpha(integers: testArray))")
print("")
// extra extra credit - use a language other than English to determine the character count
func largestCharacterCountLocale(integers: [Int], locale: Locale = Locale(identifier: "en-EN")) -> String {
numberFormatter.locale = locale
var largestInt = 0
var largestString = ""
for i in integers {
let testString = numberFormatter.string(from: NSNumber(value: i)) ?? "not a number"
if testString.count > largestString.count {
largestInt = i
largestString = testString
} else if testString.count == largestString.count {
if testString < largestString {
largestString = testString
largestInt = i
}
}
}
return "\(largestString) (\(largestInt))"
}
print("Alphabetically sorted in language other than english:")
print("Default language, English: \(largestCharacterCountLocale(integers: testArray))")
let languageArray = ["en-EN", "fr-FR", "de-DE", "it-IT", "es-ES"]
print("\nArray of languages:")
for lang in languageArray {
numberFormatter.locale = Locale(identifier: "en-EN")
let languageString = numberFormatter.locale.localizedString(forLanguageCode: lang) ?? "Not a language"
print("\(languageString): \(largestCharacterCountLocale(integers: testArray, locale: Locale(identifier: lang)))")
}
print("")
// the higher order function answers, this is what they are looking for
// initial
func largestCharCountMax(_ integers: [Int]) -> String {
func spelledOut(_ num: Int) -> String {
return numberFormatter.string(from: NSNumber(value: num)) ?? "Not a Number?"
}
if let maxNum = integers.max(by: {spelledOut($0).count < spelledOut($1).count}) {
return "\(spelledOut(maxNum)) (\(maxNum))"
}
return ""
}
numberFormatter.locale = Locale(identifier: "en-EN")
print ("Max function: \(largestCharCountMax(testArray))")
print("")
// extra credit 1
func largestCharCountMaxAlpha(_ integers: [Int]) -> String {
func spelledOut(_ num: Int) -> String {
return numberFormatter.string(from: NSNumber(value: num)) ?? ""
}
let alphaArray = integers.map{spelledOut($0)}
let maxLength = alphaArray.map{$0.count}.max()
let sortedWord = alphaArray.filter{$0.count == maxLength}.sorted().first ?? ""
let numberForWord = numberFormatter.number(from: sortedWord) ?? 0
return "\(sortedWord) (\(String(describing: numberForWord)))"
}
numberFormatter.locale = Locale(identifier: "en-EN")
print ("Max function alpha: \(largestCharCountMaxAlpha(testArray))")
print("")
// extra credit 2
func largestCharCountMaxAlphaLocale(_ integers: [Int], locale: Locale = Locale(identifier: "en-EN")) -> String {
func spelledOut(_ num: Int) -> String {
return numberFormatter.string(from: NSNumber(value: num)) ?? ""
}
numberFormatter.locale = locale
let alphaArray = integers.map{spelledOut($0)}
let maxLength = alphaArray.map{$0.count}.max()
let sortedWord = alphaArray.filter{$0.count == maxLength}.sorted().first ?? ""
let numberForWord = numberFormatter.number(from: sortedWord) ?? 0
return "\(sortedWord) (\(numberForWord))"
}
numberFormatter.locale = Locale(identifier: "en-EN")
print ("Max function alpha locale: \(largestCharCountMaxAlphaLocale(testArray, locale: Locale(identifier: "de-DE")))")
print("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment