Skip to content

Latest commit

 

History

History
70 lines (37 loc) · 1.18 KB

Where_my_anagrams_at.md

File metadata and controls

70 lines (37 loc) · 1.18 KB

CodeWars Python Solutions


Where my anagrams at?

Definition

What is an anagram? Well, two words are anagrams of each other if they both contain the same letters. For example:

'abba' & 'baab' == true

'abba' & 'bbaa' == true

'abba' & 'abbba' == false

'abba' & 'abca' == false

Write a function that will find all the anagrams of a word from a list. You will be given two inputs a word and an array with words. You should return an array of all the anagrams or an empty array if there are none. For example:

anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada']) => ['aabb', 'bbaa']

anagrams('racer', ['crazer', 'carer', 'racar', 'caers', 'racer']) => ['carer', 'racer']

anagrams('laser', ['lazing', 'lazy',  'lacer']) => []

Given Code

def anagrams(word, words):
    pass

Solution 1

def anagrams(word, words):
    return [i for i in words if set(i) == set(word) and len(i) == len(word)]

Solution 2

def anagrams(word, words):
    return [item for item in words if sorted(item)==sorted(word)]

See on CodeWars.com