8000 Fix typos and add comments to ch09 · rkpayah/python-basics-exercises@f2ffc65 · GitHub
[go: up one dir, main page]

Skip to content

Commit f2ffc65

Browse files
committed
Fix typos and add comments to ch09
1 parent 5ccdff7 commit f2ffc65

5 files changed

+25
-3
lines changed

ch09-lists-tuples-and-dictionaries/1-tuples-are-immutable-sequences.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
# Exercise 3
16-
# unpack the tuple into three string and display them
16+
# Unpack the tuple into three strings and display them
1717
position1, position2, position3 = cardinal_numbers
1818
print(position1)
1919
print(position2)

ch09-lists-tuples-and-dictionaries/4-challenge-list-of-lists.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def mean(values):
2424
def median(values):
2525
"""Return the median value of the list `values`"""
2626
values.sort()
27-
# If the number of valus is odd,
27+
# If the number of values is odd,
2828
# return the middle value of the list
2929
if len(values) % 2 == 1:
3030
# The value at the center of the list is the value

ch09-lists-tuples-and-dictionaries/7-challenge-capital-city-loop.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@
5656
"Wyoming": "Cheyenne",
5757
}
5858

59+
# Pull random state and capital pair from the dict by casting to list of tuples
5960
state, capital = random.choice(list(capitals_dict.items()))
6061

62+
# Game loop continues until the user inputs "exit"
63+
# or guesses the correct capital
6164
while True:
6265
guess = input(f"What is the capital of '{state}'? ").lower()
6366
if guess == "exit":

ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,30 @@
44

55
def get_cats_with_hats(array_of_cats):
66
cats_with_hats_on = []
7+
# We want to walk around the circle 100 times
78
for num in range(1, 100 + 1):
9+
# Each time we walk around, we visit 100 cats
810
for cat in range(1, 100 + 1):
11+
# Determine whether to visit the cat
12+
# Use modulo operator to visit every 2nd, 3rd, 4th,... etc.
913
if cat % num == 0:
14+
# Remove or add hat depending on
15+
# whether the cat already has one
1016
if array_of_cats[cat] is True:
1117
array_of_cats[cat] = False
1218
else:
1319
array_of_cats[cat] = True
20+
21+
# Add all number of each cat with a hat to list
1422
for cat in range(1, 100 + 1):
15-
if cats[cat] is True:
23+
if array_of_cats[cat] is True:
1624
cats_with_hats_on.append(cat)
25+
26+
# Return the resulting list
1727
return cats_with_hats_on
1828

1929

30+
# Cats contains whether each cat already has a hat on,
31+
# by default all are set to false since none have been visited
2032
cats = [False] * (100 + 1)
2133
print(get_cats_with_hats(cats))

ch09-lists-tuples-and-dictionaries/9c-challenge-cats-with-hats.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,24 @@
44

55
theCats = {}
66

7+
# By default, no cats have been visited
8+
# so we set every cat's number to False
79
for i in range(1, 101):
810
theCats[i] = False
911

12+
# Walk around the circle 100 times
1013
for i in range(1, 101):
14+
# Visit all cats each time we do a lap
1115
for cats, hats in theCats.items():
16+
# Determine whether or not we visit a cat
1217
if cats % i == 0:
18+
# Add or remove the hat
1319
if theCats[cats]:
1420
theCats[cats] = False
1521
else:
1622
theCats[cats] = True
1723

24+
# Print whether each cat has a hat
1825
for cats, hats in theCats.items():
1926
if theCats[cats]:
2027
print(f"Cat {cats} has a hat.")

0 commit comments

Comments
 (0)
0