Traversing such words is much like increasing a number of base 26. You can make use of the String(.., radix: )
initializer to achieve this:
extension Character {
var unicodeScalarsValue: UInt32 {
return String(self).unicodeScalars.first!.value
}
}
func printWordSequence(numWords: Int) {
for i in 0..<numWords {
print(String(i, radix: 26).characters.map({
char -> String in
let a = char.unicodeScalarsValue
if a < 58 {
return String(UnicodeScalar(a + 49))
}
else {
return String(UnicodeScalar(a + 10))
}
}).joinWithSeparator(""))
}
}
printWordSequence(3000)
/*
a
b
c
...
elh
eli
elj */
Where the Character
extension is taken from Leo Dabus answer in thread 'What's the simplest way to convert from a single character String to an ASCII value in Swift?'.
If you'd rather want to save the words in array (for later printing or use), modify the function above slightly as:
func generateWordSequence(numWords: Int) -> [String] {
var myArr : [String] = []
for i in 0..<numWords {
myArr.append(String(i, radix: 26).characters.map({
char -> String in
let a = char.unicodeScalarsValue
if a < 58 {
return String(UnicodeScalar(a + 49))
}
else {
return String(UnicodeScalar(a + 10))
}
}).joinWithSeparator(""))
}
return myArr
}
var myWordSequence = generateWordSequence(3000)
myWordSequence.map { print($0) }
/* same as above ... */