# Lab 07
# Q1: WWPD: Linked Lists
答案:link.py
# Q2: Making Cards
很简单!
def __init__(self, name, attack, defense):
self.name = name
self.attack = attack
self.defense = defense
1
2
3
4
2
3
4
def power(self, other_card):
return self.attack - other_card.defense / 2
1
2
2
# Q3: Making a Player
class Player:
def __init__(self, deck, name):
self.deck = deck
self.name = name
"*** YOUR CODE HERE ***"
self.hand = []
for i in range(5):
self.hand.append(deck.draw())
def draw(self):
assert not self.deck.is_empty(), 'Deck is empty!'
"*** YOUR CODE HERE ***"
self.hand.append(self.deck.draw())
def play(self, card_index):
"*** YOUR CODE HERE ***"
return self.hand.pop(card_index)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Q4: Link to List
def link_to_list(link):
res = []
while link:
res.append(link.first)
link = link.rest
return res
1
2
3
4
5
6
2
3
4
5
6
# Q5: Cumulative Mul
def cumulative_mul(t):
if t.is_leaf():
pass
else:
for b in t.branches:
cumulative_mul(b)
t.label *= b.label
1
2
3
4
5
6
7
2
3
4
5
6
7