Four is the number of letters in the ...: Difference between revisions
Four is the number of letters in the ... (view source)
Revision as of 10:39, 6 December 2023
, 5 months ago→{{header|Wren}}: Minor tidy
m (→{{header|Phix}}: added syntax colouring, made p2js compatible) |
m (→{{header|Wren}}: Minor tidy) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 14:
:* '''twenty─three''' is considered one word (which is hyphenated).
:* no <big>''' ''and'' '''</big> words are to be used when spelling a (English) word for a number.
:* The
'''2,000,000,000''' is two billion, ''not'' two milliard.
Line 52 ⟶ 53:
=={{header|C}}==
{{libheader|GLib}}
<
#include <locale.h>
#include <stdbool.h>
Line 254 ⟶ 255:
word_list_destroy(&result);
return 0;
}</
{{out}}
Line 277 ⟶ 278:
=={{header|C++}}==
<
#include <cstdint>
#include <iomanip>
Line 433 ⟶ 434:
}
return 0;
}</
{{out}}
Line 463 ⟶ 464:
[[Spelling_of_ordinal_numbers#Go|Spelling of ordinal numbers]] task
(omitted from this listing).
<
import (
Line 556 ⟶ 557:
// omitted from this listing
// ...
</syntaxhighlight>
{{out}}
<pre>The lengths of the first 201 words are:
Line 580 ⟶ 581:
Uses the solution [[Spelling_of_ordinal_numbers#Haskell]] and tying-the-knot technique to create the infinite sentence.
<
sentence = start ++ foldMap add (zip [2..] $ tail $ words sentence)
Line 596 ⟶ 597:
in "The " ++ spellOrdinal n ++ " word is \"" ++ w ++ "\" which has " ++
spellInteger (alphaLength w) ++ " letters. The sentence length is " ++
show (length $ unwords a) ++ " chars."</
<pre>λ> take 200 sentence
Line 610 ⟶ 611:
Take care with the requirements. As noted, numberToString(23) = twenty-three. Therefore numberToString(723423) = seven hundred twenty-three thousand four hundred twenty-three (note the two "-").
<br/>The discussion was helpful by providing the first 2202 words of the sentence.
<
import java.util.HashMap;
import java.util.Map;
Line 824 ⟶ 825:
}
</syntaxhighlight>
{{out}}
<pre>
Line 846 ⟶ 847:
=={{header|Julia}}==
The functions num2text and numtext2ordinal are from the "Spelling of ordinal numbers" and "Number names" tasks, updated for Julia 1.0 and to remove the "and" words.<
const seed = "Four is the number of letters in the first word of this sentence, "
Line 919 ⟶ 920:
println("$n words -> $itercount iterations, $totalletters letters total, ",
"last word \"$lastword\" with $(length(lastword)) letters.")
end</
It is interesting how identical lengths align with 20 columns.
1: 4 2 3 6 2 7 2 3 5 4 2 4 8 3 2 3 6 5 2 3
Line 942 ⟶ 943:
=={{header|Kotlin}}==
This pulls in (slightly adjusted) code from related tasks to convert numbers to text or ordinals.
<
val names = mapOf(
Line 1,117 ⟶ 1,118:
}
while (n <= 10_000_000)
}</
{{out}}
Line 1,152 ⟶ 1,153:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
(*Mathematica has a built-in function for getting the name of an integer. It's a semantically rich function (dealing with many languages and grammatical variants), and consequently it would slow down our algorithm significantly. So, I've used the built-in function to seed/memoize special-purpose functions. Furthermore, the problem is suited to using a representation of the sentence that is an array of strings rather than a single monolithic string, and so these integer name functions will return arrays of strings.*)
Line 1,237 ⟶ 1,238:
},
"\n\n"
]</
{{out}}
Line 1,256 ⟶ 1,257:
=={{header|Nim}}==
<
####################################################################################################
Line 1,417 ⟶ 1,418:
for n in [1_000, 10_000, 100_000, 1_000_000, 10_000_000]:
echo ""
displayWord(n)</
{{out}}
Line 1,458 ⟶ 1,459:
Uses <code>Lingua::EN::Numbers</code> module to generate number names. State variable in <tt>extend_to</tt> routine keeps track of last word tallied.
{{trans|Raku}}
<
use Lingua::EN::Numbers qw(num2en num2en_ordinal);
Line 1,489 ⟶ 1,490:
ucfirst(num2en_ordinal($_)) . " word, '$sentence[$_-1]' has " . alpha($sentence[$_-1]) . " characters. \n" .
count($_) . "\n";
}</
{{out}}
<pre>First 201 word lengths in the sequence:
Line 1,518 ⟶ 1,519:
=={{header|Phix}}==
Note that my version of [[Number_names]] includes "and" (and ","), that others do not, hence the kill_and()/grr below and the minor mismatch of sentence lengths.
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">demo</span><span style="color: #0000FF;">\</span><span style="color: #000000;">rosetta</span><span style="color: #0000FF;">\</span><span style="color: #000000;">number_names</span><span style="color: #0000FF;">.</span><span style="color: #000000;">exw</span> <span style="color: #000080;font-style:italic;">-- see note
Line 1,615 ⟶ 1,616:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
Note you will have to comment out main()..EOF in the number_names.exw file for pwa/p2js to accept it/not complain about multiple main() or #ilASM (which I may yet remove). Also as indicated 100K words is simply too much for your typical browser, let alone a million, so we limit the last part to 10K under pwa/p2js.
{{out}}
Line 1,639 ⟶ 1,640:
=={{header|Python}}==
<syntaxhighlight lang="python">
# Python implementation of Rosetta Code Task
# http://rosettacode.org/wiki/Four_is_the_number_of_letters_in_the_...
Line 1,824 ⟶ 1,825:
word_and_counts(1000000)
word_and_counts(10000000)
</syntaxhighlight>
Output:
Line 1,856 ⟶ 1,857:
Uses the Lingua::EN::Numbers module to generate both cardinal and ordinal numbers. This module places commas in number words between 3-orders-of-magnitude clusters. E.G. <code>12345678.&ordinal</code> becomes: twelve million, three hundred forty-five thousand, six hundred seventy-eighth. Uses a custom 'no-commas' routine to filter them out for accurate character counts. Generates the 'sentence' lazily so only the words needed are ever calculated and reified.
<syntaxhighlight lang="raku"
no-commas(True);
Line 1,872 ⟶ 1,873:
for 1e3, 1e4, 1e5, 1e6, 1e7 {
say "{.&ordinal.tc} word, '{@sentence[$_ - 1]}', has {@sentence[$_ - 1].&alpha} characters. ", .&count
}</
{{out}}
<pre>First 201 word lengths in the sequence:
Line 1,897 ⟶ 1,898:
=={{header|REXX}}==
<
@= 'Four is the number of letters in the first word of this sentence,' /*···*/
/* [↑] the start of a long sentence. */
Line 1,939 ⟶ 1,940:
if $\=='' & z>0 then say right(idx, w)'►'$ /*display if there are residual numbers*/
return</
The '''$SPELL#.REX''' routine can be found here ───► [[$SPELL.REX|$SPELL#.REX]]. <br><br>
Line 1,971 ⟶ 1,972:
=={{header|Rust}}==
{{trans|C}}
<
cardinal: &'static str,
ordinal: &'static str,
Line 2,308 ⟶ 2,309:
n *= 10;
}
}</
{{out}}
Line 2,333 ⟶ 2,334:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<
var names = {
Line 2,513 ⟶ 2,514:
n = n * 10
if (n > 1e7) break
}</
{{out}}
Line 2,549 ⟶ 2,550:
=={{header|zkl}}==
Uses the nth function from [[Spelling_of_ordinal_numbers#zkl]]
<
// Save the word counts
fcn fourIsThe(text,numWords){
Line 2,576 ⟶ 2,577:
return(lastWords.strip(),szs,total);
}
fcn lastWord(sentence){ sentence[sentence.rfind(" ")+1,*] }</
<
sentence,szs,total := fourIsThe(seed,201);
print(" 1:");
Line 2,584 ⟶ 2,585:
if(0 == n%25) print("\n%3d:".fmt(n+1));
}
println("\nLength of above sentence: ",total);</
{{out}}
<pre>
Line 2,598 ⟶ 2,599:
Length of above sentence: 1203
</pre>
<
sentence,x,total := fourIsThe(seed,n);
word:=lastWord(sentence);
Line 2,604 ⟶ 2,605:
.fmt(n,word,word.len(),total));
n*=10;
}</
{{out}}
<pre>
|