pythonanagram

Print the maximum occurence of the anagrams and the anagram words itself among the input anagrams


a = ['ab', 'absa', 'sbaa', 'basa', 'ba']
res = []
s = 0
for i in range(len(a)):
    b=a[i]
    c = ''.join(sorted(b))
    res.append(c)
res.sort(reverse=False)
wordfreq = [res.count(p) for p in res]
d = dict(zip(res, wordfreq))
all_values = d.values()  #all_values is a list
max_value = max(all_values)
print(max_value)
max_key = max(d, key=d.get)
print(max_key)

In the given problem user inputs various anagram words, the output should be the maximum frequency of that word and print those anagrams. If you please help me print those anagrams from the input it will be really helpful.

Ooutput:

3 aabs       

Expected Ooutput:

3
absa sbaa basa

Solution

  • You can create a dictionary of word v/s list of anagrams

    and then print out the word which contains the maximum number of elements in the anagram list

    from collections import defaultdict
    words = ['ab','absa','sbaa','basa','ba']
    wordToAnagram= defaultdict(list) 
    # word vs list anagram 
    # loop below will create {aabs:  ['absa', 'sbaa', 'basa']}
    for word in words:
        s = "".join(sorted(word))
        wordToAnagram[s].append(word)
    
    
    word, anagrams = max(wordToAnagram.items(), key=lambda x: len(x[1]))
    print(" ".join(anagrams))
    

    OUTPUT:

    3
    absa sbaa basa
    

    Details

    1. wordToAnagrams

    After iterating through words wordToAnagram(dictionary) looks like this

    {
    "ab" : ["ab", "ba"]
    "aabs":  ["absa",  "sbaa", "base"]
    }
    
    1. dictionary.items()

    wordToAnagram.items() returns tuple-pair of dictionary key-value

    where,

    key: is our sorted string "ab" or "aabs",

    value : is list of anagrams, e.g for key = "ab", value equals["ab", "ba"]

    dict_items([('ab', ['ab', 'ba']), ('aabs', ['absa', 'sbaa', 'base'])])
    
    1. max function using 'key' and lambda expression

    max(wordToAnagram.items(), key=lambda x: len(x[1]))

    finds maximum value from wordToAnagram.items() iterable, by comparing length of anagrams list (len(x[1])