generated from mwc/lab_dice
Adding assessment
This commit is contained in:
parent
055f163409
commit
bd2d7b8f0b
|
@ -0,0 +1,57 @@
|
||||||
|
# Dice Lab Assessment
|
||||||
|
|
||||||
|
Everything works as expected. Nice work!
|
||||||
|
|
||||||
|
## Comments
|
||||||
|
|
||||||
|
I thought your observation that "This method of problem solving is more
|
||||||
|
organized than how I would have written a Yahtzee program using
|
||||||
|
my past experience" was interesting. I agree, though it's certainly possible to write
|
||||||
|
OOP badly, and thereby increase the complexity of a problem :)
|
||||||
|
I find that I increasingly think deeply about whether to use an OO approach
|
||||||
|
or a functional approach--basically, whether to focus on nouns or verbs.
|
||||||
|
|
||||||
|
Your decision to count 3- and 4-of-a-kind with a zero-initialized
|
||||||
|
dict works well:
|
||||||
|
|
||||||
|
```
|
||||||
|
of_each_counter={1:0, 2:0, 3:0, 4:0, 5:0, 6:0}
|
||||||
|
```
|
||||||
|
|
||||||
|
Two alternatives from the standard library are worth keeping in mind for
|
||||||
|
the future. A Counter just takes a list and counts instances of each value.
|
||||||
|
|
||||||
|
```
|
||||||
|
>>> from collections import Counter
|
||||||
|
>>> counter = Counter([1, 3, 3, 5, 3])
|
||||||
|
>>> print(counter)
|
||||||
|
Counter({3: 3, 1: 1, 5: 1})
|
||||||
|
```
|
||||||
|
|
||||||
|
A defaultdict is like a normal dict, but when you first access a key, its value is
|
||||||
|
initialized for you if it's not already present.
|
||||||
|
|
||||||
|
```
|
||||||
|
>>> from collections import defaultdict
|
||||||
|
>>> counts = defaultdict(int)
|
||||||
|
>>> for face in die_faces:
|
||||||
|
... counts[face] += 1
|
||||||
|
```
|
||||||
|
|
||||||
|
Another "pythonic" trick would be available for GoalYahtzee, where you check whether
|
||||||
|
all the dice faces are the same:
|
||||||
|
|
||||||
|
```
|
||||||
|
for die in dice:
|
||||||
|
if die!=dice[0].face:
|
||||||
|
flag=False
|
||||||
|
```
|
||||||
|
|
||||||
|
In this case, I'll often convert the list into a set, which enforces member uniqueness.
|
||||||
|
|
||||||
|
```
|
||||||
|
unique_faces = set(dice)
|
||||||
|
if len(unique_faces) == 1:
|
||||||
|
print("Yahtzee!")
|
||||||
|
```
|
||||||
|
|
Loading…
Reference in New Issue