Skip to content

Commit eeb3701

Browse files
committed
Completed Day 21
1 parent 4f34644 commit eeb3701

File tree

8 files changed

+206
-7
lines changed

8 files changed

+206
-7
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# using collections library
2+
3+
import collections
4+
from pprint import pprint
5+
6+
# dic = {
7+
# 'two': 2,
8+
# 'five': 5,
9+
# 'one': 1,
10+
# 'three': 3,
11+
# 'four': 4
12+
# }
13+
14+
dic = collections.defaultdict(int)
15+
pprint(dic)
16+
17+
# Order dictionary
18+
19+
# dic_ordered = collections.OrderedDict(dic)
20+
#
21+
# pprint(dic_ordered)

days/04-06-collections/collections.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
{
3232
"cell_type": "code",
33-
"execution_count": 1,
33+
"execution_count": null,
3434
"metadata": {},
3535
"outputs": [],
3636
"source": [
@@ -227,13 +227,13 @@
227227
{
228228
"ename": "KeyError",
229229
"evalue": "'julian'",
230-
"output_type": "error",
231230
"traceback": [
232231
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
233232
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
234233
"\u001b[0;32m<ipython-input-10-2d801425e069>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0musers\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'bob'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0musers\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'julian'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m# oops\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
235234
"\u001b[0;31mKeyError\u001b[0m: 'julian'"
236-
]
235+
],
236+
"output_type": "error"
237237
}
238238
],
239239
"source": [
@@ -330,13 +330,13 @@
330330
{
331331
"ename": "KeyError",
332332
"evalue": "'mike'",
333-
"output_type": "error",
334333
"traceback": [
335334
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
336335
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
337336
"\u001b[0;32m<ipython-input-14-5d8074648e43>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mchallenges\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mchallenge\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mchallenges_done\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mchallenges\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mchallenge\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
338337
"\u001b[0;31mKeyError\u001b[0m: 'mike'"
339-
]
338+
],
339+
"output_type": "error"
340340
}
341341
],
342342
"source": [

days/13-15-text-games/rpc.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from rpc_class import *
2+
import random
3+
4+
5+
def main():
6+
print_header()
7+
8+
rolls = build_the_three_rolls()
9+
10+
name = get_players_name()
11+
12+
player1 = Player(name)
13+
player2 = Player("computer")
14+
15+
game_loop(player1, player2, rolls)
16+
17+
18+
def game_loop(player1, player2, rolls):
19+
count = 1
20+
while count < 4:
21+
p2_roll = random.choice(rolls)
22+
p1_roll = user_roll(rolls)
23+
24+
outcome = p1_roll.can_defeat(p2_roll)
25+
26+
# display throws
27+
print(f'Computer rolled: {p2_roll}')
28+
print(f'You rolled: {p1_roll}')
29+
30+
# display winner for this round
31+
32+
count += 1
33+
34+
# Compute who won
35+
36+
37+
def print_header():
38+
print('--------------------------------')
39+
print(' Rock, Paper, Scissors')
40+
print('--------------------------------')
41+
42+
43+
def get_players_name():
44+
name = input('What is your name?')
45+
return name
46+
47+
def build_the_three_rolls():
48+
return ['rock', 'paper', 'scissors']
49+
50+
51+
def user_roll(rolls):
52+
print()
53+
for idxRoll in rolls:
54+
print(f'{rolls.index(idxRoll)} - {idxRoll} ')
55+
56+
while True:
57+
roll = input(' Please select your roll: ')
58+
if roll.isdigit():
59+
if int(roll) <= len(rolls):
60+
break
61+
print('\n Incorrect input, please try again \n')
62+
63+
return rolls[int(roll)]
64+
65+
66+
67+
main()

days/13-15-text-games/rpc_class.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
class Roll():
3+
def __init__(self):
4+
# name of roll
5+
# rolls that can be defeated by self
6+
# rolls that defeated by self
7+
can_defeat
8+
pass
9+
10+
class Player():
11+
def __init__(self, name):
12+
# name of player
13+
self.name = name
14+

days/13-15-text-games/test.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import random
2+
3+
4+
class Creatures:
5+
def __init__(self, name, level):
6+
self.name = name
7+
self.level = level
8+
9+
def defensive_roll(self):
10+
roll = random.randint(1, 12)
11+
return roll * self.level
12+
13+
class Dragon(Creatures):
14+
def __init__(self, name, level, scaliness, breaths_fire):
15+
super().__init__(name, level)
16+
self.scaliness = scaliness
17+
self.breaths_fire = breaths_fire
18+
19+
def defensive_roll(self):
20+
roll = super().defensive_roll()
21+
value = roll * self.scaliness
22+
if self.breaths_fire:
23+
value = value * 2
24+
25+
return value
26+
27+
class Wizard(Creatures):
28+
def attack(self, creature):
29+
my_roll = self.defensive_roll()
30+
their_roll = creature.defensive_roll()
31+
32+
return my_roll >= their_roll
33+
34+
creatures = [
35+
Creatures('Bat', 5),
36+
Creatures('Toad', 1),
37+
Creatures('Tiger', 12),
38+
Dragon('Black Dragon', 50, scaliness=2, breaths_fire=False)
39+
]
40+
41+
active_creature = random.choice(creatures)
42+
43+
hero = Wizard('Spike', 10)
44+
45+
print(f'Active creature Name: {active_creature.name}, value: {active_creature.level}')
46+
47+
print(f'Active hero: {hero.name}, level: {hero.defensive_roll()}')
48+
49+
if hero.attack(active_creature):
50+
print('Win')
51+
else:
52+
print('Lose')

days/16-18-listcomprehensions-generators/list-comprehensions-generators.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,13 +503,13 @@
503503
{
504504
"ename": "StopIteration",
505505
"evalue": "",
506-
"output_type": "error",
507506
"traceback": [
508507
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
509508
"\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)",
510509
"\u001b[0;32m<ipython-input-21-63d934b06ce9>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# no more values to generate\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgen\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
511510
"\u001b[0;31mStopIteration\u001b[0m: "
512-
]
511+
],
512+
"output_type": "error"
513513
}
514514
],
515515
"source": [
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from pprint import pprint
2+
import random
3+
4+
NAMES = ['arnold schwarzenegger', 'alec baldwin', 'bob belderbos',
5+
'julian sequeira', 'sandra bullock', 'keanu reeves',
6+
'julbob pybites', 'bob belderbos', 'julian sequeira',
7+
'al pacino', 'brad pitt', 'matt damon', 'brad pitt']
8+
9+
listOfNames = [name.title() for name in NAMES]
10+
11+
# pprint(listOfNames)
12+
13+
def split_names(name):
14+
firstName, lastName = name.split(' ')
15+
return f'{lastName} {firstName}'
16+
17+
reversedNames = [split_names(name).title() for name in NAMES]
18+
19+
def gen_pairs():
20+
#  again a list comprehension is great here to get the first names
21+
# and title case them in just 1 line of code (this comment took 2)
22+
first_names = [name.split()[0].title() for name in NAMES]
23+
while True:
24+
25+
#  added this when I saw Julian teaming up with Julian (always test your code!)
26+
first, second = None, None
27+
while first == second:
28+
first, second = random.sample(first_names, 2)
29+
30+
yield f'{first} teams up with {second}'
31+
32+
33+
pairs = gen_pairs()
34+
for _ in range(10):
35+
x = next(pairs)
36+
print(x)
37+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import itertools
2+
import sys
3+
import time
4+
symbols = itertools.cycle('-\|/')
5+
while True:
6+
sys.stdout.write('\r' + next(symbols))
7+
sys.stdout.flush()
8+
time.sleep(0.1)

0 commit comments

Comments
 (0)