0
Not expected output shown
n=8 list=[3,21,5,6,14,8,14,3] #expected output=[3,5,21,6,8,14,3,14] for i in range (0,n-1,1): if list[i]%7==0: list[i],list[i+1]=list[i+1],list[i] i=i+1 print(list)
23 Answers
+ 4
A for...loop uses a fixed range and stepping, but in this case we have to adjust the loop counter <i> sometimes only, not always. That happens when element at index <i> is multiple of 7. A while...loop is more suitable IMHO when we need to adjust loop counter in various ways, maybe even with different stepping values.
+ 1
As per given expected output it is not clear what you want.
+ 1
Because the loop counter (index) needs adjustment when an element at the said index is fully divisible by 7. Otherwise I'd be swapping elements at the unintended index next time around.
+ 1
Thanks a ton Ipang
+ 1
Ok so what I'm seeing is if list[n] is a multiple of seven, have it swap places with list[n+1] and increase the loop counter by 2, otherwise do nothing and increase counter by 1. Do I have it right?
+ 1
Bob
That's right!
If element[ i ] is multiple of seven then we swap element value with the next one ( element [ i + 1 ] ), and increment <i> because we want to skip element[ i + 1 ] next time around.
We rather want to continue checking multiples of 7 again from element[ i + 2 ] because element [ i ] and element [ i + 1 ] had already been swapped, no need for a re-check.
+ 1
Ok, assuming I have it right, the i+=1 in your if statement is a different i than the iterator. The iterator can't(easily) be modified in the body of a for loop. If you need the iterator to change by a different amount based on a condition, then you have to use a while loop
0
Remember this is a QUESTION and answer section. So what exactly is your question and the issue you are facing?
0
Justice I have already mentioned expected output
0
Atul [Inactive] But you don't state the actual output. It's always best to share your code via codebits so that we don't try and guess.
0
Atul [Inactive] Or! Since you are the one asking for help, you can adjust your post to make it easier for others to help instead of making not just me or A͢J, but anyone interested in helping you that sees this question, do the work instead.
0
Ok Justice I will make it sure next time
0
Atul [Inactive] Can you explain what you want your algorithm to do exactly (not just the output). Also, how have you tried debugging this?
0
The question is swap the next value divisible by 7
0
Atul [Inactive] I would suggest adding some print statements to see what your code is doing. You can put one right before the conditional (if statement) as well as printing out what you presume to be swapping.
0
We need to adjust value of <i> when <i> is fully divisible by 7. I think use of while...loop is preferable in this case. We're not supposed to alter a for...loop counter, we use while...loop when we need to.
lst=[ 3, 21, 5, 6, 14, 8, 14, 3 ]
n = len( lst )
#expected output=[3,5,21,6,8,14,3,14]
i = 0
while i < n - 1:
if lst[ i ] % 7 == 0:
lst[ i ], lst[ i + 1 ] = lst[ i + 1], lst[ i ]
i += 1
i += 1
print( lst )
P.S. I would suggest you not to use keywords for variable names, like `list`. You will overwrite `list` definition and confuse yourself in bigger codes/projects when you need to use the original `list` keywords.
0
Ipang why u have written i+=1 two times ?And why only i+=1 when it is doing the work of skipping the next number?