English cardinal anagrams: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|Wren}}: Largest groups are now identified as such.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(18 intermediate revisions by 6 users not shown)
Line 15:
 
;Stretch
* Find and display the '''count''' of English cardinal anagrams from '''zero''' to '''ten thousand''', restricted to numbers within those bounds. ''(Ignore anagrams of numbers outside those bounds. '''E.G. 1010 <=> 10001''')''
* Find and display the '''largest group(s)''' of English cardinal anagrams from '''zero''' to '''ten thousand'''.
 
Line 23:
;* [[Number_names|Related task: Number names]]
 
 
=={{header|J}}==
Using the definitions at [[Number_names#J]] and
<syntaxhighlight lang=J>ukgram=: /:~@uk&.>
 
sample=: I.(e. ~. #~ 1<#/.~)ukgram i. 1e6
sam1e3=: I.(e. ~. #~ 1<#/.~)ukgram i. 1e3
sam1e4=: I.(e. ~. #~ 1<#/.~)ukgram i. 1e4
 
largegroups=: {{
tokens=. /:~@uk&.> y
utokens=. ~. tokens
ucounts=. #/.~ tokens
uids=. utokens #~ (=>./) ucounts
>}.y </.~ uids i. tokens
}}</syntaxhighlight>
 
We get:
 
<syntaxhighlight lang=J> 3 10$ sample
67 69 76 79 96 97 102 103 104 105
106 107 108 109 112 122 123 124 125 126
127 128 129 132 133 134 135 136 137 138
+/1000>:sample NB. count of values in OEIS::A169936 less than 1000
680
#~.ukgram (#~ 1000>:])sample NB. count of distinct anagrams for those values
317
largegroups (#~ 1000&>:) sample
679 697 769 796 967 976
</syntaxhighlight>
 
Stretch:
 
If we consider all values in values in [[OEIS:A169936]] which are less than 1e4
 
<syntaxhighlight lang=J> +/10000>:sample NB. distinct members in subsequence
9662
#~.ukgram (#~ 10000>:])sample NB. distinct anagrams in subsequence
2968
largegroups (#~ 10000&>:) sample NB. anagram groups with most members in subsequence
1679 1697 1769 1796 1967 1976 6179 6197 6791 6971 7169 7196 7691 7961 9167 9176 9671 9761
2679 2697 2769 2796 2967 2976 6279 6297 6792 6972 7269 7296 7692 7962 9267 9276 9672 9762
3679 3697 3769 3796 3967 3976 6379 6397 6793 6973 7369 7396 7693 7963 9367 9376 9673 9763
4679 4697 4769 4796 4967 4976 6479 6497 6794 6974 7469 7496 7694 7964 9467 9476 9674 9764
5679 5697 5769 5796 5967 5976 6579 6597 6795 6975 7569 7596 7695 7965 9567 9576 9675 9765
6798 6879 6897 6978 7698 7869 7896 7968 8679 8697 8769 8796 8967 8976 9678 9768 9867 9876
</syntaxhighlight>
 
If we explicitly exclude anagrams whose only partner is greater than 1e4, we get a slightly smaller subsequence:
 
<syntaxhighlight lang=J> #sam1e4
9228
#~.ukgram sam1e4
2534</syntaxhighlight>
 
Note however that for values less than 1000, every member of the [[OEIS:A169936]] sequence had a partner which was also less than 1000:
 
<syntaxhighlight lang=J> #sam1e3
680
+/1000>:sample
680</syntaxhighlight>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">import SpelledOut: spelled_out
import Counters: counter
 
languages = Dict("English" => :en, "Spanish" => :es, "Portuguese" => :pt_BR)
 
""" remove diacritical marks, as anagrams use only standard alphabet """
undiacritical(s) = replace(replace(replace(s, r"[éêé]" => "e"), "ã" => "a"), r"[óõ]" => "o")
 
""" get the letter frequencies as an anagram representation of string str """
function anarep(str)
s = undiacritical(lowercase(str))
return [count(c, s) for c in 'a':'z'] # ignores spaces, hyphens, capitalization
end
 
""" task at rosettacode.org/wiki/English_cardinal_anagrams """
function process_task(maxrange, showfirst30, lang = "English")
numstrings = map(n -> spelled_out(n, lang=languages[lang]), 0:maxrange)
numreps = map(anarep, numstrings)
anadict = counter(numreps)
counts = [anadict[numreps[i]] for i in 1:maxrange]
if showfirst30
println("First 30 $lang cardinal anagrams:")
i, printed = 1, 0
while i < maxrange && printed < 30
if counts[i] > 1
printed += 1
print(rpad(i, 4), printed % 10 == 0 ? "\n" : "")
end
i += 1
end
end
print("\nCount of $lang cardinal anagrams up to $maxrange: ")
println(count(values(anadict) .> 1))
println("\nLargest group(s) of $lang cardinal anagrams up to $maxrange:")
maxcount = maximum(counts)
for r in unique([numreps[i] for i in 1:maxrange if counts[i] == maxcount])
println(findall(==(r), numreps))
end
end
 
process_task(1000, true)
process_task(10000, false)
process_task(10000, false, "Spanish")
process_task(10000, false, "Portuguese")
</syntaxhighlight>{{out}}
<pre>
First 30 English cardinal anagrams:
67 69 76 79 96 97 102 103 104 105
106 107 108 109 112 122 123 124 125 126
127 128 129 132 133 134 135 136 137 138
 
Count of English cardinal anagrams up to 1000: 317
 
Largest group(s) of English cardinal anagrams up to 1000:
[679, 697, 769, 796, 967, 976]
 
Count of English cardinal anagrams up to 10000: 2534
 
Largest group(s) of English cardinal anagrams up to 10000:
[1679, 1697, 1769, 1796, 1967, 1976, 6179, 6197, 6791, 6971, 7169, 7196, 7691, 7961, 9167, 9176, 9671, 9761]
[2679, 2697, 2769, 2796, 2967, 2976, 6279, 6297, 6792, 6972, 7269, 7296, 7692, 7962, 9267, 9276, 9672, 9762]
[3679, 3697, 3769, 3796, 3967, 3976, 6379, 6397, 6793, 6973, 7369, 7396, 7693, 7963, 9367, 9376, 9673, 9763]
[4679, 4697, 4769, 4796, 4967, 4976, 6479, 6497, 6794, 6974, 7469, 7496, 7694, 7964, 9467, 9476, 9674, 9764]
[5679, 5697, 5769, 5796, 5967, 5976, 6579, 6597, 6795, 6975, 7569, 7596, 7695, 7965, 9567, 9576, 9675, 9765]
[6798, 6879, 6897, 6978, 7698, 7869, 7896, 7968, 8679, 8697, 8769, 8796, 8967, 8976, 9678, 9768, 9867, 9876]
 
Count of Spanish cardinal anagrams up to 10000: 2635
 
Largest group(s) of Spanish cardinal anagrams up to 10000:
[2304, 2403, 3204, 3402, 4203, 4302]
[2306, 2603, 3206, 3602, 6203, 6302]
[2308, 2803, 3208, 3802, 8203, 8302]
[2324, 2423, 3224, 3422, 4223, 4322]
[2326, 2623, 3226, 3622, 6223, 6322]
[2328, 2823, 3228, 3822, 8223, 8322]
[2334, 2433, 3234, 3432, 4233, 4332]
[2336, 2633, 3236, 3632, 6233, 6332]
[2338, 2833, 3238, 3832, 8233, 8332]
[2344, 2443, 3244, 3442, 4243, 4342]
[2346, 2643, 3246, 3642, 6243, 6342]
[2348, 2843, 3248, 3842, 8243, 8342]
[2354, 2453, 3254, 3452, 4253, 4352]
[2356, 2653, 3256, 3652, 6253, 6352]
[2358, 2853, 3258, 3852, 8253, 8352]
[2364, 2463, 3264, 3462, 4263, 4362]
[2366, 2663, 3266, 3662, 6263, 6362]
[2368, 2863, 3268, 3862, 8263, 8362]
[2374, 2473, 3274, 3472, 4273, 4372]
[2376, 2673, 3276, 3672, 6273, 6372]
[2378, 2873, 3278, 3872, 8273, 8372]
[2384, 2483, 3284, 3482, 4283, 4382]
[2386, 2683, 3286, 3682, 6283, 6382]
[2388, 2883, 3288, 3882, 8283, 8382]
[2394, 2493, 3294, 3492, 4293, 4392]
[2396, 2693, 3296, 3692, 6293, 6392]
[2398, 2893, 3298, 3892, 8293, 8392]
[2406, 2604, 4206, 4602, 6204, 6402]
[2408, 2804, 4208, 4802, 8204, 8402]
[2426, 2624, 4226, 4622, 6224, 6422]
[2428, 2824, 4228, 4822, 8224, 8422]
[2436, 2634, 4236, 4632, 6234, 6432]
[2438, 2834, 4238, 4832, 8234, 8432]
[2446, 2644, 4246, 4642, 6244, 6442]
[2448, 2844, 4248, 4842, 8244, 8442]
[2456, 2654, 4256, 4652, 6254, 6452]
[2458, 2854, 4258, 4852, 8254, 8452]
[2466, 2664, 4266, 4662, 6264, 6462]
[2468, 2864, 4268, 4862, 8264, 8462]
[2476, 2674, 4276, 4672, 6274, 6472]
[2478, 2874, 4278, 4872, 8274, 8472]
[2486, 2684, 4286, 4682, 6284, 6482]
[2488, 2884, 4288, 4882, 8284, 8482]
[2496, 2694, 4296, 4692, 6294, 6492]
[2498, 2894, 4298, 4892, 8294, 8492]
[2608, 2806, 6208, 6802, 8206, 8602]
[2628, 2826, 6228, 6822, 8226, 8622]
[2638, 2836, 6238, 6832, 8236, 8632]
[2648, 2846, 6248, 6842, 8246, 8642]
[2658, 2856, 6258, 6852, 8256, 8652]
[2668, 2866, 6268, 6862, 8266, 8662]
[2678, 2876, 6278, 6872, 8276, 8672]
[2688, 2886, 6288, 6882, 8286, 8682]
[2698, 2896, 6298, 6892, 8296, 8692]
[3406, 3604, 4306, 4603, 6304, 6403]
[3408, 3804, 4308, 4803, 8304, 8403]
[3426, 3624, 4326, 4623, 6324, 6423]
[3428, 3824, 4328, 4823, 8324, 8423]
[3436, 3634, 4336, 4633, 6334, 6433]
[3438, 3834, 4338, 4833, 8334, 8433]
[3446, 3644, 4346, 4643, 6344, 6443]
[3448, 3844, 4348, 4843, 8344, 8443]
[3456, 3654, 4356, 4653, 6354, 6453]
[3458, 3854, 4358, 4853, 8354, 8453]
[3466, 3664, 4366, 4663, 6364, 6463]
[3468, 3864, 4368, 4863, 8364, 8463]
[3476, 3674, 4376, 4673, 6374, 6473]
[3478, 3874, 4378, 4873, 8374, 8473]
[3486, 3684, 4386, 4683, 6384, 6483]
[3488, 3884, 4388, 4883, 8384, 8483]
[3496, 3694, 4396, 4693, 6394, 6493]
[3498, 3894, 4398, 4893, 8394, 8493]
[3608, 3806, 6308, 6803, 8306, 8603]
[3628, 3826, 6328, 6823, 8326, 8623]
[3638, 3836, 6338, 6833, 8336, 8633]
[3648, 3846, 6348, 6843, 8346, 8643]
[3658, 3856, 6358, 6853, 8356, 8653]
[3668, 3866, 6368, 6863, 8366, 8663]
[3678, 3876, 6378, 6873, 8376, 8673]
[3688, 3886, 6388, 6883, 8386, 8683]
[3698, 3896, 6398, 6893, 8396, 8693]
[4608, 4806, 6408, 6804, 8406, 8604]
[4628, 4826, 6428, 6824, 8426, 8624]
[4638, 4836, 6438, 6834, 8436, 8634]
[4648, 4846, 6448, 6844, 8446, 8644]
[4658, 4856, 6458, 6854, 8456, 8654]
[4668, 4866, 6468, 6864, 8466, 8664]
[4678, 4876, 6478, 6874, 8476, 8674]
[4688, 4886, 6488, 6884, 8486, 8684]
[4698, 4896, 6498, 6894, 8496, 8694]
 
Count of Portuguese cardinal anagrams up to 10000: 2505
 
Largest group(s) of Portuguese cardinal anagrams up to 10000:
[4679, 4697, 4796, 4976, 6479, 6497, 6794, 6974, 7496, 7694, 9476, 9674]
[4798, 4879, 4897, 4978, 7498, 7894, 8479, 8497, 8794, 8974, 9478, 9874]
[6798, 6879, 6897, 6978, 7698, 7896, 8679, 8697, 8796, 8976, 9678, 9876]
</pre>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl" line>
use strict; use warnings;
use feature <say postderef>;
use List::AllUtils 'max_by';
use Lingua::EN::Numbers 'num2en';
 
for my $limit (1e3, 1e4) {
my(%E,@E);
push $E{ join '', sort split '', num2en($_) }->@*, $_ for 0..$limit;
for (keys %E) { delete $E{$_} if $E{$_}->@* < 2 }
@E = sort { $a <=> $b } map { @$_ } values %E;
 
say 'First 30 English cardinal anagrams:' . join ' ', @E[0..29] if $limit == 1e3;
say "\nCount of English cardinal anagrams up to $limit: " . keys %E;
say "\nLargest group(s) of English cardinal anagrams up to $limit";
say join ' ', $E{$_}->@* for max_by {f $E{$_}->@* } keys %E;
}
</syntaxhighlight>
{{out}}
<pre>
First 30 English cardinal anagrams:
67 69 76 79 96 97 102 103 104 105 106 107 108 109 112 122 123 124 125 126 127 128 129 132 133 134 135 136 137 138
 
Count of English cardinal anagrams up to 1000: 317
 
Largest group(s) of English cardinal anagrams up to 1000
679 697 769 796 967 976
 
Count of English cardinal anagrams up to 10000: 2534
 
Largest group(s) of English cardinal anagrams up to 10000
5679 5697 5769 5796 5967 5976 6579 6597 6795 6975 7569 7596 7695 7965 9567 9576 9675 9765
4679 4697 4769 4796 4967 4976 6479 6497 6794 6974 7469 7496 7694 7964 9467 9476 9674 9764
3679 3697 3769 3796 3967 3976 6379 6397 6793 6973 7369 7396 7693 7963 9367 9376 9673 9763
1679 1697 1769 1796 1967 1976 6179 6197 6791 6971 7169 7196 7691 7961 9167 9176 9671 9761
2679 2697 2769 2796 2967 2976 6279 6297 6792 6972 7269 7296 7692 7962 9267 9276 9672 9762
6798 6879 6897 6978 7698 7869 7896 7968 8679 8697 8769 8796 8967 8976 9678 9768 9867 9876</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">for</span> <span style="color: #000000;">limit</span> <span style="color: #008080;">in</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10000</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">ana</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000000;">ani</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">limit</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">key</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">ordinal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)))</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ana</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">ani</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">i</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">ana</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ana</span><span style="color: #0000FF;">,</span><span style="color: #000000;">key</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">ani</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ani</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lengths</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ani</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">limit</span><span style="color: #0000FF;">==</span><span style="color: #000000;">1000</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">all</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span> <span style="color: #008080;">in</span> <span style="color: #000000;">lengths</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">all</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">all</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ani</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">all</span><span style="color: #0000FF;">)>=</span><span style="color: #000000;">30</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">all</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">flatten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">all</span><span style="color: #0000FF;">))[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">30</span><span style="color: #0000FF;">]</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 30 English cardinal anagrams:\n%s\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">all</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%3d"</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Count of English cardinal anagrams up to %,d: %,d\n\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lengths</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&gt;"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))})</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">maxlen</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lengths</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">biggest</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span> <span style="color: #008080;">in</span> <span style="color: #000000;">lengths</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">maxlen</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">biggest</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">biggest</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"["</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ani</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"]"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Largest group%s of English cardinal anagrams up to %,d:\n %s\n\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">biggest</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #008000;">""</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"s"</span><span style="color: #0000FF;">),</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">biggest</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n "</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
First 30 English cardinal anagrams:
67 69 76 79 96 97 102 103 104 105
106 107 108 109 112 122 123 124 125 126
127 128 129 132 133 134 135 136 137 138
 
Count of English cardinal anagrams up to 1,000: 317
 
Largest group of English cardinal anagrams up to 1,000:
[679 697 769 796 967 976]
 
Count of English cardinal anagrams up to 10,000: 2,534
 
Largest groups of English cardinal anagrams up to 10,000:
[1679 1697 1769 1796 1967 1976 6179 6197 6791 6971 7169 7196 7691 7961 9167 9176 9671 9761]
[2679 2697 2769 2796 2967 2976 6279 6297 6792 6972 7269 7296 7692 7962 9267 9276 9672 9762]
[3679 3697 3769 3796 3967 3976 6379 6397 6793 6973 7369 7396 7693 7963 9367 9376 9673 9763]
[4679 4697 4769 4796 4967 4976 6479 6497 6794 6974 7469 7496 7694 7964 9467 9476 9674 9764]
[5679 5697 5769 5796 5967 5976 6579 6597 6795 6975 7569 7596 7695 7965 9567 9576 9675 9765]
[6798 6879 6897 6978 7698 7869 7896 7968 8679 8697 8769 8796 8967 8976 9678 9768 9867 9876]
</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">""" rosettacode.org/wiki/English_cardinal_anagrams """
from collections import Counter
from num2words import num2words
 
 
def anagrammed(astring):
""" Get the letter counts of astring for use as an anagram representation.
Ignores spaces, hyphens, capitalization """
lstr = astring.lower()
charcounts = [sum(c == letter for c in lstr)
for letter in 'abcdefghijklmnopqrstuvwxyz']
return ''.join([f'{j:0>2d}' for j in charcounts])
 
 
def process_task(maxrange, showfirst30=True):
""" task at rosettacode.org/wiki/English_cardinal_anagrams """
numwords = [num2words(n) for n in range(maxrange+1)]
anastrings = [anagrammed(astr) for astr in numwords]
rep_to_count = Counter(anastrings)
counts = [rep_to_count[anastrings[i]] for i in range(maxrange+1)]
if showfirst30:
print("First 30 English cardinal anagrams:")
i, printed = 1, 0
while i < maxrange and printed < 30:
if counts[i] > 1:
printed += 1
print(f'{i:4}', end='\n' if printed % 10 == 0 else '')
i += 1
 
print(f'\nCount of English cardinal anagrams up to {maxrange}: ', end='')
print(sum(n > 1 for n in rep_to_count.values()))
print(f'\nLargest group(s) of English cardinal anagrams up to {maxrange}:')
maxcount = max(counts)
for rep in set(anastrings[i] for i in range(maxrange+1) if counts[i] == maxcount):
print([i for i in range(maxrange+1) if anastrings[i] == rep])
 
 
process_task(1000)
process_task(10000, False)
</syntaxhighlight>{{out}}
<pre>
First 30 English cardinal anagrams:
67 69 76 79 96 97 102 103 104 105
106 107 108 109 112 122 123 124 125 126
127 128 129 132 133 134 135 136 137 138
 
Count of English cardinal anagrams up to 1000: 317
 
Largest group(s) of English cardinal anagrams up to 1000:
[679, 697, 769, 796, 967, 976]
 
Count of English cardinal anagrams up to 10000: 2534
 
Largest group(s) of English cardinal anagrams up to 10000:
[4679, 4697, 4769, 4796, 4967, 4976, 6479, 6497, 6794, 6974, 7469, 7496, 7694, 7964, 9467, 9476, 9674, 9764]
[3679, 3697, 3769, 3796, 3967, 3976, 6379, 6397, 6793, 6973, 7369, 7396, 7693, 7963, 9367, 9376, 9673, 9763]
[5679, 5697, 5769, 5796, 5967, 5976, 6579, 6597, 6795, 6975, 7569, 7596, 7695, 7965, 9567, 9576, 9675, 9765]
[1679, 1697, 1769, 1796, 1967, 1976, 6179, 6197, 6791, 6971, 7169, 7196, 7691, 7961, 9167, 9176, 9671, 9761]
[6798, 6879, 6897, 6978, 7698, 7869, 7896, 7968, 8679, 8697, 8769, 8796, 8967, 8976, 9678, 9768, 9867, 9876]
[2679, 2697, 2769, 2796, 2967, 2976, 6279, 6297, 6792, 6972, 7269, 7296, 7692, 7962, 9267, 9276, 9672, 9762]
</pre>
 
=={{header|Raku}}==
Line 66 ⟶ 461:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./sort" for Sort
I've reused the code for determining the number names from the related task.
<syntaxhighlight lang="ecmascript">import "./sortfmt" for SortFmt, Name
import "./fmt" for Fmt
 
var small = [
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
]
 
var tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
 
var illions = ["", " thousand", " million", " billion"," trillion", " quadrillion", " quintillion"]
 
var say
say = Fn.new { |n|
var t = ""
if (n < 0) {
t = "negative "
n = -n
}
if (n < 20) {
t = t + small[n]
} else if (n < 100) {
t = t + tens[(n/10).floor]
var s = n % 10
if (s > 0) t = t + "-" + small[s]
} else if (n < 1000) {
t = t + small[(n/100).floor] + " hundred"
var s = n % 100
if (s > 0) t = t + " " + say.call(s)
} else {
var sx = ""
var i = 0
while (n > 0) {
var p = n % 1000
n = (n/1000).floor
if (p > 0) {
var ix = say.call(p) + illions[i]
if (sx != "") ix = ix + " " + sx
sx = ix
}
i = i + 1
}
t = t + sx
}
return t
}
 
for (limit in [1000, 10000]) {
var ana = {}
for (i in 0..limit) {
var key = Sort.quick(sayName.callfromNum(i).toList).join("")
if (ana.containsKey(key)) {
ana[key].add(i)
Line 134 ⟶ 484:
}
var count = ana.count { |me| me.value.count > 1 }
Fmt.print("Count of English cardinalscardinal anagrams up to $,d: $,d", limit, count)
var max = 0
var largest
Line 158 ⟶ 508:
127 128 129 132 133 134 135 136 137 138
 
Count of English cardinalscardinal anagrams up to 1,000: 317
 
Largest group(s) of English cardinal anagrams up to 1,000:
[679 697 769 796 967 976]
 
Count of English cardinalscardinal anagrams up to 10,000: 2,534
 
Largest group(s) of English cardinal anagrams up to 10,000:
9,476

edits