I wrote a program for solving the 27th issue from the exam example. I can't understand why, when the selection condition changes inside the loop, it gives a completely different number

Initial version:

```
File= open ('27 -B.txt ',' r ')
file_read= File.read ()
file_list= file_read.split ()
file_list= [int (i) for i in file_list]
file_list.pop (0)
res= 0
for i in range (0, len (file_list), 2):
i1= file_list [i]
i2= file_list [i + 1]
if (i1 >
= i2) and (((res + i1)% 3)== 0):
res= res + i1
elif ((res + i2)% 3)== 0:
res= res + i2
print (res)
File.close ()
```

Changed elif to if:

```
File= open ('27 -B.txt ',' r ')
file_read= File.read ()
file_list= file_read.split ()
file_list= [int (i) for i in file_list]
file_list.pop (0)
res= 0
for i in range (0, len (file_list), 2):
i1= file_list [i]
i2= file_list [i + 1]
if (i1 >
= i2) and (((res + i1)% 3)== 0):
res= res + i1
if (i2 >
i1) and (((res + i2)% 3)== 0):
res= res + i2
print (res)
File.close ()
```

yes, but if the first if worked, then the second should not be due to the fact that the first condition should not be met for it

incandescent2021-02-23 13:50:35in the second variant, elif does not contain the condition (i2>i1), i.e. in the first, if (((res + i1)% 3)== 0) fails, additionally check only ((res + i2)% 3)== 0, and (i2>i1) may be false ... that is ... conditions are still different

Jack_oS2021-02-23 14:09:29if i1>= i2, then i2>i1 cannot be true, and if i1>= i2 is false, then i2>i1 must be true, therefore it is not in elif

incandescent2021-02-23 14:39:11let's try again ... 1st option: (i1>= i2) and (((res + i1)% 3)== 0) returned true &false, result false (res + i1 is not a multiple of 3), went to elif (for i1>= i2), but only (res + i1)% 3 was checked in it ... try nesting ifs in each other (if (i1>= i2): if (((res + i1)% 3 )== 0): etc.), maybe it becomes clearer

Jack_oS2021-02-23 14:48:06-
**Answer # 1**Do I understand correctly that the solution to the problem looks like this:

`arr= [(1, 3), (5, 12), (6, 9), (5, 4), (3, 3), (1, 1)] res= sum (max (obj [0], obj [1]) for obj in arr) if res% 3== 0: res= res -sorted (abs (obj [1] -obj [0]) for obj in arr if (obj [0] -obj [1])% 3!= 0) [0] print (res)`

but you can do it head-on:

`res= 0 delta= 0 for obj in arr: res += obj [1] if obj [1] > obj [0] else obj [0] delta= abs (obj [1] -obj [0]) if (delta > abs (obj [1] -obj [0]) or delta== 0) and obj [0]!= obj [1] else delta res= res if res% 3!= 0 else res -delta print (res)`

I still can't figure out if it's right)

incandescent2021-02-23 13:55:07@incandescent, well, everything is simple -first we get the maximum possible amount as the sum of the maximum elements in pairs, and then if the sum is divisible by 3, we subtract the minimum from the difference in pairs that is not a multiple of 3, which gives us an amount that is not a multiple of 3. Why do we need delta? because this is essentially replacing one number of a pair with a second (subtract the first number from the sum and add the second, which is equivalent to simply subtracting the delta of these two numbers) :))) I gave the second code -it is a little more authentic, but it does the same thing and works with this is a little faster, since everything is done in 1 pass

Zhihar2021-02-23 13:59:34

in if -elif with true in if, what will not be checked in elif, with if -if -both are checked and, in the case of true in the second if, will change your res

Jack_oS2021-02-23 13:35:00