15,848,453 members
See more:
when the program receive input for "cubes", I want to first check if the maximium value is on one of the end of the sequence, if yes, delete the max value, if not exit and print no, as long as 'd' is not empty, keep on looping, till 'd' is empty then print yes, or exit the program in one of the loop.

I can input 'T' number of test sequences.
suppose I give a sequense [4, 3 ,1, 3 ,4]

I use debugger and found out , while loop delete '4' in the first iteration, and directly goes to outside "for loop". what is the reason? thank you very much in advance!

```from collections import deque
T=int(input())#number of test cases
for i in range(T):
n=int(input())
cubes=map(int, input().split())
d=deque(cubes)

while len(d)!=0:
a=max(d)
if d.index(a)==0 or d.index(a)==-1:
d.remove(a)
else:
break
print(no)
print('yes')```

What I have tried:

tried different input, the result is the same
Posted
Updated 2-Sep-21 3:13am
v3
Maciej Los 2-Sep-21 7:33am
Why to loop over entire "cube"?
```inp = [4, 3 ,1, 3 ,4]
if inp[0] == inp[len(inp)-1]:
print('yes')
else:
print('no')
```
Zhengxi Jiang 2-Sep-21 7:39am
it is a game about put cubes together. there are rules: 1. you can only take the leftmost or the rightmost of the cubes. these integers reprenst the sidelength of the cubes. the cubes i on top of cube j has to smaller than the cubes j. if according to the rules i can build it, answer is yes, otherwise it is no, means not possible to build. but here the problem is the code, why the while loop not keep on delete 4, after that 3 and 1, it goes to the higher 'for loop'. in fact it works as if is no while loop ,

## Solution 1

Python
```if d.index(a)==0 or d.index(a)==-1:
d.remove(a)
else:
break     # this terminates the while loop
print(no) # this statement will never be executed```

Move the `print` statement before the `break`.

Change the while loop to the following:
Python
```while len(d)!=0:
a=max(d)
print(F'max: {a}, index: {d.index(a)}')
# check if the max value is at the beginning, or end of the list
if d.index(a)==0 or d.index(a)==len(d) - 1:
d.remove(a)
print(F'{a} removed')
else:
print('no')
break
print('yes: ', d)```

Note: the added print statements show exactly what happens.

[/edit]

v3
jsc42 2-Sep-21 12:58pm
I'm not a Python expert but ...
Doesn't d.remove(a) remove the item in position 'a', not the item with value 'a'?
So, if you have [4, 3, 1, 3, 4]
First Iteration: a = 4 (max n); d.index(a) == 0 and d.index(a) also == len(d) - 1; so you do d.remove(a) meaning remove item 4 which (using zero based subscripts) just coincidentally is one of the items that has a value of 4.
Second Iteration: a = 4 (because there were 2 '4's in the original list); d.index(a) == 0 but not == len(d) - 1; d.remove(a) once again tried to remove item 4, but your list now only has indicies 0 to 3 so the remove will fail.

If I understand the task, then I think the simplest way to implement want you need is to split the tests for d.index(a) into two 'if' branches and when one is true, remove it using the found index (which will be 0 or len(d) - 1)
Richard MacCutchan 3-Sep-21 3:29am
No, it removes the item with the value `a`: collections — Container datatypes — Python 3.9.7 documentation[^].

And on the second iteration, the expression `len(d) - 1`, still returns the correct length as `d` is now one item shorter, so it removes the `4` at the end. And I did run a few tests on the code before I posted it here.
Richard MacCutchan 3-Sep-21 4:00am
Here are the results for the list `[4, 3, 1, 3, 4]`
```        python cp.py
max: 4, index: 0
4 removed
max: 4, index: 3
4 removed
max: 3, index: 0
3 removed
max: 3, index: 1
3 removed
max: 1, index: 0
1 removed
yes:  deque([])
```
jsc42 3-Sep-21 6:13am
Thanks!