creating array using numpy from 1 to 100000 as value of x and y = x*x
x = np.arange(1,100000)
y = x*x
but when checked y value there are 31k+ negative value
count = 0
for i in y:
if i < 0:
count+=1
print(count)
31612
https://www.linkedin.com/posts/sandeep-agrawal-3b8857196_python-python3-pythonlearning-activity-6852679546345521152-cG-a
You are probably having integer overflow, try convert x to float before raising the power:
x = np.arange(1,100000)
y = x**10
sum(y < 0)
49760
Convert to float:
x = np.arange(1,100000).astype(float)
y = x**10
sum(y < 0)
0
Related
I want to change location by putting command but got trouble in for loop
All I want is put R R R U D D and get (3,4) location
here is my code
x,y = first start point
N= size of map
N=5
x,y = 1,1
I define left(-1,0), right(1,0), upper(0,-1), down(0,1)
def L(x,y):
if x>1 and x<N and y>1 and y<N:
x=x
y=y-1
return(x,y)
else:
return(x,y)
def R(x,y):
if x<N and y<N:
x=x
y=y+1
return(x,y)
else:
return(x,y)
def U(x,y):
if x>1 and x<N and y>1 and y<N:
x=x-1
y=y
return(x,y)
else:
return(x,y)
def D(x,y):
if x<N and y<N:
x=x+1
y=y
return(x,y)
else:
return(x,y)
input command
move_type=L(x,y),R(x,y),U(x,y),D(x,y)
num = [*map(int, input().split())]
put num [1 1]
change location - this is the point where I got trouble
for i in num:
x,y = move_type[i]
print(x,y)
**result come like this
1 2
1 2
I expect (1,2)
(1,3)
what's wrong with my code
help me plz**
Run Method like that
num = [*map(int, input().split())]
x, y = num
# move_type=L(x,y),R(x,y),U(x,y),D(x,y)
# above tuple has the return value of function because you call them with the x and y as 1.
move_type = L, R, U, D # But here I only provide the function name to
# tuple and later I execute them with the new x and y values
for i in num:
x, y = move_type[i](x, y) # calling the function with the new value of x and y
print(x, y)
One suggestion change your all functions
def L(x, y):
if x > 1 and x < N and y > 1 and y < N: # if this if execute then the x and y modify and return at the end
x = x
y = y-1
# but if if not execute the x and y values return to the same x and y values
return (x, y) # this will return the x, y
def R(x, y):
if x < N and y < N:
x = x
y = y+1
return (x, y)
def U(x, y):
if x > 1 and x < N and y > 1 and y < N:
x = x-1
y = y
return (x, y)
def D(x, y):
if x < N and y < N:
x = x+1
y = y
return (x, y)
It works with certain values (e.g "100 12 2"), but fails at "102 12 2" for some reason.
Checked both on Windows and MacOS with different python versions, results are the same and are not affected by setup.
from math import floor
s, x, y = 102, 12, 2
def calc(s, x, y):
q = 0
if x > y:
while s - (s / x) > 0:
q += s / x
q1 = s / x
s = s - (s / x) * x
s += q1 * y
return floor(q)
else:
return 'Inf'
if type(calc(s, x, y)) is int:
print(calc(s, x, y))
else:
print('Inf')
Try replacing the zero in the condition with a small number, such as 1e-16:
from math import floor
s, x, y = 102, 12, 2
def calc(s, x, y):
q = 0
if x > y:
while s - (s / x) > 1e-16:
q += s / x
q1 = s / x
s = s - (s / x) * x
s += q1 * y
return floor(q)
else:
return float('Inf')
print(calc(s, x, y)) # 10
The reason of doing so is that the sequence s - s/x does not become exactly zero; it only gets arbitrarily close to zero. (Even if exact zero is guaranteed algebraically, float is subject to inherent imprecision, so you need some threshold anyways.)
def max3bad(x,y,z):
maximum = 0
if x >= y:
if x >= z:
maximum = x
elif y >= z:
maximum = y
else:
maximum = z
return(maximum)
wrong output for what input?
get an input for which u get wrong output
for case: x = 2, y = 1, z = 3
the code output is 0 not 3.
It's better to get max number by :
max(x, y, z)
or you fix bug in your code:
def max3bad(x,y,z):
maximum = 0
if x >= y:
if x >= z:
maximum = x
else:
maximum = z
elif y >= z:
maximum = y
else:
maximum = z
return maximum
i wanted to do the project euler problems by using python.
but i am having problems with the following task:
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.
Find the largest palindrome made from the product of two 3-digit numbers.
my code for the given task:
def palindrome_number():
n=0
lower_range = 100
upper_range = 999
while x >= lower_range or x <= upper_range and y >= lower_range or y <= upper_range:
z = x * y
while z > n:
s = str(x * y)
if s == s[::-1]:
n = x * y
print(n)
now i dont know how to check for all the x and y numbers varying from 100-999.
i thought it has to be like in my code, but it doesnt work
Solution 1: using a generator expression
Actually the problem can be solved in one line :)
max(x*y for x in range(100, 1000) for y in range(100, 1000) if str(x*y)==str(x*y)[::-1])
Solution 2: using a for loop
for loops are better suited for this kind of operation than while loops. Below is the solution (I have only replaced your while with two for loops. The first loop tells the variable x to run from 100 to 999 and the second one tells y to do the same. With these two loops you will try out all combinations for x and y.)
def palindrome_number():
n = 0
lower_range = 100
upper_range = 999
for x in range(lower_range, upper_range+1):
for y in range(lower_range, upper_range+1):
z = x * y
if z > n: # an *if* suffices ;)
s = str(x * y)
if s == s[::-1]:
n = x * y
print(n)
Solution 3: using a while loop
To get the same thing with while loops you would have to take care of changing x and y to get all combinations:
x = y = lower_range
while x >= lower_range and x <= upper_range: # (*and* instead of *or*)
while y >= lower_range and y <= upper_range: # (again you want the >= *and* the <= to be fulfilled)
z = x * y
if z > n:
s = str(x * y)
if s == s[::-1]:
n = x * y
y += 1 # change y to get all combinations
y = lower_range
x += 1 # change x to get all combinations
This code is restarting the Python shell, and I cannot work out the errors of my code.
def middle(x,y,z):
if x > y and x < y:
return x
elif y > x and y < z:
return y
elif z > x and z < y:
return z
else:
return False
#Main Routine
middle(1,11,111)
Note that Python can chain comparisons for you (see the docs), and you are missing several cases:
def middle(x, y, z):
"""Return the middle of the three input values."""
if y < x < z or z < x < y: # or min(y, z) < x < max(y, z)
return x
elif x < y < z or z < y < z:
return y
elif x < z < y or y < z < x:
return z
return False
In use:
>>> middle(1, 11, 111)
11
If you want to see results when running the script directly, you will have to be explicit about this; as Martijn suggested in the comments, you could print middle(1, 11, 111). Otherwise the result will be evaluated, but not actually shown on-screen.
You can also simplify by sorting the inputs:
def middle(x, y, z):
"""Return the middle of the three input values."""
x, y, z = sorted((x, y, z))
return y if x < y < z else False
I am not sure what you want to do with this but your function looks weird to me
For example:
if x > y and x < y:
Will never be true
Also, if this function aims at returning the median of the three value it does not do this.
it would be more like
def middle (x, y, z):
t = [x, y, z]
t.sort()
return t[1]
Hope this helped