Last answered:

04 Apr 2023

Posted on:

13 Jul 2022


How did it go with your solution?

Hello. I'm curious about other students' solution in this challenge. I always try to solve the challenges with the least lines that I could, only to find out later that Giles perform the solution with even fewer lines of code. Here's mine:

n = 1000
coins = [1 for i in range(n)]

for i in range(1,n):
    for j in range(1,n):
        if (j+1) % (i+1) == 0:
            if coins[j] == 1:
                coins[j] = 0
                coins[j] = 1

result = [num+1 for num in range(len(coins)) if coins[num] == 1]

I think being able to understand codes written by other people is also a good practice. Would you care to share your solution?

5 answers ( 0 marked as helpful)
Posted on:

16 Oct 2022


Here's my code...thank you for sharing yours
def coins(n):
    l1= ["H" for i in range(n)]
    for a in range(0,n):
        for i in range(a+1,n,a+2):
            if l1[i] == "H":
                l1[i] = "T"
                l1[i] = "H"
    index = [count+1 for count,ele in enumerate(l1) if ele=="H"]

Posted on:

05 Nov 2022


here's mine, a bit more complex but it works!

list_coins = ['head' for i in range(1000)]
list_heads = []
jump = 1
count = 1
# print(list_coins)
while jump < len(list_coins):
    jump += 1
    for j in range((jump-1),(len(list_coins)),jump):
        # print(f'jump is {jump}')
        if list_coins[j] == 'head':
            list_coins[j] = 'tail'
        elif list_coins[j] == 'tail':
            list_coins[j] = 'head'
        # print(list_coins)
# print(list_coins)
for c in list_coins:
    if c == 'head':
    count += 1

Posted on:

15 Nov 2022


Here's how I did mine.

coins = {}
for i in range(1, 21):
    coins[i] = "heads"

def coin_flip(coin_dict):
    step = 2
    while step <= len(coin_dict):
        for i in range(step, len(coin_dict)+1, step):
            coin = coin_dict[i]
            if coin == "heads":
                coin_dict[i] = "tails"
                coin_dict[i] = "heads"
        step += 1


I think it would make more sense if I did the dictionary part in the function and have it take the number of coins as the parameter but it works.

Posted on:

29 Nov 2022


I couldn't figure out the dictionary part, but here is the code I used to flip the coins. It made the most sense to me to use the modulus operator to iterate through all the indices that were divisible by 2, 3, 4, etc.

n = 1001

#create a list with 1000 0s
coins = [0]*n

for i in range(0, n):
    for j in range(1, n):
        if i % j == 0:
            coins[i] = 1 - coins[i]

Posted on:

04 Apr 2023

n = 1000

def count_heads(n):
    d = dict()
    for i in range(1,n+1):
        d[i] = 0
        for j in range(2,n+1):
            if i % j == 0:
                d[i] += 1
    for key, value in d.items():
        if value % 2 == 0:

I based my code in the idea that the numbers with heads at the end were those that had a even number of divisible occurrences.
I first though the goal was simply to say how many numbers were heads, so I had a counter for that. When I noticed the purpose was to show the numbers, I modified the code. 

Submit an answer