Menu Close

Given an array of strings, print anagrams….FcukTheCode

Given an array of words, print all anagrams together.

Anagrams : a word, phrase, or name formed by rearranging the letters of another, such as spar, formed from rasp.

For example,

Input:
[“cat”, “dog”, “tac”, “god”, “act”]

Output:
cat  tac  act  dog  god
def Anograms(WordArr,N):
    L=[]
    for i in range(N):
        L.append([WordArr[i],i])
        L[i][0]=''.join(sorted(L[i][0]))
    L.sort()
    print(*[WordArr[x[1]] for x in L])


WordArr=list(map(str,input("Enter an array of words: ").split(',')))
Anograms(WordArr,len(WordArr))

INPUT_1:
Enter an array of words:   car,  ape,  meal,  pea,  male,  arc,  lame,  dog
OUPUT:
 car   arc   meal   male  lame   ape   pea   dog


INPUT_2:
Enter an array of words:   cat,  dog,  tac,  god,  act
OUTPUT:
 cat   tac   act   dog   god


INPUT_3:
Enter an array of words:   eat,  tea,  tan,  ate,  nat,  bat
OUTPUT:
 bat   eat   tea   ate   tan   nat


ILLUSTRATION : 

EXECUTED USING PYTHON3 LINUX