Skip to content

Instantly share code, notes, and snippets.

@franlu
Last active November 22, 2016 13:39
Show Gist options
  • Save franlu/881ac9f06761822b41ab33dd9bcb40b9 to your computer and use it in GitHub Desktop.
Save franlu/881ac9f06761822b41ab33dd9bcb40b9 to your computer and use it in GitHub Desktop.
Alphabet soup
Puzzle
=======
Everyone loves alphabet soup. And of course you want to know if you can construct a message from the letters found in your bowl.
Task:
=====
Write a function that takes as input two strings:
1. the message you want to write
2. all the letters found in your bowl of alphabet soup.
Assumptions:
============
· You may assume the soup only contains the 26 capital letters of the English alphabet.
· Also assume it may be a very large bowl of soup containing many letters.
· There is no guarantee that each letter occurs a similar number of times - indeed some letters might be missing entirely.
· And of course the letters are ordered randomly.
The function should determine if you can write your message with the letters found in your bowl of soup. The function should return true or false accordingly.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import string
import random
import collections
def fill_bowl(m):
soup = list(string.ascii_lowercase)
soup_list = [random.choice(soup) for n in range(m)]
bowl = "".join(soup_list)
return bowl
def alphabet_soup(message, bowl):
if len(message) > 0:
bowl_count = collections.Counter(bowl)
for letter in message:
if bowl_count[letter] and bowl_count[letter] >= 1:
bowl_count[letter] -= 1
else:
return False
return True
else:
return True
message_size = 500
bowl_size = 10000
message = fill_bowl(message_size)
bowl = fill_bowl(bowl_size)
if alphabet_soup(message, bowl):
print "We can write: " + message
else:
print "We can't write: " + message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment