Invalid syntax python problem (if statement many actions) - if-statement

I am encountering a problem on my python code. I'm trying to increment and decrement two values after an if statement. However, this couldn't be achieved (Error: invalid syntax). Knowing that when I increment only a single value (e.g. A_p), it works well. Does the error come from the and operator? Is there a way to perform multiple actions after an if statement?
Thank you.
Omer
k1 = 0.2
k2 = 0.2
Delta_t = 2
A_p = 4
B_p = 3
list_nm = [0.800, 0.801, 0.752, 0.661, 0.169, 0.956, 0.949, 0.003, 0.201, 0.291, 0.615, 0.131, 0.241, 0.685, 0.116, 0.241, 0.849]
for i in range(7):
if list_nm[i] < (A_p/7):
print("Particle A was choosen")
if list_nm[i] < k1*Delta_t:
A_p -= 1 and B_p += 1
else:
A_p = A_p and B_p = B_p
elif list_nm[i] < (B_p/7):
print("Particle B was choosen")
if list_nm[i] < k2*Delta_t:
A_p +=1 and B_p -=1
else:
A_p = A_p and B_p = B_p
else:
print("No choice was made")
A_p = A_p and B_p = B_p
print(B_p)

A_p -= 1 and B_p += 1
is not valid Python, and is a specific keyword used for combining booleans, such as:
if a > 3 and a < 7:
pass
What you're after instead is two separate statements:
A_p -= 1
B_p += 1
or, if you must have it on a single line (and I'd question why you think this is necessary), one of the following, depending on what you prefer:
A_p -= 1 ; B_p += 1
(A_p, B_p) = (A_p - 1, B_p + 1)
In addition, the line:
A_p = A_p and B_p = B_p
even when you split it into two statements, is effectively doing nothing. You may as well remove it totally, along with the else: line preceding it if that's the only thing in the else block.
In other words, start with something this:
(k1, k2, Delta_t) = (0.2, 0.2, 2)
(A_p, B_p) = (4, 3)
list_nm = [0.800, 0.801, 0.752, 0.661, 0.169, 0.956, 0.949, 0.003, 0.201, 0.291, 0.615, 0.131, 0.241, 0.685, 0.116, 0.241, 0.849]
for i in range(7):
if list_nm[i] < (A_p / 7):
print("Particle A was choosen")
if list_nm[i] < k1 * Delta_t:
A_p -= 1
B_p += 1
elif list_nm[i] < (B_p / 7):
print("Particle B was choosen")
if list_nm[i] < k2 * Delta_t:
A_p += 1
B_p -= 1
else:
print("No choice was made")
print(B_p)

Related

How to get all solutions for an integer program in ortools?

I am trying to get all solutions for a Mixed Integer program through ortools. I have two lists x and y of size 4. I want to get all solutions which satisfy sum(x) = 4 * sum(y). I created a function which takes list of past solutions as input and returns next solution. I am able to get only 2 solutions even though there are more. What am I doing wrong here?
I am expecting the following solutions
Solution 1:
xs1 = [0,0,0,0], ys1 = [0,0,0,0]
Solution 2:
xs2 = [4,0,0,0], ys2 = [1,0,0,0]
Solution 3:
xs3 = [0,4,0,0], ys3 = [1,0,0,0]
Solution 4:
xs4 = [0,0,4,0], ys4 = [0,0,1,0]
and soon on
from ortools.linear_solver import pywraplp
def opt(xs, ys):
solver = pywraplp.Solver.CreateSolver('SCIP')
infinity = solver.infinity()
# x and y are integer non-negative variables.
n = 4
M = 20
x = [0]* n
y = [0]* n
w = [[0]* n]*len(xs)
δ = [[0]* n]*len(xs)
for i in range(0,n):
x[i] = solver.IntVar(0, 20, 'x'+str(i))
y[i] = solver.IntVar(0, 20, 'y'+str(i))
for j in range(len(xs)):
w[j][i] = solver.IntVar(0, 20, 'zp'+str(j)+ '-' + str(i))
δ[j][i] = solver.IntVar(0, 1, 'δ'+str(j)+ '-' + str(i))
for j in (range(len(xs))):
for i in range(0,n):
solver.Add((w[j][i] - x[i] + xs[j][i]) >=0)
solver.Add((w[j][i] - x[i] + xs[j][i]) <= M*(1-δ[j][i]))
solver.Add((w[j][i] + x[i] - xs[j][i]) >=0)
solver.Add((w[j][i] + x[i] - xs[j][i]) <= M*δ[j][i])
for j in range(len(xs)):
solver.Add(solver.Sum([w[j][i] for i in range(0,n)]) >= 1)
solver.Add(solver.Sum([x[i] for i in range(0, n)]) - 4 * solver.Sum([y[i] for i in range(0, n)]) == 0)
solver.Minimize(solver.Sum([x[i] for i in range(0, n)]))
status = solver.Solve()
if status == pywraplp.Solver.OPTIMAL:
solver_x = [0]*n
solver_y = [0]*n
for i in range(0,n):
solver_x[i] = x[i].solution_value()
solver_y[i] = y[i].solution_value()
return ([solver_x, solver_y, solver.Objective().Value()])
else:
print('No Solution')
return ([[0], [0]], -1)
psx = [[0,0,0,0], [0,4,0,0]]
psy = [[0,0,0,0], [1,0,0,0]]
ns = opt(psx, psy)
print(ns)
Output:
No Solution
([[0], [0]], -1)
Reference:
Finding multiple solutions to general integer linear programs
How to write constraints for sum of absolutes
If you have a pure integer programming model, you can use the CP-SAT solver which allows you to print all the solutions [See this].

File "solution.py", line 36 return B1 SyntaxError: 'return' outside function. Genuine

class Solution:
# #param a : list of list of integers
# #return a list of list of integers
def diagonal(self, a):
mylen = len(a)
t_v = 2*len(a)
ret_val = 2*mylen-1
B1=[[0 for x in range(y)] for y in range(1,t_v/2+1)]
B2=[[0 for x in range(y)] for y in range(t_v/2-1,0,-1)]
for i in range((t_v/2)-1):
B1.append(B2)
for i in range(t_v/2):
c=i
vark=0
for j in range(i+1):
B1[i][vark] = a[j][c]
vark += 1
c -= 1
myindex=t_v/2
someVal=1
for i in range(t_v/2-1, 0, -1):
c=len(a)-1
vark=0
mytrack=someVal
for j in range(i):
B1[myindex][j] = a[mytrack][c]
mytrack += 1
c -= 1
myindex +=1
someVal += 1
return B1

Plot specific lines for specific values with Pyplot

I'm trying to plot two files of data of this type:
name1.fits 0 0 2.40359218172
name2.fits 0 0 2.15961244263
The third column has values from 0 to 5. I want to plot column 2 vs column 4, but, for lines with values in col 3 less than 2 (0 and 1), I want to shift col 2 by -0.1, and for lines with values greater than 3 (4 and 5) I want to shift col 2 by +0.1.
However my code seems to be shifting all values by +0.1. Here is what I have so far:
import matplotlib.pyplot as plt
import numpy as np
with open('file1.txt') as data, open('file2.txt') as stds:
lines1 = data.readlines()
lines2 = stds.readlines()
x1a = []
x2a = []
x1b = []
x2b = []
x1c = []
x2c = []
y1a = []
y2a = []
y1b = []
y2b = []
y1c = []
y2c = []
for line1 in lines1:
p = line1.split()
if p[2] < 2:
x1a.append(float(p[1]))
y1a.append(float(p[3]))
elif 1 < p[2] < 4:
x1b.append(float(p[1]))
y1b.append(float(p[3]))
elif p[2] > 3:
x1c.append(float(p[1]))
y1c.append(float(p[3]))
for line2 in lines2:
q = line2.split()
if q[2] < 2:
x2a.append(float(q[1]))
y2a.append(float(q[3]))
elif 1 < q[2] < 4:
x2b.append(float(q[1]))
y2b.append(float(q[3]))
elif q[2] > 3:
x2c.append(float(q[1]))
y2c.append(float(q[3]))
x1a = np.array(x1a)
x2a = np.array(x2a)
x1b = np.array(x1b)
x2b = np.array(x2b)
x1c = np.array(x1c)
x2c = np.array(x2c)
y1a = np.array(y1a)
y2a = np.array(y2a)
y1b = np.array(y1b)
y2b = np.array(y2b)
y1c = np.array(y1c)
y2c = np.array(y2c)
minorLocator = AutoMinorLocator(5)
fig, ax = plt.subplots(figsize=(8, 8))
fig.subplots_adjust(left=0.11, right=0.95, top=0.94)
plt.plot(x1a-0.1,y1a,'b^',mec='blue',label=r'B0',ms=8)
plt.plot(x2a-0.1,y2a,'r^',mec='red',fillstyle='none',mew=0.8,ms=8)
plt.plot(x1b,y1b,'bo',mec='blue',label=r'B0',ms=8)
plt.plot(x2b,y2b,'ro',mec='red',fillstyle='none',mew=0.8,ms=8)
plt.plot(x1c+0.1,y1c,'bx',mec='blue',label=r'B0',ms=8)
plt.plot(x2c+0.1,y2c,'rx',mec='red',fillstyle='none',mew=0.8,ms=8)
plt.axis([-1.0, 3.0, 0., 4])
ax.xaxis.set_tick_params(labeltop='on')
ax.yaxis.set_minor_locator(minorLocator)
plt.show()
Here is the plot:
plot
I'm pretty sure the problem is in my "ifs". I hope you can clear the way and/or show me a better option for this.
When you do your queries (if) you must ensure the conversion happens before the question so:
for line1 in lines1:
p = line1.split()
if p[2] < 2:
x1a.append(float(p[1]))
y1a.append(float(p[3]))
elif 1 < p[2] < 4:
x1b.append(float(p[1]))
y1b.append(float(p[3]))
elif p[2] > 3:
x1c.append(float(p[1]))
y1c.append(float(p[3]))
, should actually be:
for line1 in lines1:
p = line1.split()
if float(p[2]) < 2: # changed here
x1a.append(float(p[1]))
y1a.append(float(p[3]))
elif 1 < float(p[2]) < 4: # There seems to be a problem with this if
x1b.append(float(p[1]))
y1b.append(float(p[3]))
elif float(p[2]) > 3: # changed here
x1c.append(float(p[1]))
y1c.append(float(p[3]))
The same for your q variables. Also notice that asking 1 < x < 4 will intercept with x > 3 and x < 2. You should also correct this.

Syntax error for else and elif (Can't determine if structure issue)

I'm very new to python and I've been working on a basic calculator within python for the last few hours (rhetorical I know, given what python has built in, but it's part of my learning process), I've run into an error I can't seem to fix, generally I'm able to get my scripts on their feet and running with the assistance of a couple of Google searches but this one has me stumped. I'm getting a syntax error where I have an else, and while at first I was pretty sure it was a structure issue, rewriting the script didn't fix anything, vague I know, so here's the script (I've marked the spot with a comment) :
def Calculator():
tempnums = [] #stores nums only
tempfuncs = [] #stores funcs only
tmpfuncs = {} #stores funcs only
times = lambda multiply: tempnums[0]*tempnums[1]
div = lambda divide: tempnums[0]%tempnums[1]
plus = lambda add: tempnums[0]+tempnums[1]
minus = lambda subtract:tempnums[0]-tempnums[1]
done = 0
varnum = 0
xtimes = 0
divtimes = 0
plustimes = 0
mintimes = 0
while done == 0: #USER INPUT PROCESS
varnum = varnum + 1
tempint = input() #nums
exec("num%d = tempint" % (varnum))
function = raw_input() #functions
if function != "=":
if function == 'x':
if x not in tmpfuncs:
xtimes = xtimes + 1
tmpfuncs[x] = times
else:
xtimes = xtimes + 1
exec("tmpfuncs[x%d] = times" % (xtimes)
else: #ERROR COMES HERE
if function == '//':
if dv not in tmpfuncs:
divtimes = divtimes + 1
tmpfuncs[dv] = div
else:
divtimes = divtimes + 1
exec("tmpfuncs[dv%d] = div" % (divtimes)
if function == '+':
if pls not in tmpfuncs:
plustimes = plustimes + 1
tmpfuncs[pls] = plus
else:
plustimes = plustimes + 1
exec("tmpfuncs[pls%d] = plus" % (plustimes)
if function == '-':
if mn not in tmpfuncs:
mintimes = mintimes + 1
tmpfuncs[mn] = minus
else:
mintimes = mintimes + 1
exec("tmpfuncs[mn%d] = minus" % (mintimes)
else: #user has selected to calculate input
done = 1
for i in range(1, varnum + 1):
exec("tempnums.append(num%d)" % (i)) #adding nums to a list with dynamic var names
print tmpfuncs
#next we'll make it so that tempnums[0] is the changing result as we use tempnums 0 and 1 to calculate the answer, deleting one as we go until there is only zero
Calculator()
Calculator()
I'm hoping this is legible as I'm posting from mobile (as a matter of fact I'm writing this from mobile as well).
The line above the else is missing a closing parens:
exec("tmpfuncs[x%d] = times" % (xtimes)
should be
exec("tmpfuncs[x%d] = times" % (xtimes))
The same error occurs in many other of your exec lines. Also, I suggest you consider restructuring your code so you do not need to use exec at all.

Merge Sort returns the same array in Python

for merge sort i wrote this code:
I have tested merge function that works correctly. but in mergeSort function i coudn't handle the arrays. it returns the same list as the input list.
def mergeSort(a):
l, h = 0, len(a)-1
mid = (l+h)/2
if (l<h-1): #the lowest length must be 2
mergeSort(a[l:mid+1])
mergeSort(a[mid+1:h+1])
return merge(a[l:mid+1],a[mid+1:h+1])
def merge(a,b):
n_a = len(a)
n_b = len(b)
c = [[] for i in range(n_a + n_b)]
i,j,k=0,0,0
while (i<n_a and j<n_b):
if a[i]<b[j]:
c[k] = a[i]
i += 1
else:
c[k]= b[j]
j += 1
k += 1
while(i<n_a):
c[k] = a[i]
k+=1
i+=1
while(j< n_b):
c[k] = b[j]
k+=1
j+=1
return c
I would rewrite merge as so:
def mergeSort(a):
h = len(a)
mid = h / 2
if h >= 2:
return merge(mergeSort(a[:mid]), mergeSort(a[mid:]))
else:
return a
A few notes:
l is always 0, might as well remove it
h is len(a) - 1, but then you use h + 1, might as well use h = len(a)
Actually writting h >= 2 makes it clearer that you need at least 2 items in your list
mid can be len(a) / 2.
when taking the complete beginning/end of an array in a slice, the first/last bound is not required