I want to swap a number divisible by 2 with the next number divisible by 5 in a list

Viewed 351

Example 1

Source: [2, 4, 2, 4, 5, 10]

Expected: [5, 10, 2, 4, 2, 4]
Obtained: [5, 10, 2, 4, 2, 4]

My code gives me an accurate result for this combination.

Example 2

Source: [2, 2, 5, 5, 4, 5, 5, 6, 5]

Expected: [5, 5, 2, 2, 5, 4, 5, 6]
Obtained: [5, 5, 5, 5, 5, 2, 2, 6, 4]

My code did not provide the desired outcome here.

Example 3

Source: [2, 4, 5, 7, 10, 13, 17, 19, 15]

Expected: [5, 10, 2, 7, 4, 13, 17, 19, 15]
Obtained: [5, 10, 15, 7, 4, 13, 17, 19, 2]

If the number is switched once, it should be left undisturbed (my knowledge of python is highly rudimentary...I started a few days ago).

My Source Code

a = []
b = a.copy()
for div_by_2 in a:

#print('entering for loop to check divisibility by 2 for the number ' , div_by_2 , ' in the list')
if div_by_2 % 2 == 0:

    #print('entering if in for loop of checking  divisibility by 2 as ' , div_by_2 , 'is divisible by 2')
    #print(a.index(div_by_2) , ' is the index of the number divisible by 2')
    #print(('begining of value of check five for loop is ' ,a.index(div_by_2)))
    for check_five in range (a.index(div_by_2),len(a)):
        #print('entering for loop to check five')
        #print(check_five , 'is index of number being checked for divisibility by 5')
        #print(a[check_five], ' is the numerical value of number divisible by 5 ')

        if a[check_five] % 5 == 0:

            #print('entering if in check 5 for loop')
            div_by_5 = a[check_five]

            #print(div_by_5, ' is divisible by 5')
            #print(('index being replaced is ', a.index(div_by_2)) , ' with value' , a[check_five])
            #print('the number divisible by 5 is being replaced with , ' ,a[div_by_2])
            a[a.index(div_by_2)] , a[check_five] =a[check_five] , div_by_2

            #print('        list updated! as            ' , a)
            break
    print('the original list was' , b)
    print('the final list is' , a)

It should also replace 5m with 2n (where m, n belongs to natural numbers),not just one way around.

I am stuck. I have tried many ways and went with this as this is as far as I could go. Any help would be appreciated.

2 Answers

I'd go about the problem differently:

  • iterate over the list
  • if number not divisible by 2 -> push number to first_list
  • if number divisible by 2 -> assign it to divisible_by_2 variable
    • continue iterating
    • if not divisible by 5 -> push number to second_list
    • if divisible by 5 -> push number to first_list
      • concatenate second_list to first_list
      • push divisible_by_2 variable to first_list
      • continue iterating and push all following numbers to first_list

Let me know if this works for you:

a =  [2,2,5,5,4,5,5,6,5]
lis=a.copy()
b = [i%5 for i in a]
c = [i%2 for i in a]
parsed = []

for i in range(len(a)):
    if i not in parsed:
        if c[i]==0:
            if 0 in b:
                parsed.append(b.index(0))
                a[i],a[b.index(0)]=a[b.index(0)],a[i]
                b[b.index(0)]=1
        elif b[i]==0:
            b[i]=1
print('Original list:',lis)
print('Final list',a)
Related