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

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: added syntax colouring, made p2js compatible)
m (syntax highlighting fixup automation)
Line 52:
=={{header|C}}==
{{libheader|GLib}}
<langsyntaxhighlight lang="c">#include <ctype.h>
#include <locale.h>
#include <stdbool.h>
Line 254:
word_list_destroy(&result);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 277:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <cctype>
#include <cstdint>
#include <iomanip>
Line 433:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 463:
[[Spelling_of_ordinal_numbers#Go|Spelling of ordinal numbers]] task
(omitted from this listing).
<langsyntaxhighlight Golang="go">package main
 
import (
Line 556:
// omitted from this listing
// ...
</syntaxhighlight>
</lang>
{{out}}
<pre>The lengths of the first 201 words are:
Line 580:
Uses the solution [[Spelling_of_ordinal_numbers#Haskell]] and tying-the-knot technique to create the infinite sentence.
 
<langsyntaxhighlight lang="haskell">import Data.Char
 
sentence = start ++ foldMap add (zip [2..] $ tail $ words sentence)
Line 596:
in "The " ++ spellOrdinal n ++ " word is \"" ++ w ++ "\" which has " ++
spellInteger (alphaLength w) ++ " letters. The sentence length is " ++
show (length $ unwords a) ++ " chars."</langsyntaxhighlight>
 
<pre>λ> take 200 sentence
Line 610:
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.
<langsyntaxhighlight lang="java">
import java.util.HashMap;
import java.util.Map;
Line 824:
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 846:
 
=={{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.<langsyntaxhighlight lang="julia">using DataStructures # for deque
 
const seed = "Four is the number of letters in the first word of this sentence, "
Line 919:
println("$n words -> $itercount iterations, $totalletters letters total, ",
"last word \"$lastword\" with $(length(lastword)) letters.")
end</langsyntaxhighlight> {{output}} <pre>
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:
=={{header|Kotlin}}==
This pulls in (slightly adjusted) code from related tasks to convert numbers to text or ordinals.
<langsyntaxhighlight lang="scala">// version 1.1.4-3
 
val names = mapOf(
Line 1,117:
}
while (n <= 10_000_000)
}</langsyntaxhighlight>
 
{{out}}
Line 1,152:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">(*==Number names==*)
 
(*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:
},
"\n\n"
]</langsyntaxhighlight>
 
{{out}}
Line 1,256:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils, strformat, tables
 
####################################################################################################
Line 1,417:
for n in [1_000, 10_000, 100_000, 1_000_000, 10_000_000]:
echo ""
displayWord(n)</langsyntaxhighlight>
 
{{out}}
Line 1,458:
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}}
<langsyntaxhighlight lang="perl">use feature 'state';
use Lingua::EN::Numbers qw(num2en num2en_ordinal);
 
Line 1,489:
ucfirst(num2en_ordinal($_)) . " word, '$sentence[$_-1]' has " . alpha($sentence[$_-1]) . " characters. \n" .
count($_) . "\n";
}</langsyntaxhighlight>
{{out}}
<pre>First 201 word lengths in the sequence:
Line 1,518:
=={{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.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
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:
 
=={{header|Python}}==
<syntaxhighlight lang="python">
<lang Python>
# Python implementation of Rosetta Code Task
# http://rosettacode.org/wiki/Four_is_the_number_of_letters_in_the_...
Line 1,824:
word_and_counts(1000000)
word_and_counts(10000000)
</syntaxhighlight>
</lang>
 
Output:
Line 1,856:
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" perl6line>use Lingua::EN::Numbers;
no-commas(True);
 
Line 1,872:
for 1e3, 1e4, 1e5, 1e6, 1e7 {
say "{.&ordinal.tc} word, '{@sentence[$_ - 1]}', has {@sentence[$_ - 1].&alpha} characters. ", .&count
}</langsyntaxhighlight>
{{out}}
<pre>First 201 word lengths in the sequence:
Line 1,897:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX pgm finds/shows the number of letters in the Nth word in a constructed sentence*/
@= 'Four is the number of letters in the first word of this sentence,' /*···*/
/* [↑] the start of a long sentence. */
Line 1,939:
 
if $\=='' & z>0 then say right(idx, w)'►'$ /*display if there are residual numbers*/
return</langsyntaxhighlight>
The &nbsp; '''$SPELL#.REX''' &nbsp; routine can be found here &nbsp; ───► &nbsp; [[$SPELL.REX|$SPELL#.REX]]. <br><br>
 
Line 1,971:
=={{header|Rust}}==
{{trans|C}}
<langsyntaxhighlight lang="rust">struct NumberNames {
cardinal: &'static str,
ordinal: &'static str,
Line 2,308:
n *= 10;
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,333:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var names = {
Line 2,513:
n = n * 10
if (n > 1e7) break
}</langsyntaxhighlight>
 
{{out}}
Line 2,549:
=={{header|zkl}}==
Uses the nth function from [[Spelling_of_ordinal_numbers#zkl]]
<langsyntaxhighlight lang="zkl"> // Built the sentence in little chucks but only save the last one
// Save the word counts
fcn fourIsThe(text,numWords){
Line 2,576:
return(lastWords.strip(),szs,total);
}
fcn lastWord(sentence){ sentence[sentence.rfind(" ")+1,*] }</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">var seed="Four is the number of letters in the first word of this sentence, ";
sentence,szs,total := fourIsThe(seed,201);
print(" 1:");
Line 2,584:
if(0 == n%25) print("\n%3d:".fmt(n+1));
}
println("\nLength of above sentence: ",total);</langsyntaxhighlight>
{{out}}
<pre>
Line 2,598:
Length of above sentence: 1203
</pre>
<langsyntaxhighlight lang="zkl">n:=1000; do(5){
sentence,x,total := fourIsThe(seed,n);
word:=lastWord(sentence);
Line 2,604:
.fmt(n,word,word.len(),total));
n*=10;
}</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits