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

Content added Content deleted
(promoted draft task to task.)
Line 419: Line 419:


Ten millionth word, 'thousand', has 8 characters. 70995729 characters in the sentence, up to and including this word.</pre>
Ten millionth word, 'thousand', has 8 characters. 70995729 characters in the sentence, up to and including this word.</pre>

=={{header|Phix}}==
<lang Phix>include demo\rosetta\number_names.exw

-- as per Spelling_of_ordinal_numbers#Phix:
constant {irregs,ordinals} = columnize({{"one","first"},
{"two","second"},
{"three","third"},
{"five","fifth"},
{"eight","eighth"},
{"nine","ninth"},
{"twelve","twelfth"}})
function ordinal(string s)
integer i
for i=length(s) to 1 by -1 do
integer ch = s[i]
if ch=' ' or ch='-' then exit end if
end for
integer k = find(s[i+1..$],irregs)
if k then
s = s[1..i]&ordinals[k]
elsif s[$]='y' then
s[$..$] = "ieth"
else
s &= "th"
end if
return s
end function
--/copy of Spelling_or_ordinal_numers#Phix

function countLetters(string s)
integer res = 0
for i=1 to length(s) do
integer ch = s[i]
if (ch>='A' and ch<='Z')
or (ch>='a' and ch<='z') then
res += 1
end if
end for
return res
end function

sequence words = split("Four is the number of letters in the first word of this sentence,")
integer fi = 1

function kill_and(sequence s)
--grr...
for i=length(s) to 1 by -1 do
if s[i] = "and" then
s[i..i] = {}
end if
end for
return s
end function

function WordLen(integer w)
-- Returns the w'th word and its length (only counting letters).
while length(words)<w do
fi += 1
integer n = countLetters(words[fi])
sequence ns = kill_and(split(spell(n)))
sequence os = kill_and(split(ordinal(spell(fi)) & ","))
-- append eg {"two","in","the","second,"}
words &= ns&{"in","the"}&os
end while
string word = words[w]
return {word, countLetters(word)}
end function

function TotalLength()
-- Returns the total number of characters (including blanks,
-- commas, and punctuation) of the sentence so far constructed.
integer res = 0
for i=1 to length(words) do
res += length(words[i])+1
end for
return res
end function

procedure main()
integer i,n
string w
printf(1,"The lengths of the first 201 words are:\n")
for i=1 to 201 do
if mod(i,25)==1 then
printf(1,"\n%3d: ", i)
end if
{?,n} = WordLen(i)
printf(1," %2d", n)
end for
printf(1,"\nLength of sentence so far:%d\n", TotalLength())
for p=3 to 7 do
i = power(10,p)
{w, n} = WordLen(i)
printf(1,"Word %8d is \"%s\", with %d letters.", {i, w, n})
printf(1," Length of sentence so far:%d\n", TotalLength())
end for
end procedure
main()</lang>
{{out}}
<pre>
The lengths of the first 201 words are:

1: 4 2 3 6 2 7 2 3 5 4 2 4 8 3 2 3 6 5 2 3 5 3 2 3 6
26: 3 2 3 5 5 2 3 5 3 2 3 7 5 2 3 6 4 2 3 5 4 2 3 5 3
51: 2 3 8 4 2 3 7 5 2 3 10 5 2 3 10 3 2 3 9 5 2 3 9 3 2
76: 3 11 4 2 3 10 3 2 3 10 5 2 3 9 4 2 3 11 5 2 3 12 3 2 3
101: 11 5 2 3 12 3 2 3 11 5 2 3 11 3 2 3 13 5 2 3 12 4 2 3 11
126: 4 2 3 9 3 2 3 11 5 2 3 12 4 2 3 11 5 2 3 12 3 2 3 11 5
151: 2 3 11 5 2 3 13 4 2 3 12 3 2 3 11 5 2 3 8 3 2 3 10 4 2
176: 3 11 3 2 3 10 5 2 3 11 4 2 3 10 4 2 3 10 3 2 3 12 5 2 3
201: 11
Length of sentence so far:1204
Word 1000 is "in", with 2 letters. Length of sentence so far:6280
Word 10000 is "in", with 2 letters. Length of sentence so far:64692
Word 100000 is "one", with 3 letters. Length of sentence so far:671578
Word 1000000 is "the", with 3 letters. Length of sentence so far:7235383
Word 10000000 is "thousand,", with 8 letters. Length of sentence so far:72079160
</pre>


=={{header|REXX}}==
=={{header|REXX}}==