ans = num_rushes(100, 15, 7)
print(ans)
should return 19 but mine is returning 12?
Here is what I have:
def num_rushes(slope_height, rush_height_gain, back_sliding):
rushes = 0
current_height = 0
rush_height_counter = 0
back_sliding_counter = 0
rush_height_gain = 0.95 ** rush_height_counter * rush_height_gain
back_sliding = 0.95 ** back_sliding_counter * back_sliding
while current_height < slope_height:
current_height = current_height + rush_height_gain
rushes += 1
rush_height_counter += 1
back_sliding_counter += 1
if current_height < slope_height:
current_height = current_height - back_sliding
else:
return rushes
with this piece of code 0.95 ** rush_height_counter * rush_height_gain do you mean
0.95 ** (rush_height_counter * rush_height_gain)
or
(0.95 ** rush_height_counter) * rush_height_gain
I think you want the first one but python ( as I know ) do second one!
Related
I have the following code, which I'd like to optimize. Unfortunately, I can't find a way to gain speed or vectorize anything.
from scipy.sparse import diags
import numpy as np
def _compute_matrix_a(points, dw, dl, coefficient):
"""
"""
coefficient = coefficient.reshape(points, order='F')
extradl = coefficient[:,0] * 3
extradw = coefficient[0] * 3
#Build diagonals
diagonal_a = []
diagonal_b = []
diagonal_c = []
diagonal_d = []
diagonal_e = []
for idx, (k, i) in enumerate(product(range(points[1]), range(points[0]))):
if k == 0:
continue
if k <= 1:
pass
elif k < points[1] - 1:
a = 1 / (4 * dl**2) * (coefficient[i][k-1] - coefficient[i][k+1])\
+ coefficient[i][k] / dl**2
diagonal_a.append(a)
else:
a = coefficient[i][k-1] / (dl + extradl[i])**2\
+ coefficient[i][k] / (dl**2 / 2 + extradl[i]**2 / 2)
diagonal_a.append(a)
if k == 0 or (k == 1 and i == 0):
pass
elif i == 0:
b = 0
diagonal_b.append(b)
elif i < points[0] - 1:
b = 1 / (4 * dw**2) * (coefficient[i-1][k] - coefficient[i+1][k]) \
+ coefficient[i][k] * (1 / dw**2 - 1 / (2 * i * dw**2))
diagonal_b.append(b)
else:
b = coefficient[i-1][k] / (dw + extradw[k]) ** 2 \
+ coefficient[i][k] / (dw**2 / 2 + extradw[k]**2 / 2) \
- coefficient[i][k] / (i * dw * (dw + extradw[k]))
diagonal_b.append(b)
if k == 0:
pass
elif i == 0:
if k < points[1] - 1:
c = - 4 * coefficient[i][k] / dw**2\
- 2 * coefficient[i][k] / dl**2
else:
c = - 4 * coefficient[i][k] / dw**2\
- 2 * coefficient[i][k] / (dl**2 / 2 + extradl[i]**2 / 2)
diagonal_c.append(c)
elif i < points[0] - 1:
if k < points[1] - 1:
c = - 2 * coefficient[i][k] / dl**2 \
- 2 * coefficient[i][k] / dw**2
else:
c = - 2 * coefficient[i][k] / dw**2 \
- 2 * coefficient[i][k] / (dl**2 / 2 + extradl[i]**2 / 2)
diagonal_c.append(c)
else:
if k < points[1] - 1:
c = - 2 * coefficient[i][k] / (dw**2 / 2 + extradw[k]**2 / 2) \
- 2 * coefficient[i][k] / dl**2
else:
c = - 2 * coefficient[i][k] / (dw**2 / 2 + extradw[k]**2 / 2) \
- 2 * coefficient[i][k] / (dl**2 / 2 + extradl[i]**2 / 2)
diagonal_c.append(c)
if k == 0 or (k == points[1] - 1 and i == points[0] - 1):
pass
elif i == 0:
d = 4 * coefficient[i][k] / dw**2
diagonal_d.append(d)
elif i == points[0] - 1:
d = 0
diagonal_d.append(d)
else:
d = 1 / (4 * dw ** 2) * (coefficient[i+1][k] - coefficient[i-1][k]) \
+ coefficient[i][k] * (1 / dw**2 + 1 / (2 * i * dw**2))
diagonal_d.append(d)
if k == 0 or k == points[1] - 1:
pass
else:
e = 1 / (4 * dl**2) * (coefficient[i][k+1] - coefficient[i][k-1])\
+ coefficient[i][k] / dl**2
diagonal_e.append(e)
diagonals = [diagonal_a, diagonal_b, diagonal_c, diagonal_d, diagonal_e]
mat_a = diags(diagonals, [-points[0], -1, 0, 1, points[0]]).toarray()
return mat_a
mesh_points = (20, 50)
coefficients = np.random.rand(mesh_points[0], mesh_points[1])
dwidth = 100.
dlength = 100.
mymatrix = _compute_matrix_a(mesh_points, dwidth, dlength, coefficients)
I can't seem to find a way around my two loops (itertool product) to create the matrix.
I tried using np.roll:
# This gets the k-1
coefficients_km1 = np.roll(coefficients, 1, axis=0)
# This gets the i-1
coefficients_im1 = np.roll(coefficients, 1, axis=1)
# This gets the k+1
coefficients_kp1 = np.roll(coefficients, -1, axis=0)
# This gets the i+1
coefficients_ip1 = np.roll(coefficients, -1, axis=1)
Unfortunately, I couldn't convert the idea fully and got stuck in creating the array.
I'm open to any suggestion to optimize this function.
I need to calculate Turkish Tax number via Groovy.. Basically it's 10 digit number..
Here is the math behind it..
First 9 digit number (d), last 10 digit control ( c )value
Tr tax number = d1 d2 d3 d4 d5 d6 d7 d8 d9 c1
So d1..d9 >> d[i]
p[i] = (d[i] + 10 — i) mod 10
p[i] = 9 => q[i] = 9
p[i] != 9 => q[i] = (p[i] * 2^(10 — i)) mod 9
c1 = (10 — (Σ q[i] mod 10)) mod 10
Let’s say tax number : 018273645x
p[1] = ( d[1] + 10–1 ) mod 10 = (0 + 10–1) mod 10 = 9
p[2] = ( d[2] + 10–2) mod 10 = (1 + 10–2) mod 10 = 9
p[3] = ( d[3] + 10–3 ) mod 10 = (8 + 10–3) mod 10 = 5
p[4] = ( d[4] + 10–4 ) mod 10 = (2 + 10–4) mod 10 = 8
p[5] = ( d[5] + 10–5 ) mod 10 = (7 + 10–5) mod 10 = 2
p[6] = ( d[6] + 10–6 ) mod 10 = (3 + 10–6) mod 10 = 7
p[7] = ( d[7] + 10–7 ) mod 10 = (6 + 10–7) mod 10 = 9
p[8] = ( d[8] + 10–8 ) mod 10 = (4 + 10–8) mod 10 = 6
p[9] = ( d[9] + 10–9 ) mod 10 = (5 + 10–9) mod 10 = 6
q[1] = 9
q[2] = 9
q[3] = (p[3] * 2^(10–3)) mod 9 = (5 * 2⁷) mod 9 = 1
q[4] = (p[4] * 2^(10–4)) mod 9 = (8 * 2⁶) mod 9 = 8
q[5] = (p[5] * 2^(10–5)) mod 9 = (2 * 2⁵) mod 9 = 1
q[6] = (p[6] * 2^(10–6)) mod 9 = (7 * 2⁴) mod 9 = 4
q[7] = 9
q[8] = (p[8] * 2^(10–8)) mod 9 = (6 * 2²) mod 9 = 6
q[9] = (p[9] * 2^(10–9)) mod 9 = (6 * 2¹) mod 9 = 3
i = 1..9 >> Σ q[i] = 9 + 9 + 1 + 8 + 1 + 4 + 9 + 6 + 3 = 50
c1 = (10 — (Σ q[i] mod 10)) mod 10 = (10–50 mod 10) mod 10
c1 = (10–0) mod 10 = 0
So it would be >> 0182736450
Here is my code.
boolean isTurkishVID(String idnum) {
d=idnum;
int v1 = 0;
int v2 = 0;
int v3 = 0;
int v4 = 0;
int v5 = 0;
int v6 = 0;
int v7 = 0;
int v8 = 0;
int v9 = 0;
int v11 = 0;
int v22 = 0;
int v33 = 0;
int v44 = 0;
int v55 = 0;
int v66 = 0;
int v77 = 0;
int v88 = 0;
int v99 = 0;
int v_last_digit = 0;
int total = 0;
if (d.size()==10) {
v1 = (d[0].toInteger() + 9) % 10;
v2 = (d[1].toInteger() + 8) % 10;
v3 = (d[2].toInteger() + 7) % 10;
v4 = (d[3].toInteger() + 6) % 10;
v5 = (d[4].toInteger() + 5) % 10;
v6 = (d[5].toInteger() + 4) % 10;
v7 = (d[6].toInteger() + 3) % 10;
v8 = (d[7].toInteger() + 2) % 10;
v9 = (d[8].toInteger() + 1) % 10;
v11 = (v1 * 512) % 9;
v22 = (v2 * 256) % 9;
v33 = (v3 * 128) % 9;
v44 = (v4 * 64) % 9;
v55 = (v5 * 32) % 9;
v66 = (v6 * 16) % 9;
v77 = (v7 * 8) % 9;
v88 = (v8 * 4) % 9;
v99 = (v9 * 2) % 9;
if (v1 != 0 && v11 == 0) v11 = 9;
if (v2 != 0 && v22 == 0) v22 = 9;
if (v3 != 0 && v33 == 0) v33 = 9;
if (v4 != 0 && v44 == 0) v44 = 9;
if (v5 != 0 && v55 == 0) v55 = 9;
if (v6 != 0 && v66 == 0) v66 = 9;
if (v7 != 0 && v77 == 0) v77 = 9;
if (v8 != 0 && v88 == 0) v88 = 9;
if (v9 != 0 && v99 == 0) v99 = 9;
total = v11 + v22 + v33 + v44 + v55 + v66 + v77 + v88 + v99;
if (total % 10 == 0) total = 0;
else total = (10 - (total % 10));
if (total == v_last_digit) {
return true;
} else return false;
} else return false;
}
It returns error when I use it like
isTurkishVI("1234567891");
Returns;
groovy.lang.MissingMethodException: No signature of method: ConsoleScript20.isTurkishVI() is applicable for argument types: (java.lang.Integer) values: [1234567891]
Possible solutions: isTurkishVID(java.lang.String)
at ConsoleScript20.run(ConsoleScript20:74)
My present code takes too much time to execute say N=100000 values. Last time I tried it took around 4 hrs. Which is too much computing time. If someone can suggest anything to make the code a little faster?
def gen_chain(N):
coordinates = np.loadtxt('saw.txt', skiprows=0)
return coordinates
def lj(rij2):
sig_by_r6 = np.power(sigma / rij2, 3)
sig_by_r12 = np.power(sig_by_r6, 2)
lje = 4.0 * epsilon * (sig_by_r12 - sig_by_r6)
return lje
def fene(rij2):
return (-0.5 * K * R**2 * np.log(1 - ((np.sqrt(rij2) - r0)**2 / R**2)))
def total_energy(coord):
# Non-bonded
e_nb = 0
for i in range(N):
for j in range(i - 1):
ri = coord[i]
rj = coord[j]
rij = ri - rj
rij2 = np.dot(rij, rij)
if (np.sqrt(rij2) < rcutoff):
e_nb += lj(rij2)
# Bonded
e_bond = 0
for i in range(1, N):
ri = coord[i]
rj = coord[i - 1]
rij = ri - rj
rij2 = np.dot(rij, rij)
e_bond += fene(rij2)
return e_nb + e_bond
def move(coord):
trial = np.ndarray.copy(coord)
for i in range(N):
delta = (2.0 * np.random.rand(3) - 1) * max_delta
trial[i] += delta
return trial
def accept(delta_e):
beta = 1.0 / T
if delta_e <= 0.0:
return True
random_number = np.random.rand(1)
p_acc = np.exp(-beta * delta_e)
if random_number < p_acc:
return True
return False
if __name__ == "__main__":
# FENE parameters
K = 40
R = 0.3
r0 = 0.7
# LJ parameters
sigma = 0.624
epsilon = 1.0
# MC parameters
N = 100 # number of particles
rcutoff = 2.5 * sigma
max_delta = 0.01
n_steps = 100000
T = 0.5
coord = gen_chain(N)
energy_current = total_energy(coord)
traj = open('traj.xyz', 'w')
for step in range(n_steps):
if step % 1000 == 0:
traj.write(str(N) + '\n\n')
for i in range(N):
traj.write("C %10.5f %10.5f %10.5f\n" % (coord[i][0], coord[i][1], coord[i][2]))
print(step, energy_current)
coord_trial = move(coord)
energy_trial = total_energy(coord_trial)
delta_e = energy_trial - energy_current
if accept(delta_e):
coord = coord_trial
energy_current = energy_trial
traj.close()
I know it cannot be compared to C/C++.Therefore, please don't suggest to use any other language. I also welcome suggestions regarding some unnecessary objects.
Could you please help me speed up this code?
It takes a very long time to run because of how big the input file is, thank you to anyone who helps out.
racers = [int(file[0].split()[0]) / float(x) for x in file[0].split()[1::]]
print("hi")
def av(race):
race = race.split()
j = 0
while j != len([float(race[0]) / float(x) for x in race[1::]]):
racers[j] = [float(race[0]) / float(x) for x in race[1::]][j] + racers[j]
j += 1
for i in range(1, len(file)):
av(file[i])
a = min(racers)
del(racers[racers.index(min(racers))])
b = min(racers)
c = b-a
h = int(c)
c-=h
m = int(c * 60)
c-=m/60
s = round(c * 60 * 60)
print(str(h) + "h" + str(m) + "m" + str(s) + "s")
def taxcalc(gsalary2):
tax=0
if(gsalary2 <= 10164):
tax = 0
elif gsalary2 > 10164 and gsalary2 <= 19740 :
tax1 = 10 / 100 * 10164
tax2 = (15 / 100 * (gsalary2 - 10164))
tax = tax1 + tax2
elif gsalary2 > 19740 and gsalary2 <= 29316 :
tax1 = 10 / 100 * 10164
tax2 = 15 / 100 * 9576
tax3 = (20 / 100 * (gsalary2 - 19740))
tax = tax1 + tax2 + tax3
elif (gsalary2 > 29316 and gsalary2 <= 38892) :
tax1 = 10 / 100 * 10164
tax2 = 15 / 100 * 9576
tax3 = 20 / 100 * 9576
tax4 = 25 / 100 * (gsalary2 - 29316)
tax = tax1 + tax2 + tax3 + tax4
elif gsalary2 > 38892 :
tax1 = 10 / 100 * 10164
tax2 = 15 / 100 * 9576
tax3 = 20 / 100 * 9576
tax4 = 25 / 100 * 9576
tax5 = 30 / 100 * (gsalary2 - 38892)
tax = tax1 + tax2 + tax3 + tax4 + tax5
return tax
The above function fails totally
only this section seems to work
elif gsalary2 > 38892 :
tax1 = 10 / 100 * 10164
tax2 = 15 / 100 * 9576
tax3 = 20 / 100 * 9576
tax4 = 25 / 100 * 9576
tax5 = 30 / 100 * (gsalary2 - 38892)
tax = tax1 + tax2 + tax3 + tax4 + tax5
return tax
Make sure your return tax line is at the same indentation level as your if .... The way you have it now, it looks like it will only return a value if your last elif is true. You are getting None (instead of 0) because your function is not returning anything and therefore returns the default None instead. In other words, you need to make sure you always return tax.
def taxcalc(gsalary2):
tax = 0
if(gsalary2 <= 10164):
...
return tax