Last answered:

18 Sept 2023

Posted on:

17 Sept 2023

0

the solution does not reflect the given conditions (the Coins problem)

As much as I had fun with it, it's a bit unfair to give a problem and then twist the conditions of the problem to suit your solution in the explanation. Please, remember that people who have only started learning are referring to the solutions provided for the point of truth.


The conditions were:
- we have a row of 1000 coins HEADS up

- we start flipping the coins starting from EACH SECOND coin


For some reason, in the solution starts from the all tails and we start from the first coin (index 0).

Yes, I understand that during the first round, all coins will be changed to 1s but! that was not the condition.

Furthermore, the solution to the given conditions would have one number more: 0 (the first element which would be unchanged if we had followed the initial conditions of the problem).

2 answers ( 0 marked as helpful)
Instructor
Posted on:

18 Sept 2023

0

Hey Tetiana,


Thank you for reaching out!


As the instructor emphasizes at the beginning of the lecture, multiple solutions exist for the problem. No solution is inherently "more correct," but some are more optimal in terms of code simplicity and execution time. The lecturer's solution is particularly efficient, requiring minimal code and computational memory. By presenting such efficient solutions, we aim to enhance your coding practices and ultimately elevate your programming skills.

Let's break down the solution presented in the lecture. For simplicity, let's consider 10 coins (n=11):

n = 11
coins = [0]*n

Let's also modify the nested loop as follows:

for i in range(1,n):
    for j in range(0,n,i):
        coins[j] = 1 - coins[j]
        print(f'i: {i}, j: {j} \n{coins}\n')

We've added a print function that displays the current i, j, and coins values.

You initial observation is that the lecturer begins with all coins facing tails up, rather than heads up. This is really just a preference that doesn't affect the outcome. We could either:

- Begin with tails up and flip each coin until they're all heads up (at i=1, j=10), or

- Start with heads up and adjust the code accordingly:

n = 11
coins = [1]*n # We start with all coins heads up

for i in range(2,n): # We start at i=2
    for j in range(0,n,i):
        coins[j] = 1 - coins[j]
        print(f'i: {i}, j: {j} \n{coins}\n')
Instructor
Posted on:

18 Sept 2023

0

Your second observation is that with each change in the value of i, we start by flipping the first coin (index 0) rather than skipping to every second, third coin, etc. This choice is a strategic programming decision for a more compact solution. Think of the flip of the coin at index "0" as an "empty flip" that helps us make the correct flip afterwards. If you exclude this coin from your dictionary, you'll consistently obtain the right answer for any value of n, remembering that n represents the number of coins plus one, not the coin count itself. An added advantage is that each index aligns with its respective coin number (e.g., index 1 for the first coin, index 4 for the fourth coin, and so on). This makes it easier to arrive at the punchline of the task, namely, that only coins at square-numbered positions remain heads up.

d = {}
for i,v in enumerate(coins):
    # an additional condition is added to exclude the coin indexed 0
    if v != 0 and i != 0:
        d[i]=v

While the lecturer's approach might initially seem non-intuitive, it's a strategic alternative among many. Ultimately, what matters is whether the solution solves the task for every value of n.

Hope this helps!

Kind regards,

365 Hristina

Submit an answer