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?