#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    17
    Rep Power
    0

    Help with a Locker Game


    A school has 100 lockers and 100 students. All lockers
    are closed on the first day of school. As the students enter, the first student,
    denoted S1, opens every locker. Then the second student, S2, begins with the
    second locker, denoted L2, and closes every other locker. Student S3 begins
    with the third locker and changes every third locker (closes it if it was open,
    and opens it if it was closed). Student S4 begins with locker L4 and changes
    every fourth locker. Student S5 starts with L5 and changes every fifth locker,
    and so on, until student S100 changes L100.

    After all the students have passed through the building and changed the lockers,
    which lockers are open? Write a program to find your answer.
    (Hint: Use a list of 100 Boolean elements, each of which indicates whether a
    locker is open (True) or closed (False). Initially, all lockers are closed.)


    In my attempt at this I came up with the following code and got the following error in line 12 : TypeError: remove() takes exactly one argument (2 given)

    Can anyone help fix this??


    Any help would be greatly appreciated!!



    Code:
    isClosed = list(100 * ["False"])
    
    skipValue = 1
    startValue = 0
    studentLimit = 100
    isOpen = []
    
    
    while startValue <= studentLimit: 
        for i in range(startValue,len(isClosed),skipValue):
            if isClosed[i] == "False":
                isClosed.remove(i,"False")
                isClosed.insert(i,"True")
            else:
                isClosed.remove(i,"True")
                isClosed.insert(i,"False")
        startValue = startValue + 1
        skipValue = skipValue+1
    
    for i in range(0,len(isclosed)):
        if isClosed[i] == "True":
            isOpen.append(i)
            
    printlist(isOpen)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    492
    Rep Power
    33
    The result should be predictable since every successive student reverses the open/close, but you'll have to implement the counting code yourself. Instead of remove & insert you can just change the individual item.
    Code:
    lockers =[False for ctr in range(100)]  ## all lockers closed
    for student in range(100):
        for reverse in range(student, 100):  ## start at current student
            lockers[reverse] = not lockers[reverse]
    
    print lockers[:50]
    print lockers[50:]
    Also
    skipValue = skipValue+1
    is a different variable than the one used in the for loop even though they have the same name, so can be deleted.
    Last edited by dwblas; November 12th, 2013 at 11:09 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    17
    Rep Power
    0
    Thanks I will see what I can do!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    17
    Rep Power
    0
    Originally Posted by dwblas
    The result should be predictable since every successive student reverses the open/close, but you'll have to implement the counting code yourself. Instead of remove & insert you can just change the individual item.
    Code:
    lockers =[False for ctr in range(100)]  ## all lockers closed
    for student in range(100):
        for reverse in range(student, 100):  ## start at current student
            lockers[reverse] = not lockers[reverse]
    
    print lockers[:50]
    print lockers[50:]
    Also
    skipValue = skipValue+1
    is a different variable than the one used in the for loop even though they have the same name, so can be deleted.



    What does ctr stand for?
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    Addressing post 1:
    Code:
    while startValue <= studentLimit: 
        for i in range(startValue,len(isClosed),skipValue):
            if isClosed[i] == "False":
                isClosed.remove(i,"False")
    I presume you remove the ith item from the list. That's all the information needed. Hence the argument "False" is not required. Indeed, it isn't even permitted.

    Addressing post 1: The string "False" has type str . dwblas's Booleans True and False have type bool . Problem description requested type bool .

    Addressing post 17 minutes after the hour:
    lockers =[False for ctr in range(100)]
    dwblas used a list comprehension. `ctr' is a temporary variable that takes values of the iterable range(100) . `ctr' is unused since dwblas is making a constant list, and no variables are needed in the expression to generate items. Another way to make the initial list is
    lockers = [False]*100
    [edit](which you did in the your first post but didn't realize that 100 * ["False"] is already a list)[/edit]
    Last edited by b49P23TIvg; November 13th, 2013 at 11:31 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo