Skip to content

Commit 61ffb04

Browse files
committed
JS - itertools code + readme
1 parent d670b98 commit 61ffb04

File tree

5 files changed

+77
-2
lines changed

5 files changed

+77
-2
lines changed

days/19-21-itertools/README.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
# Days 19-21: Itertools
22

3+
Iteration is something you absolutely have to grasp in order to master Python.
34

5+
Itertools helps make iteration simpler and is incredibly powerful which is why you'll be learning it for the next 3 days!
46

57

6-
## Day N: Watch videos and <x>!
8+
## Day N: Watch videos and Play in the Shell!
79

10+
For your first day, watch all of the videos up until the traffic lights app.
811

12+
After watching the first 4 videos pop into your Python shell and start playing around with `cycle()`, `product()`, `combinations()` and `permutations()`.
913

1014

11-
## Day N+1: <x>
15+
## Day N+1: Create a Traffic Lights script
1216

17+
Yep that's right! For your second day, use itertools to create a script that simulates traffic lights!
1318

19+
If you get absolutely stuck, watch the *Traffic Lights* video to see how we did it.
1420

1521

1622
## Day N+2: Your Turn!
1723

24+
For your last day, I'm going to suggest you head to our [codechalleng.es](https://codechalleng.es) platform and take a few of the itertools themed Bites.
1825

26+
The following 3 links will get you free access to the Bites:
27+
28+
[Bite 64](https://codechalleng.es/bites/promo/itertools-fun1)
29+
[Bite 17](https://codechalleng.es/bites/promo/itertools-fun2)
30+
[Bite 65](https://codechalleng.es/bites/promo/itertools-fun3)
31+
32+
You don't need to do them in any particular order, they're just there for you to learn something!
1933

2034

2135
### Time to share what you've accomplished!
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import itertools
2+
import sys
3+
import time
4+
5+
symbols = itertools.cycle('-\|/')
6+
7+
while True:
8+
sys.stdout.write('\r' + next(symbols))
9+
sys.stdout.flush()
10+
time.sleep(0.1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from itertools import permutations, combinations
2+
3+
friends = 'mike bob julian'.split()
4+
# note that we get a generator so using list to consume it
5+
print(list(combinations(friends, 2)))
6+
7+
# what if order matters?
8+
print(list(permutations(friends, 2)))

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

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import itertools
2+
import sys
3+
import time
4+
5+
6+
def spinner(seconds):
7+
"""Show an animated spinner while we sleep."""
8+
symbols = itertools.cycle('-\|/')
9+
tend = time.time() + seconds
10+
while time.time() < tend:
11+
# '\r' is carriage return: return cursor to the start of the line.
12+
sys.stdout.write('\rPlease wait... ' + next(symbols)) # no newline
13+
sys.stdout.flush()
14+
time.sleep(0.1)
15+
print()
16+
17+
18+
if __name__ == "__main__":
19+
spinner(3)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from time import sleep
2+
import itertools
3+
import random
4+
5+
colours = 'Red Green Amber'.split()
6+
rotation = itertools.cycle(colours)
7+
8+
def rg_timer():
9+
return random.randint(3, 7)
10+
11+
def light_rotation(rotation):
12+
for colour in rotation:
13+
if colour == 'Amber':
14+
print('Caution! The light is %s' % colour)
15+
sleep(3)
16+
elif colour == 'Red':
17+
print('STOP! The light is %s' % colour)
18+
sleep(rg_timer())
19+
else:
20+
print('Go! The light is %s' % colour)
21+
sleep(rg_timer())
22+
23+
if __name__ == '__main__':
24+
light_rotation(rotation)

0 commit comments

Comments
 (0)