Python Forum
Stuck with using lists to solve task
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stuck with using lists to solve task
#4
If using 0 and 1 to represent closed / open doors it's simple to toggle value using XOR (bitwise exclusive OR). Something along those lines (just an idea, haven't tested it):

# 1 means door open, 0 mean doors closed
# create list of 100 doors open
doors = [1] * 100

# toggle every second value
for i in range(1, 100, 2):
     doors[i] ^= True

# toggle nth value with nth step
for n in range(2, 101):
    for i in range(n, 100, n):
        doors[i] ^= True

print(doors)
print(sum(doors))
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Stuck with using lists to solve task - by gery576 - Jan-15-2024, 11:03 AM
RE: Stuck with using lists to solve task - by perfringo - Jan-15-2024, 07:46 PM
RE: Stuck with using lists to solve task - by DPaul - Jan-16-2024, 06:57 AM
RE: Stuck with using lists to solve task - by DPaul - Jan-16-2024, 08:44 AM
RE: Stuck with using lists to solve task - by DPaul - Jan-16-2024, 06:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to solve this task? DERO 2 1,901 Dec-06-2021, 07:15 PM
Last Post: BashBedlam
  simple task with lists... Maxwell123 3 2,483 Jun-27-2020, 01:00 PM
Last Post: GOTO10
  I need help to solve this task using while statement rico4pepe 6 9,426 Apr-02-2020, 11:34 AM
Last Post: pyzyx3qwerty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020