Python Forum
Multiply and Addition in the same loop statement with logic.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiply and Addition in the same loop statement with logic.
#1
Hi Python Community,

Greetings to you all !

After the if statement is passed through Reven and Rodd have to take two alternative indexes: one has to do multiply, and another has to do add.

like example,

Output:
array has {0,1,2,3,4,5,6,7,8,9} Reven = ((((A[0]*A[2])+A[4])*A[6])+A[8]) like 0 +2 2 *4 8 +6 14 *8 array has {0,1,2,3,4,5,6,7,8,9} Reven = ((((A[1]*A[3])+A[5])*A[7])+A[9]) like 1 + 3 4 * 5 20 +7 27 * 8
wriiten code

def AddMult(A):
    Reven = 0
    Rodd = 0
    for i in range(len(A)):
        if i % 2 == 0:
            Reven = Reven + A[i]
        else:
            Rodd = Rodd + A[i]

    E = (Reven%2)
    O = (Rodd%2)
    if(O > E):
        return "ODD"
    elif(E > O):
        return "EVEN"
    elif(O == E):
        return "NEUTRAL"
Could you please help me solve this?
buran write Jan-31-2023, 10:02 AM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply
#2
Buddy, use the 'insert Python' button to post your snipped. Wink
Reply
#3
Which is it?
Reven = ((((A[0]*A[2])+A[4])*A[6])+A[8])
Reven = (0 * 2 + 4) * 6 + 8 = 32
0 +2
2 *4
8 +6
14 *8
Reven = ((0 + 2) * 4 + 6) * 8 = 112

When solving something like this I perform the operation by hand and look for a pattern.

values = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
result = [0, 0] # result for even, odd

Output:
index value operator result Description 0 0 + [0, 0] result[0] += 0 1 1 + [0, 1] result[1] += 1 2 2 * [0, 1] result[0] *= 2 3 3 * [0, 3] result[1] *= 3 4 4 + [4, 3] result[0] += 4 5 5 + [4, 8] result[1] += 5 6 6 * [24, 8] result[0] *= 6 7 7 * [24, 56] result[1] *= 7 8 8 + [32, 56] result[0] += 8 9 9 + [32, 65] result[1] += 9
The index for result[index] has a period 2 cycle: 0, 1
The operator has a period 4 cycle: +, +, *, *

Using that information I might write the function like this:
from operator import add, mul

def add_mul(numbers):
    result = [0, 0]
    for i, number in enumerate(numbers):
        result[i % 2] = (add, add, mul, mul)[i % 4](result[i % 2], number)
    return result
I don't quite understand the even/odd evaluation at the end, but this code:
    E = (Reven%2)
    O = (Rodd%2)
    if(O > E):
        return "ODD"
    elif(E > O):
        return "EVEN"
    elif(O == E):
        return "NEUTRAL"
Can be written like this:
even, odd = (result % 2 for result in add_mul(range(10)))
print("NEUTRAL" if (even == odd) else "EVEN" if even else "ODD")
In Python, 0 is falsey and 1 is truey. If you know the only possible values are 0 and 1 you don't need to do comparisons like E > O.

And about using "O" as a variable. Don't do it. Capital o looks like zero, just like lower case L looks like one. Avoid using either to avoid any confusion.
tester_V likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Optimise multiply for loop in Python KenBCN 4 428 Feb-06-2024, 06:48 PM
Last Post: Gribouillis
Photo Python code: While loop with if statement HAMOUDA 1 535 Sep-18-2023, 11:18 AM
Last Post: deanhystad
  forloop to compute sum by alternating from addition to subtraction JulianZ 3 1,764 Apr-02-2022, 09:36 AM
Last Post: DeaD_EyE
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,768 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  how to create pythonic codes including for loop and if statement? aupres 1 1,889 Jan-02-2021, 06:10 AM
Last Post: Gribouillis
  if statement in for loop researcher123 6 2,508 Oct-01-2020, 05:07 PM
Last Post: deanhystad
  Help: list comprehension for loop with double if statement mart79 3 2,382 May-04-2020, 06:34 AM
Last Post: buran
  Need help with For Loop logic for reading docx Table column data vintysaw 2 3,834 Jan-10-2020, 06:36 AM
Last Post: vintysaw
  (Python help) Change in logic not breaking 'while' loop? btcg2807 1 1,863 Sep-18-2019, 09:43 AM
Last Post: Larz60+
  addition for elements in lists of list ridgerunnersjw 3 3,046 Sep-15-2019, 07:11 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020