forked from gfvirga/python_challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsticker.py
More file actions
83 lines (67 loc) · 2.77 KB
/
sticker.py
File metadata and controls
83 lines (67 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Example solution for an interview question
# Facebook logo stickers cost $2 each from the company store. I have an idea.
# I want to cut up the stickers, and use the letters to make other words/phrases.
# A Facebook logo sticker contains only the word 'facebook', in all lower-case letters.
#
# Write a function that, given a string consisting of a word or words made up
# of letters from the word 'facebook', outputs an integer with the number of
# stickers I will need to buy.
#
# get_num_stickers('coffee kebab') -> 3
# get_num_stickers('book') -> 1
# get_num_stickers('ffacebook') -> 2
#
# You can assume the input you are passed is valid, that is, does not contain
# any non-'facebook' letters, and the only potential non-letter characters
# in the string are spaces.
def get_num_stickers(word):
sticker ={}
total_full_stickers = 0
# Buys first sticker
sticker = buy_sticker("facebook",sticker)
# Loops through the word for each letter
for letter in word.replace(" ", ""):
# If finds the letter in current sticker dictionary
if letter in sticker:
# Take a letter away
sticker[letter] -= 1
# If the letter amount available is 0 - add to the total sticker counter and buys another sticker adding to the new letter to the dictionary hash
if sticker[letter] == 0:
total_full_stickers += 1
sticker = buy_sticker("facebook",sticker)
# If not it will break
else:
print "Letter not in facebook"
break
return total_full_stickers
def buy_sticker(word, sticker):
# Cutting the facebook word into letters and putting a dictionary
for cut in word:
if cut in sticker:
sticker[cut] += 1
else:
sticker[cut] = 1
return sticker
# word in question
# prints the result of get_num_stickers function
word = "coffee kebab"
print "For the word '" + word + "' need to buy " + str(get_num_stickers(word)) + " stickers!"
word = "book"
print "For the word '" + word + "' need to buy " + str(get_num_stickers(word)) + " stickers!"
word = "ffacebook"
print "For the word '" + word + "' need to buy " + str(get_num_stickers(word)) + " stickers!"
word = "face"
print "For the word '" + word + "' need to buy " + str(get_num_stickers(word)) + " stickers!"
word = "bab beef"
print "For the word '" + word + "' need to buy " + str(get_num_stickers(word)) + " stickers!"
word = "cook"
print "For the word '" + word + "' need to buy " + str(get_num_stickers(word)) + " stickers!"
word = "what"
print "For the word '" + word + "' need to buy " + str(get_num_stickers(word)) + " stickers!"
# Test
from collections import Counter
test = buy_sticker("facebook", {})
if test == Counter("facebook"):
print("Tested ok")
else:
print 1