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.