Home > OS >  how do i remove the Nth element from my list until there's only one element remains and print t
how do i remove the Nth element from my list until there's only one element remains and print t

Time:10-15

n1 = input("player1 name: ")
n2 = input("player2 name: ")

sum = 0

def removecommonchr(n1, n2):
    my_str = ''
    for w in set(n1):
        if w not in n2:
            my_str  = w 

    my_str2 = ''
    for w in set(n2):
        if w not in n1:
            my_str2  = w 

    l1 = ["A1", "B2", "C3", "D4", "E5", "F6"]
    while True:
        if len(l1) == 1:
            break

        e = (len(my_str))   (len(my_str2))
        sum =  e
        l1.pop(e)
        print(l1)

removecommonchr(n1, n2)

after removing the 5th item in the list, I want the program keep counting and poping/removing the 5th element. With N being the sum from length of str and str2, I want to remove the Nth item from the list ["A1", "B2", "C3", "D4", "E5", "F6"]. Repeat this, wrapping around the list, and starting each time with the item that comes after the last removed item.

here's what i want to do:

[“A1”, “B2”, “C3”, “D4”, “E5”, “F6”]
counting starts from A1, E5 is at 5th count so I remove E5 and start counting again but this
time start from F6.

[“A1”, “B2”, “C3”, “D4”, “F6”]
D4 is at 5th count so I remove D4 and counting starts from F6.

[“A1”, “B2”, “C3”, “F6”]
F6 is at 5th count so I remove F6 and counting starts from A1.

[“A1”, “B2”, “C3”]
B2 is at 5th count so I remove B2 and counting starts from C3.

[“A1”, “C3”]
C3 is at 5th count so I remove C3. Now I have only one letter remaining, so this is the final
answer.

[“A1”]
print the final answer

I want my code to run like this but I don't know how to continuously remove the 5th count on the list, and I'm stuck with my current code above:

sample run:

player1 name: kristine
player2 name: christian
['A1', 'B2', 'C3', 'D4', 'E5']
Traceback (most recent call last):
  File "D:\Work\lab\3.py", line 27, in <module>
    removecommonchr(n1, n2)
  File "D:\Work\lab\3.py", line 24, in removecommonchr
    l1.pop(e)
IndexError: pop index out of range

after removing F6, it prompts an error, how to I fix this?

CodePudding user response:

You need to maintain the index at which the player is removed and your k to it (5 in this case), when the length of list becomes smaller that the number, take mod of the number with the length of the list, so that it gives you the next index which needs to be removed.

l1 = ["A1", "B2", "C3", "D4", "E5", "F6"]
k = 5
currentIndex = 0
while len(l1) != 1:
    currentIndex = (currentIndex   k - 1) % len(l1)
    l1.pop(currentIndex) 
    print(l1)

Output:
['A1', 'B2', 'C3', 'D4', 'F6']
['A1', 'B2', 'C3', 'F6']
['A1', 'B2', 'C3']
['A1', 'C3']
['A1']

CodePudding user response:

The naive fix is to keep rotating your index within the current length. Instead of

sum =  e

do

sum = (sum   e) % len(l1)

However, the easiest and probably most performant approach would use deque.rotate:

from collections import deque

q = deque(["A1", "B2", "C3", "D4", "E5", "F6"])
e = 5

while len(q) > 1:
    q.rotate(1-e)
    print(q.popleft())
print(q)

E5
D4
F6
B2
C3
deque(['A1'])

CodePudding user response:

Keep in mind that pop() removes the item, and the list changes length after the pop. So, you can just use pop() with no arguments for the end item. I mean you have to change l1.pop(e) to l1.pop() in your code.

  • Related