From 35eb0decdba21074cfe5b9780282e9129d517938 Mon Sep 17 00:00:00 2001 From: Tsakagur Date: Fri, 13 Dec 2019 11:37:00 +0100 Subject: [PATCH 1/4] Easier solution I found the solutions proposed a bit difficult to read. I think this one is just a direct translation of the instructions and will be easier for everyone to understand. --- .../9a-cats-with-hats | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 ch09-lists-tuples-and-dictionaries/9a-cats-with-hats diff --git a/ch09-lists-tuples-and-dictionaries/9a-cats-with-hats b/ch09-lists-tuples-and-dictionaries/9a-cats-with-hats new file mode 100644 index 0000000..9449c74 --- /dev/null +++ b/ch09-lists-tuples-and-dictionaries/9a-cats-with-hats @@ -0,0 +1,16 @@ +# At the start all cats are hatless. +cats = [False]*100 + +# We do a loop over each step size from 1 to 100 +for step in range(1,101): + + # We only look at cat indices that are a multiple of our step size + for i in range(1, 100//step + 1): + + # We change the hat status for each cat we loop over + cats[i*step-1] = not cats[i*step-1] + +# Print the list of cats that have a hat +for i in range(100): + if cats[i]: + print(f"Cat {i+1} has a hat!") From e25054eaa1d0358c71693bde34e0a4da95d1af62 Mon Sep 17 00:00:00 2001 From: Adam Makhlouf Date: Sat, 14 Dec 2019 11:12:12 +0100 Subject: [PATCH 2/4] Update ch09-lists-tuples-and-dictionaries/9a-cats-with-hats Trick to simplify the math with the indices. Co-Authored-By: David Amos --- ch09-lists-tuples-and-dictionaries/9a-cats-with-hats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch09-lists-tuples-and-dictionaries/9a-cats-with-hats b/ch09-lists-tuples-and-dictionaries/9a-cats-with-hats index 9449c74..72a78c0 100644 --- a/ch09-lists-tuples-and-dictionaries/9a-cats-with-hats +++ b/ch09-lists-tuples-and-dictionaries/9a-cats-with-hats @@ -1,5 +1,5 @@ # At the start all cats are hatless. -cats = [False]*100 +cats = [False]*101 # We do a loop over each step size from 1 to 100 for step in range(1,101): From e8687aa3a34bba6f69f7171c14259c13d18aaa0a Mon Sep 17 00:00:00 2001 From: Adam Makhlouf Date: Mon, 23 Dec 2019 10:41:58 +0100 Subject: [PATCH 3/4] Update and renaming of the 9a-challenge-cats-with-hats Making the variables easier to understand and some renaming for clarity. --- .../9a-cats-with-hats | 16 ---------------- .../9a-challenge-cats-with-hats | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 16 deletions(-) delete mode 100644 ch09-lists-tuples-and-dictionaries/9a-cats-with-hats create mode 100644 ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats diff --git a/ch09-lists-tuples-and-dictionaries/9a-cats-with-hats b/ch09-lists-tuples-and-dictionaries/9a-cats-with-hats deleted file mode 100644 index 72a78c0..0000000 --- a/ch09-lists-tuples-and-dictionaries/9a-cats-with-hats +++ /dev/null @@ -1,16 +0,0 @@ -# At the start all cats are hatless. -cats = [False]*101 - -# We do a loop over each step size from 1 to 100 -for step in range(1,101): - - # We only look at cat indices that are a multiple of our step size - for i in range(1, 100//step + 1): - - # We change the hat status for each cat we loop over - cats[i*step-1] = not cats[i*step-1] - -# Print the list of cats that have a hat -for i in range(100): - if cats[i]: - print(f"Cat {i+1} has a hat!") diff --git a/ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats b/ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats new file mode 100644 index 0000000..2b760fd --- /dev/null +++ b/ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats @@ -0,0 +1,19 @@ +# At the start all cats are hatless. +cats = [False]*101 + +# We do a loop over each step size from 1 to 100 +for step_size in range(1, 100 + 1): + + # For each step size we calculate how many cats we are going to visit + cats_to_visit = 100 // step_size + + # We only loop over cat indices that are a multiple of our step size + for i in range(1, cats_to_visit + 1): + + # We reverse the hat status for each cat we loop over + cats[i*step_size] = not cats[i*step_size] + +# Print the list of cats that have a hat +for i in range(100): + if cats[i]: + print(f"Cat {i+1} has a hat!") From 48a5e7237c8330c5127bc9558320991b8e72535e Mon Sep 17 00:00:00 2001 From: Adam Makhlouf Date: Mon, 23 Dec 2019 10:45:03 +0100 Subject: [PATCH 4/4] Forgot to correct end loop Now the cats that have a hat are all correctly displayed. --- .../9a-challenge-cats-with-hats | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats b/ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats index 2b760fd..1f3e7a1 100644 --- a/ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats +++ b/ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats @@ -14,6 +14,6 @@ for step_size in range(1, 100 + 1): cats[i*step_size] = not cats[i*step_size] # Print the list of cats that have a hat -for i in range(100): +for i in range(101): if cats[i]: - print(f"Cat {i+1} has a hat!") + print(f"Cat {i} has a hat!") pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy