Four is the number of letters in the ...: Difference between revisions

m (added that the "and" word isn't to be used when spelling an English word for a number.)
Line 165:
Word 10000000 is "thousand", with 8 letters. Length of sentence so far: 70995756
</pre>
 
=={{header|Julia}}==
The functions num2text and numtext2ordinal are from the "Spelling of ordinal numbers" task,
which uses code # from the "Number names" task, updated for Julia 1.0.<lang julia>using DataStructures # for deque
 
const seed = "Four is the number of letters in the first word of this sentence, "
const (word2, word3) = ("in", "the")
 
lettercount(w) = length(w) - length(collect(eachmatch(r"-", w)))
splits(txt) = [x.match for x in eachmatch(r"[\w\-]+", txt)]
todq(sentence) = (d = Deque{String}(); map(x->push!(d, x), splits(sentence)[2:end]); d)
 
struct CountLetters
seedsentence::String
words::Deque{String}
commasafter::Vector{Int}
CountLetters(s) = new(s, todq(s), [13])
CountLetters() = CountLetters(seed)
end
 
function Base.iterate(iter::CountLetters, state = (1, 5, ""))
if length(iter.words) < 1
return nothing
end
returnword = popfirst!(iter.words)
nextwordindex = state[1] + 1
wordlen = lettercount(returnword)
wordvec = vcat(num2text(wordlen), word2, word3, splits(numtext2ordinal(num2text(nextwordindex))))
map(x -> push!(iter.words, x), wordvec)
push!(iter.commasafter, length(iter.words))
added = length(returnword) + (nextwordindex in iter.commasafter ? 2 : 1)
(wordlen, (nextwordindex, state[2] + added, returnword))
end
 
Base.eltype(iter::CountLetters) = Int
 
function sumwords(iterations)
countlet = CountLetters()
iter_result = iterate(countlet)
itercount = 2
while iter_result != nothing
(wlen, state) = iter_result
if itercount == iterations
return state
end
iter_result = iterate(countlet, state)
itercount += 1
end
throw("Iteration failed on \"Four is the number\" task.")
end
 
for n in [2202, 1000, 10000, 100000, 1000000, 10000000]
(itercount, totalletters, lastword) = sumwords(n)
println("$n words -> $itercount iterations, $totalletters letters total, ",
"last word \"$lastword\" with $(length(lastword)) letters.")
end</lang> {{output}} <pre>
2202 words -> 2202 iterations, 14035 letters total, last word "ninety-ninth" with 12 letters.
1000 words -> 1000 iterations, 6290 letters total, last word "in" with 2 letters.
10000 words -> 10000 iterations, 64320 letters total, last word "in" with 2 letters.
100000 words -> 100000 iterations, 661369 letters total, last word "one" with 3 letters.
1000000 words -> 1000000 iterations, 7127541 letters total, last word "the" with 3 letters.
10000000 words -> 10000000 iterations, 71103026 letters total, last word "thousand" with 8 letters.
</pre>
 
 
=={{header|Kotlin}}==
4,102

edits