Problem of the Day
A new programming or logic puzzle every Mon-Fri

Alphabet Soup

Given a list of letters, return a valid dictionary word that can be made with those letters. As a much tougher bonus, return the longest possible dictionary word that you can make with those letters.

Permalink: http://problemotd.com/problem/alphabet-soup/

Comments:

  • Nick Krichevsky - 10 years, 5 months ago

    Are we allowed to use each letter more than once? For example if the letters given are a and b, would the word 'ba' be the only valid response? Or would ['aa', 'aba', 'abba', 'ba', 'baa', 'baba'] all be valid responses?

    reply permalink

  • Max Burstein - 10 years, 5 months ago

    Each letter only once. So with a and b you could just return a.

    reply permalink

  • Nick Krichevsky - 10 years, 5 months ago

    Done.

    d = open('dictionary.txt','r')
    words = [line.rstrip() for line in d]
    d.close()
    letters = ['a','b']
    validWords = []
    for word in words:
        failed = False
        l = list(letters) # make a duplicate
        for letter in word:
            if letter in l:
                l.remove(letter)
            else:
                failed = True
                break
        if not failed:
            validWords.append(word)
    print validWords
    print "Longest word possible:", max(validWords)
    

    reply permalink

Content curated by @MaxBurstein