Skip to content

Fix typos and add comments to ch09 #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


# Exercise 3
# unpack the tuple into three string and display them
# Unpack the tuple into three strings and display them
position1, position2, position3 = cardinal_numbers
print(position1)
print(position2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def mean(values):
def median(values):
"""Return the median value of the list `values`"""
values.sort()
# If the number of valus is odd,
# If the number of values is odd,
# return the middle value of the list
if len(values) % 2 == 1:
# The value at the center of the list is the value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@
"Wyoming": "Cheyenne",
}

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

# Game loop continues until the user inputs "exit"
# or guesses the correct capital
while True:
guess = input(f"What is the capital of '{state}'? ").lower()
if guess == "exit":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@

def get_cats_with_hats(array_of_cats):
cats_with_hats_on = []
# We want to walk around the circle 100 times
for num in range(1, 100 + 1):
# Each time we walk around, we visit 100 cats
for cat in range(1, 100 + 1):
# Determine whether to visit the cat
# Use modulo operator to visit every 2nd, 3rd, 4th,... etc.
if cat % num == 0:
# Remove or add hat depending on
# whether the cat already has one
if array_of_cats[cat] is True:
array_of_cats[cat] = False
else:
array_of_cats[cat] = True

# Add all number of each cat with a hat to list
for cat in range(1, 100 + 1):
if cats[cat] is True:
if array_of_cats[cat] is True:
cats_with_hats_on.append(cat)

# Return the resulting list
return cats_with_hats_on


# Cats contains whether each cat already has a hat on,
# by default all are set to false since none have been visited
cats = [False] * (100 + 1)
print(get_cats_with_hats(cats))
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@

theCats = {}

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

# Walk around the circle 100 times
for i in range(1, 101):
# Visit all cats each time we do a lap
for cats, hats in theCats.items():
# Determine whether or not we visit a cat
if cats % i == 0:
# Add or remove the hat
if theCats[cats]:
theCats[cats] = False
else:
theCats[cats] = True

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