Skip to content

Latest commit

 

History

History
69 lines (40 loc) · 1.22 KB

Arrh_grabscrab.md

File metadata and controls

69 lines (40 loc) · 1.22 KB

CodeWars Python Solutions


Arrh, grabscrab!

Definition

Pirates have notorious difficulty with enunciating. They tend to blur all the letters together and scream at people.

At long last, we need a way to unscramble what these pirates are saying.

Write a function that will accept a jumble of letters as well as a dictionary, and output a list of words that the pirate might have meant.

Example

grabscrab( "ortsp", ["sport", "parrot", "ports", "matey"] )

Should return ["sport", "ports"].

Return matches in the same order as in the dictionary. Return an empty array if there are no matches.

Good luck!


Given Code

def grabscrab(word, possible_words):
    pass

Solution 1

def grabscrab(word, possible_words):
    words = []
    letters1 = {l:word.count(l) for l in word}
    for word in possible_words:
        letters2 = {l:word.count(l) for l in word}
        if letters1 == letters2:
            words.append(word)
    return words

Solution 2

def grabscrab(word, possible_words):
    return [w for w in possible_words if sorted(word) == sorted(w)]

See on CodeWars.com