English cardinal anagrams: Difference between revisions

Content added Content deleted
Line 296: Line 296:
[5679 5697 5769 5796 5967 5976 6579 6597 6795 6975 7569 7596 7695 7965 9567 9576 9675 9765]
[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]
[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>
</pre>