Given an array of words, Group 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,D=[],{} for i in range(N): L.append([WordArr[i],i]) L[i][0]=''.join(sorted(L[i][0])) D.setdefault(L[i][0],[]).append(L[i][1]) L.clear() for X in list(D.values()): temp=[] for j in X: temp.append(WordArr[j]) L.append(temp) print(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
OUTPUT:
[ [‘dog’], [ ‘ape’, ‘pea’], [ ‘meal’, ‘male’, ‘lame’], [ ‘car’, ‘arc’] ]
INPUT_2:
Enter an array of words: cat, dog, tac, god, act
OUTPUT:
[ [ ‘dog’, ‘god’], [ ‘cat’, ‘tac’, ‘act’] ]
INPUT_3:
Enter an array of words: eat,tea,tan,ate,nat,bat
OUTPUT:
[ [ ‘tan’, ‘nat’], [ ‘eat’, ‘tea’, ‘ate’], [‘bat’] ]
INPUT_4:
Enter an array of words: a
OUTPUT:
[ [‘a’] ]
INPUT_5:
Enter an array of words: ‘ ‘
OUTPUT:
[ [‘ ‘] ]
ILLUSTRATION :
Morae! Q
- Find a triplet such that triplet is minimum of all the triplets.
- Rearrange characters in a string such that no two adjacent are same.
- Return the coordinates of all cells in matrix sorted by their distance.
- Function to verify the ISBN number from the book.
- Find out the absolute difference of values of two array integers.
- Rotate the array in the right direction by K steps.
- Find the sequence of moves that wins the game, collect exactly G coins!.
- Calculate the sum of boundary elements of a matrix.
- Find the total number of ways to reach the tree house.
- Compute profit and loss of a product.
- Find the total expense of purchased items.
- Determine the maximum sequence weight.
- Find the person’s birth year is a leap year or not.
- Find the inner rating with the displayed rating.
- Find if angles are valid to form a triangle.
- Find out the winner of the game from the given statistics.
- Verify the tag to determine if one needs to arrest or allow the vehicle.
- Find the largest hexadecimal number.
- Group up all anagrams together from given words.
- Return all anagrams from the given words.