Skip to content

Commit 39fc222

Browse files
committed
itertools and scrabble
1 parent db34e15 commit 39fc222

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

days/19-21-itertools/code/Bite64.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import itertools
2+
3+
names = 'Tim Bob Julian Carmen Sofia Mike Kim Andre'.split()
4+
locations = 'DE ES AUS NL BR US'.split()
5+
confirmed = [False, True, True, False, True]
6+
7+
8+
def get_attendees():
9+
for participant in itertools.zip_longest(names, locations, confirmed,fillvalue='-'):
10+
print(participant)
11+
12+
13+
if __name__ == '__main__':
14+
get_attendees()

days/19-21-itertools/code/Bite65.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import itertools
2+
import os
3+
import urllib.request
4+
5+
# PREWORK
6+
DICTIONARY = os.path.join('/tmp', 'dictionary.txt')
7+
urllib.request.urlretrieve('http://bit.ly/2iQ3dlZ', DICTIONARY)
8+
9+
with open(DICTIONARY) as f:
10+
dictionary = set([word.strip().lower() for word in f.read().split()])
11+
12+
13+
def get_possible_dict_words(draw):
14+
"""Get all possible words from a draw (list of letters) which are
15+
valid dictionary words. Use _get_permutations_draw and provided
16+
dictionary"""
17+
all_words = _get_permutations_draw(draw)
18+
valid_words = [word for word in all_words if word in dictionary]
19+
return valid_words
20+
pass
21+
22+
def _get_permutations_draw(draw):
23+
"""Helper to get all permutations of a draw (list of letters), hint:
24+
use itertools.permutations (order of letters matters)"""
25+
all_words = []
26+
27+
for i in range(2, len(draw)):
28+
for word in itertools.permutations(draw, i):
29+
all_words.append("".join(word).lower())
30+
return all_words
31+
pass
32+
33+
draw1 = 'T, I, I, G, T, T, L'.split(", ")
34+
print(draw1)
35+
print(get_possible_dict_words(draw1))

days/19-21-itertools/code/Bites17.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from itertools import permutations, combinations
2+
3+
def friends_teams(friends,team_size=2,order_does_matter=False):
4+
if order_does_matter:
5+
return list(permutations(friends, team_size))
6+
else:
7+
return list(combinations(friends, team_size))
8+
9+
friends = friends = 'Bob Dante Julian Martin'.split()
10+
11+
print(friends_teams(friends, 2, True))
12+
print(friends_teams(friends))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import itertools
2+
import time
3+
import random
4+
5+
traffic_lights = itertools.cycle("green amber red".split())
6+
7+
8+
def rotate_traffic_lights():
9+
for color in traffic_lights:
10+
if color == "amber":
11+
print("Ready to stop")
12+
time.sleep(3)
13+
if color == "red":
14+
print("Stop")
15+
time.sleep(random.randint(3,7))
16+
if color == "green":
17+
print("Go and keep going")
18+
time.sleep(random.randint(3,7))
19+
20+
21+
22+
rotate_traffic_lights()

0 commit comments

Comments
 (0)