How to resolve the algorithm Four is the number of letters in the ... step by step in the Kotlin programming language
How to resolve the algorithm Four is the number of letters in the ... step by step in the Kotlin programming language
Table of Contents
Problem Statement
The Four is ... sequence is based on the counting of the number of letters in the words of the (never─ending) sentence:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Four is the number of letters in the ... step by step in the Kotlin programming language
The code snippet is written in Kotlin and it's a program that generates a sentence of a specified length by converting numbers to words.
The program receives a number n
as input and generates a sentence with n
words. The first word of the sentence is "Four" and the remaining words are generated by converting the length of the previous word to a word. For example, the second word is the length of the word "Four" which is four so the second word is "four", the third word is the length of the word "four" which is four so the third word is "four", and so on.
The program uses two maps, names
and bigNames
, to convert numbers to words. The names
map contains the names of numbers from one to twenty, and the bigNames
map contains the names of large numbers such as thousand, million, and billion.
The program also uses an irregOrdinals
map to convert the names of certain numbers to their ordinal form. For example, the name "one" is converted to "first", the name "two" is converted to "second", and so on.
The numToText
function converts a number to a word. It first checks if the number is zero, and if it is, it returns the string "zero". If the number is negative, the function negates it and sets a flag to indicate that the number is negative. The function then splits the number into groups of three digits from the right and stores the groups in an array. The function then calls the threeDigitsToText
function to convert each group of three digits to a word. The function then joins the words together to form the final word. If the number is negative, the function prepends the word "minus" to the final word.
The threeDigitsToText
function converts a group of three digits to a word. It first checks if the group of three digits is zero, and if it is, it returns an empty string. The function then splits the group of three digits into hundreds, tens, and units. The function then uses the names
map to convert the hundreds, tens, and units to words. The function then joins the words together to form the final word.
The getWords
function generates a list of words for a given number. It first adds the opening words "Four is the number of letters in the first word of this sentence," to the list. It then adds the words that are generated by converting the length of the previous word to a word. The function stops adding words when the list has reached the desired number of words.
The getLengths
function returns a pair of lists. The first list contains the lengths of the first n
words, and the second list contains the length of the sentence. The function first calls the getWords
function to generate a list of words. It then calculates the length of each word and adds it to the first list. The function then calculates the length of the sentence by adding the length of each word and the number of spaces and commas.
The getLastWord
function returns a triple of values. The first value is the last word in the sentence, the second value is the length of the last word, and the third value is the length of the sentence. The function first calls the getWords
function to generate a list of words. It then returns the last word in the list, the length of the last word, and the length of the sentence.
The main
function is the entry point of the program. It sets the initial value of n
to 201 and calls the getLengths
function to generate the lengths of the first 201 words. It then prints the lengths of the words to the console. It then sets the value of n
to 1000 and enters a loop. In the loop, it calls the getLastWord
function to get the last word, length of the last word, and length of the sentence. It then prints the last word, length of the last word, and length of the sentence to the console. The loop continues until n
reaches 10,000,000.
Source code in the kotlin programming language
// version 1.1.4-3
val names = mapOf(
1 to "one",
2 to "two",
3 to "three",
4 to "four",
5 to "five",
6 to "six",
7 to "seven",
8 to "eight",
9 to "nine",
10 to "ten",
11 to "eleven",
12 to "twelve",
13 to "thirteen",
14 to "fourteen",
15 to "fifteen",
16 to "sixteen",
17 to "seventeen",
18 to "eighteen",
19 to "nineteen",
20 to "twenty",
30 to "thirty",
40 to "forty",
50 to "fifty",
60 to "sixty",
70 to "seventy",
80 to "eighty",
90 to "ninety"
)
val bigNames = mapOf(
1_000L to "thousand",
1_000_000L to "million",
1_000_000_000L to "billion",
1_000_000_000_000L to "trillion",
1_000_000_000_000_000L to "quadrillion",
1_000_000_000_000_000_000L to "quintillion"
)
val irregOrdinals = mapOf(
"one" to "first",
"two" to "second",
"three" to "third",
"five" to "fifth",
"eight" to "eighth",
"nine" to "ninth",
"twelve" to "twelfth"
)
fun String.toOrdinal(): String {
if (this == "zero") return "zeroth" // or alternatively 'zeroeth'
val splits = this.split(' ', '-')
val last = splits[splits.lastIndex]
return if (irregOrdinals.containsKey(last)) this.dropLast(last.length) + irregOrdinals[last]!!
else if (last.endsWith("y")) this.dropLast(1) + "ieth"
else this + "th"
}
fun numToText(n: Long, uk: Boolean = false): String {
if (n == 0L) return "zero"
val neg = n < 0L
val maxNeg = n == Long.MIN_VALUE
var nn = if (maxNeg) -(n + 1) else if (neg) -n else n
val digits3 = IntArray(7)
for (i in 0..6) { // split number into groups of 3 digits from the right
digits3[i] = (nn % 1000).toInt()
nn /= 1000
}
fun threeDigitsToText(number: Int) : String {
val sb = StringBuilder()
if (number == 0) return ""
val hundreds = number / 100
val remainder = number % 100
if (hundreds > 0) {
sb.append(names[hundreds], " hundred")
if (remainder > 0) sb.append(if (uk) " and " else " ")
}
if (remainder > 0) {
val tens = remainder / 10
val units = remainder % 10
if (tens > 1) {
sb.append(names[tens * 10])
if (units > 0) sb.append("-", names[units])
}
else sb.append(names[remainder])
}
return sb.toString()
}
val strings = Array(7) { threeDigitsToText(digits3[it]) }
var text = strings[0]
var andNeeded = uk && digits3[0] in 1..99
var big = 1000L
for (i in 1..6) {
if (digits3[i] > 0) {
var text2 = strings[i] + " " + bigNames[big]
if (text.isNotEmpty()) {
text2 += if (andNeeded) " and " else " " // no commas inserted in this version
andNeeded = false
}
else andNeeded = uk && digits3[i] in 1..99
text = text2 + text
}
big *= 1000
}
if (maxNeg) text = text.dropLast(5) + "eight"
if (neg) text = "minus " + text
return text
}
val opening = "Four is the number of letters in the first word of this sentence,".split(' ')
val String.adjustedLength get() = this.replace(",", "").replace("-", "").length // no ',' or '-'
fun getWords(n: Int): List<String> {
val words = mutableListOf<String>()
words.addAll(opening)
if (n > opening.size) {
var k = 2
while (true) {
val len = words[k - 1].adjustedLength
val text = numToText(len.toLong())
val splits = text.split(' ')
words.addAll(splits)
words.add("in")
words.add("the")
val text2 = numToText(k.toLong()).toOrdinal() + "," // add trailing comma
val splits2 = text2.split(' ')
words.addAll(splits2)
if (words.size >= n) break
k++
}
}
return words
}
fun getLengths(n: Int): Pair<List<Int>, Int> {
val words = getWords(n)
val lengths = words.take(n).map { it.adjustedLength }
val sentenceLength = words.sumBy { it.length } + words.size - 1 // includes hyphens, commas & spaces
return Pair(lengths, sentenceLength)
}
fun getLastWord(n: Int): Triple<String, Int, Int> {
val words = getWords(n)
val nthWord = words[n - 1]
val nthWordLength = nthWord.adjustedLength
val sentenceLength = words.sumBy { it.length } + words.size - 1 // includes hyphens, commas & spaces
return Triple(nthWord, nthWordLength, sentenceLength)
}
fun main(args: Array<String>) {
var n = 201
println("The lengths of the first $n words are:\n")
val (list, sentenceLength) = getLengths(n)
for (i in 0 until n) {
if (i % 25 == 0) {
if (i > 0) println()
print("${"%3d".format(i + 1)}: ")
}
print("%3d".format(list[i]))
}
println("\n\nLength of sentence = $sentenceLength\n")
n = 1_000
do {
var (word, wLen, sLen) = getLastWord(n)
if (word.endsWith(",")) word = word.dropLast(1) // strip off any trailing comma
println("The length of word $n [$word] is $wLen")
println("Length of sentence = $sLen\n")
n *= 10
}
while (n <= 10_000_000)
}
You may also check:How to resolve the algorithm Write entire file step by step in the SenseTalk programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the Ela programming language
You may also check:How to resolve the algorithm Halt and catch fire step by step in the Factor programming language
You may also check:How to resolve the algorithm Draw a clock step by step in the ContextFree programming language
You may also check:How to resolve the algorithm Password generator step by step in the JavaScript programming language