0

I'm really confused and I don't know how to solve the following problem.

Imagine that I have written a very long code and I want to transform this long code to a function to prevent repetition. The problem is that I need to give 'add sign' (+) and 'subtraction sign'(-) as function inputs. I mean that I have a very long code that I need to change two signs of add and subtraction for each time and nothing more and finally to get the output according changing these two operators (these two operators are in fact my variables!)

As you know repeating this long function for four different states of changing 'add and subtraction signs', makes my code even longer and longer. I would like to know if there is any way to define just One function that gives these pair of add and subtraction operators as input and give me the output for these four different conditions? I have made a simple example to clarify the question:

Imagine that I have four similar 'for loops' just different in 'minus sign' and 'plus sign':

edges=set()
a=60; b=40
for i in range(0,300):
    a+=1; b+=1
    if a!=0 and b!=0:
        edges.add((a, b))

a=60; b=40
for i in range(0,300):
    a-=1; b-=1
    if a>=0 and b>=0:
        edges.add((a, b))

a=60; b=40
for i in range(0,300):
    a+=1; b-=1
    if a>=0 and b>=0:
        edges.add((a, b))

a=60; b=40
for i in range(0,300):
    a-=1; b+=1
    if a>=0 and b>=0:
        edges.add((a, b))
print(edges)

As you see, these four 'for loops' are all the same except in 'plus and minus signs'. I want to write just one function instead of these four repeated 'for loops'. Is there any other way to solve this problem? Anyone has any idea?

Sara
  • 163
  • 2
  • 10

4 Answers4

7

Write the function to accept increments as parameters.

def add_edges(aincr, bincr):
    for i in range(0,300):
        a += aincr; b += bincr
        if a!=0 and b!=0:
            edges.add((a, b))

Then pass in the appropriate increments when calling:

add_edges(1, 1)
add_edges(-1, -1)
add_edges(1, -1)
add_edges(-1, 1)

There is no need to pass the concept of "add" or "subtract", since you can do that numerically. You could (as other answers show), but try not to overcomplicate things.

kindall
  • 178,883
  • 35
  • 278
  • 309
2

You can create add_1 and minus_1 as functions and pass them as args into another function:

def add_1(x):
    return x + 1

def minus_1(x):
    return x - 1

def do_op(a, b, f1, f2):
    for i in range(0,300):
        a, b = f1(a), f2(b)
        if a>=0 and b>=0:
            edges.add((a, b))

do_op(60, 40, add_1, add_1)
do_op(60, 40, minus_1, minus_1)
do_op(60, 40, add_1, minus_1)
do_op(60, 40, minus_1, add_1)
print(edges)

You can pass in lambdas as well directly:

do_op(60, 40, lambda x:  x + 1, lambda x:  x + 1)
do_op(60, 40, lambda x:  x - 1, lambda x:  x + 1)
do_op(60, 40, lambda x:  x + 1, lambda x:  x - 1)
do_op(60, 40, lambda x:  x - 1, lambda x:  x - 1)
Pavel Savine
  • 189
  • 8
1
import operator
ops = { "+": operator.add, "-": operator.sub } # etc.

def do_math(a, b, s):
    print(ops[s](a, b))
    
do_math(5, 3, '-')

Disclaimer: I used this post as a reference.

Neuron
  • 5,141
  • 5
  • 38
  • 59
NBlaine
  • 493
  • 2
  • 11
1

Subtracting is the same as adding negative value, so:

def operation(first_val, second_val, add=True):
    second_val = second_val if add else -second_val
    return first_val + second_val

or using += notation:

def increment_decrement(value, add=True):
    step = 1 if add else -1
    value += 1
    return value
papson
  • 11
  • 1