Using setattr() to convert strings to variables - python

I am creating a sigsum() function which takes the sum using an input equation and an input variable. Here's what I have so far:
def sigsum(eqn, index, lower=0, upper=None, step=1):
if type(step) is not int:
raise TypeError('step must be an integer')
elif step < 1:
raise ValueError('step must be greater than or equal to 1')
if upper is None:
upper = 1280000
if lower is None:
lower = -1280000
if (upper - lower) % step:
upper -= (upper - lower) % step
index = lower
total = 0
while True:
total += eqn
if index == upper:
break
index += step
return total
Usage of function:
print(sigsum('1/(i+5)','i'))
>>> 12.5563
My current problem is converting 'eqn' and 'index' to variables that exist inside the function local namespace. I heard around that using exec is not a good idea and that maybe setattr() might work. Can anyone help me out?
Thanks.

For eqn I suggest using a lambda function:
eqn = lambda i: 1 / (i + 5)
then index is not needed, because it is just "the variable passed to the function" (does not need a name).
Then your function becomes
def integrate(fn, start = 0, end = 128000, step = 1):
"""
Return a stepwise approximation of
the integral of fn from start to end
"""
num_steps = (end - start) // step
if num_steps < 0:
raise ValueError("bad step value")
else:
return sum(fn(start + k*step) for k in range(num_steps))
and you can run it like
res = step_integrate(eqn) # => 10.253703030104417
Note that there are many steps to this, and many of them involve very small numbers; rounding errors can become a major problem. If accuracy is important you may want to manually derive an integral,
from math import log
eqn = lambda i: 1 / (i + 5)
eqn.integral = lambda i: log(i + 5)
def integrate(fn, start = 0, end = 128000, step = 1):
"""
Return the integral of fn from start to end
If fn.integral is defined, used it;
otherwise do a stepwise approximation
"""
if hasattr(fn, "integral"):
return fn.integral(end) - fn.integral(start)
else:
num_steps = (end - start) // step
if num_steps < 0:
raise ValueError("bad step value")
else:
return sum(fn(start + k*step) for k in range(num_steps))
which again runs like
res = step_integrate(eqn) # => 10.150386692204735
(note that the stepwise approximation was about 1% too high.)

I would use a lambda function as Hugh Bothwell suggested you
would have to modify sigsum as the following
def sigsum(eqn, lower=0, upper=None, step=1):
if type(step) is not int:
raise TypeError('step must be an integer')
elif step < 1:
raise ValueError('step must be greater than or equal to 1')
if upper is None:
upper = 1280000
if lower is None:
lower = -1280000
if (upper - lower) % step:
upper -= (upper - lower) % step
index = lower
total = 0
while True:
total += eqn(index)
if index == upper:
break
index += step
return total
Usage of function:
print(sigsum(lambda i: 1/(i+5)))
>>> 12.5563
you can also define a function separatly:
def myfunction(i):
return 1/(i+5)
and pass it to sigsum
print(sigsum(myfunction))
>>> 12.5563
to be able to pass function as a parameter is called in computer language speech function as first class object. (C and java for example doesn't have it, javascript and python have)

Related

Unbounded Binary Search (Finding the point where a monotonically increasing function becomes zero)

I am trying to write code that finds the point where a monotonically increasing function becomes zero.
I am using Unbounded Binary Search for this.
I tried the following code but it just gives me the point where the function becomes positive first time. It is also not all that accurate. I want to have an accuracy of 12 decimal places.
# Python3 program for Unbound Binary search.
# Let's take an example function as
# f(x) = x^2 - 10*x - 20
# Note that f(x) can be any monotonocally
# increasing function
def f(x):
return (x * x - 10 * x - 20)
# Returns the value x where above function
# f() becomes positive first time.
def findFirstPositive():
# When first value itself is positive
if (f(0) > 0):
return 0
# Find 'high' for binary search
# by repeated doubling
i = 1
while (f(i) <= 0):
i = i * 2
# Call binary search
return binarySearch(i / 2, i)
# Searches first positive value of
# f(i) where low <= i <= high
def binarySearch(low, high):
if (high >= low):
# mid = (low + high)/2
mid = low + (high - low) / 2;
# If f(mid) is greater than 0
# and one of the following two
# conditions is true:
# a) mid is equal to low
# b) f(mid-1) is negative
if (f(mid) > 0 and (mid == low or f(mid - 1) <= 0)):
return mid
# If f(mid) is smaller than or equal to 0
if (f(mid) <= 0):
return binarySearch((mid + 1), high)
else: # f(mid) > 0
return binarySearch(low, (mid - 1))
# Return -1 if there is no positive
# value in given range
return -1
# Driver Code
print("The value n where f() becomes " +
"positive first is ", findFirstPositive())
Output:
The value n where f() becomes positive first is 12.0
I am new to Python and any help will be appreciated.
Thank you.
How about something like this?
def f(x):
return (x * x - 10 * x - 20)
def findFirstPositive():
if (f(0) > 0):
return 0
i = 1
while (f(i) <= 0):
i = i * 2
return binarySearch(i / 2, i)
def binarySearch(low, high):
if (high >= low):
mid = (low + high)/2
if abs(f(mid)) < 1e-10:
return mid
if f(mid) <= 0:
return binarySearch(mid, high)
else:
return binarySearch(low, mid)
return -1
print("The value n where f() becomes positive first is ", findFirstPositive())
Also, note that your function - x*x-10*x-20 - isn't actually monotonically increasing everywhere; however, your code only starts searching from x=0 so it still works.

A Normal Distribution Calculator

so im trying to make a program to solve various normal distribution questions with pure python (no modules other than math) to 4 decimal places only for A Levels, and there is this problem that occurs in the function get_z_less_than_a_equal(0.75):. Apparently, without the assert statement in the except clause, the variables all get messed up, and change. The error, i'm catching is the recursion error. Anyways, if there is an easier and more efficient way to do things, it'd be appreciated.
import math
mean = 0
standard_dev = 1
percentage_points = {0.5000: 0.0000, 0.4000: 0.2533, 0.3000: 0.5244, 0.2000: 0.8416, 0.1000: 1.2816, 0.0500: 1.6440, 0.0250: 1.9600, 0.0100: 2.3263, 0.0050: 2.5758, 0.0010: 3.0902, 0.0005: 3.2905}
def get_z_less_than(x):
"""
P(Z < x)
"""
return round(0.5 * (1 + math.erf((x - mean)/math.sqrt(2 * standard_dev**2))), 4)
def get_z_greater_than(x):
"""
P(Z > x)
"""
return round(1 - get_z_less_than(x), 4)
def get_z_in_range(lower_bound, upper_bound):
"""
P(lower_bound < Z < upper_bound)
"""
return round(get_z_less_than(upper_bound) - get_z_less_than(lower_bound), 4)
def get_z_less_than_a_equal(x):
"""
P(Z < a) = x
acquires a, given x
"""
# first trial: brute forcing
for i in range(401):
a = i/100
p = get_z_less_than(a)
if x == p:
return a
elif p > x:
break
# second trial: using symmetry
try:
res = -get_z_less_than_a_equal(1 - x)
except:
# third trial: using estimation
assert a, "error"
prev = get_z_less_than(a-0.01)
p = get_z_less_than(a)
if abs(x - prev) > abs(x - p):
res = a
else:
res = a - 0.01
return res
def get_z_greater_than_a_equal(x):
"""
P(Z > a) = x
"""
if x in percentage_points:
return percentage_points[x]
else:
return get_z_less_than_a_equal(1-x)
print(get_z_in_range(-1.20, 1.40))
print(get_z_less_than_a_equal(0.7517))
print(get_z_greater_than_a_equal(0.1000))
print(get_z_greater_than_a_equal(0.0322))
print(get_z_less_than_a_equal(0.1075))
print(get_z_less_than_a_equal(0.75))
Since python3.8, the statistics module in the standard library has a NormalDist class, so we could use that to implement our functions "with pure python" or at least for testing:
import math
from statistics import NormalDist
normal_dist = NormalDist(mu=0, sigma=1)
for i in range(-2000, 2000):
test_val = i / 1000
assert get_z_less_than(test_val) == round(normal_dist.cdf(test_val), 4)
Doesn't throw an error, so that part probably works fine
Your get_z_less_than_a_equal seems to be the equivalent of NormalDist.inv_cdf
There are very efficient ways to compute it accurately using the inverse of the error function (see Wikipedia and Python implementation), but we don't have that in the standard library
Since you only care about the first few digits and get_z_less_than is monotonic, we can use a simple bisection method to find our solution
Newton's method would be much faster, and not too hard to implement since we know that the derivative of the cdf is just the pdf, but still probably more complex than what we need
def get_z_less_than_a_equal(x):
"""
P(Z < a) = x
acquires a, given x
"""
if x <= 0.0 or x >= 1.0:
raise ValueError("x must be >0.0 and <1.0")
min_res, max_res = -10, 10
while max_res - min_res > 1e-7:
mid = (max_res + min_res) / 2
if get_z_less_than(mid) < x:
min_res = mid
else:
max_res = mid
return round((max_res + min_res) / 2, 4)
Let's test this:
for i in range(1, 2000):
test_val = i / 2000
left_val = get_z_less_than_a_equal(test_val)
right_val = round(normal_dist.inv_cdf(test_val), 4)
assert left_val == right_val, f"{left_val} != {right_val}"
# AssertionError: -3.3201 != -3.2905
We see that we are losing some precision, that's because the error introduced by get_z_less_than (which rounds to 4 digits) gets propagated and amplified when we use it to estimate its inverse (see Wikipedia - error propagation for details)
So let's add a "digits" parameter to get_z_less_than and change our functions slightly:
def get_z_less_than(x, digits=4):
"""
P(Z < x)
"""
res = 0.5 * (1 + math.erf((x - mean) / math.sqrt(2 * standard_dev ** 2)))
return round(res, digits)
def get_z_less_than_a_equal(x, digits=4):
"""
P(Z < a) = x
acquires a, given x
"""
if x <= 0.0 or x >= 1.0:
raise ValueError("x must be >0.0 and <1.0")
min_res, max_res = -10, 10
while max_res - min_res > 10 ** -(digits * 2):
mid = (max_res + min_res) / 2
if get_z_less_than(mid, digits * 2) < x:
min_res = mid
else:
max_res = mid
return round((max_res + min_res) / 2, digits)
And now we can try the same test again and see it passes

Smallest number whose digits are decreasing

I found a problem to implement functions that takes a positive integer n as an input and returns the smallest positive integer larger than n whose digits are decreasing and similarly for a function that returns the smallest positive integer larger than n whose digits are increasing. I think the increasing function works correctly. But what is the mistake in the function decrease? For the input decreasing(100) it returns 11 rather than 110.
# the next integer whose digits are increasing.
def increasing(n):
asastring = str(n)
length = len(asastring)
if asastring == "9"*length:
return "1"*(length+1)
if length == 1:
return int(n)+1
if length >= 2:
firstcharacter = asastring[0]
secondcharacter = asastring[1]
if int(firstcharacter) > int(secondcharacter):
return int(str(firstcharacter)*length)
if firstcharacter == secondcharacter:
return firstcharacter+str(increasing(int(asastring[1:])))
if int(firstcharacter) < int(secondcharacter):
if secondcharacter == "9":
return str(int(firstcharacter)+1) * len(str(n))
return firstcharacter+str(increasing(int(asastring[1:])))
# the next integer whose digits are decreasing.
def decreasing(n):
asastring = str(n)
length = len(asastring)
# First the case where we need to add a digit.
if asastring == "9"*length:
return "1"+"0"*length
# Now we know that the next integer has the same number of digits as the original number.
if length == 1:
return int(n)+1
if length >= 2:
firstcharacter = asastring[0]
secondcharacter = asastring[1]
if int(firstcharacter) > int(secondcharacter):
endpart = str(((asastring[1:3])))
value = firstcharacter + str(decreasing(int(asastring[1:])))
return str(firstcharacter) + str(decreasing(int(asastring[1:])))
if int(firstcharacter) == int(secondcharacter):
return decreasing(firstcharacter+str(decreasing(int(asastring[1:]))))
if int(firstcharacter) < int(secondcharacter):
return str(int(firstcharacter)+1)+'0'*(length-1)
i=100
print(increasing(i))
print(decreasing(i))
You need to remove the int type casting done in the recursive calls as int('00') is converting your number to zero(basically removing all starting zeros) and shortening the length of your string. Just remove that casting.. remaining code is working fine:
def decreasing(n):
asastring = str(n)
length = len(asastring)
# First the case where we need to add a digit.
if asastring == "9"*length:
return "1"+"0"*length
# Now we know that the next integer has the same number of digits as the original number.
if length == 1:
return int(n)+1
if length >= 2:
firstcharacter = asastring[0]
secondcharacter = asastring[1]
if int(firstcharacter) > int(secondcharacter):
return str(firstcharacter) + str(decreasing(asastring[1:]))
if int(firstcharacter) == int(secondcharacter):
return decreasing(firstcharacter+str(decreasing(asastring[1:])))
if int(firstcharacter) < int(secondcharacter):
return str(int(firstcharacter)+1)+'0'*(length-1)
There are multiple intertwined issues that need to be addressed. The naming of these two functions is a source of confusion. If we follow the logic, then the function increasing() should be called nondecreasing() and similarly, the function decreasing() should be called nonincreasing(). It's the difference between > (greater than) and >= (greater than or equal).
The next confusion is what type do these functions accept and return? If we examine what the working increasing() function returns, we get:
str return "1"*(length+1)
int return int(n)+1
int return int(str(firstcharacter)*length)
str return firstcharacter+str(increasing(int(asastring[1:])))
str return str(int(firstcharacter)+1) * len(str(n))
str return firstcharacter+str(increasing(int(asastring[1:])))
If we similarly look at how the increasing() deals with its own internal recursive calls to see what it thinks it accepts and returns, we get:
int -> int return firstcharacter+str(increasing(int(asastring[1:])))
int -> int return firstcharacter+str(increasing(int(asastring[1:])))
So here's an attempted rework of increasing(), aka nondecreasing(), that tries to make it consistently accept an int and return an int:
def nondecreasing(n): # aka increasing()
as_string = str(n)
length = len(as_string)
if as_string == "9" * length:
return int("1" * (length + 1))
if length == 1:
return int(n) + 1
first_digit, second_digit, second_digit_onward = as_string[0], as_string[1], as_string[1:]
if first_digit > second_digit:
return int(first_digit * length)
if first_digit == second_digit:
return int(first_digit + str(nondecreasing(int(second_digit_onward))))
if as_string == first_digit + "9" * (length - 1):
return int(str(int(first_digit) + 1) * length)
return int(first_digit + str(nondecreasing(int(second_digit_onward))))
The decreasing(), aka nonincreasing(), function is more problematic. It relies on its ability to accept an int, or a str upon internal calls, to solve the problem.
Discussing these sort of issues, and not making other programmers rediscover them, are what code comments are all about.
I don't believe the above issue precludes nonincreasing() from consistently returning an int:
def nonincreasing(n): # aka decreasing()
as_string = str(n)
length = len(as_string)
if as_string == "9" * length:
return int("1" + "0" * length)
if length == 1:
return int(n) + 1
first_digit, second_digit, second_digit_onward = as_string[0], as_string[1], as_string[1:]
if first_digit > second_digit:
return int(first_digit + str(nonincreasing(second_digit_onward)))
if first_digit == second_digit:
remaining_digits = str(nonincreasing(second_digit_onward))
second_digit = remaining_digits[0]
n = first_digit + remaining_digits
if first_digit < second_digit:
return int(str(int(first_digit) + 1) + '0' * (length - 1))
return int(n)
The key to fixing this function was to remove the return statement from the penultimate if clause and instead fix up the data and let it fall through to the next if clause to see if the results need repair or not.
I believe #devender22's insight about int() casting is a crucial one but I don't believe the accompanying solution is valid as it generates large blocks of incorrect results (e.g. 990 through 998 all go to 1000 when they should simply be bumped up by 1).
In order to check my nonincreasing() function with all of its cases, I wrote a less efficient, non-recursive solution, without separate cases, using completely different Python operators:
def nonincreasing(n):
def is_increasing(n):
string = str(n)
return any(map(lambda x, y: y > x, string, string[1:]))
while is_increasing(n + 1):
n += 1
return n + 1
And then made sure the two implementations agreed on their output.

Higher order python functions and setting their attributes correctly

I'm currently writing a function that makes and returns a new function to create polynomial expressions. I want the function to store the coefficients of the polynomial and the polynomial itself in string form. However, it doesn't seem that I can set either of the attributes without the interpreter insisting the newly created function has no such attribute.
Please see my function below
def poly(coefs):
"""Return a function that represents the polynomial with these coefficients.
For example, if coefs=(10, 20, 30), return the function of x that computes
'30 * x**2 + 20 * x + 10'. Also store the coefs on the .coefs attribute of
the function, and the str of the formula on the .__name__ attribute.'"""
# your code here (I won't repeat "your code here"; there's one for each function)
def createPoly(x):
formulaParts = []
power = 0
createPoly.coefs = coefs
for coef in coefs:
if power == 0:
formulaParts += [('%d') % (coef)]
elif power == 1:
formulaParts += [('%d * x') % (coef)]
else:
formulaParts += [('%d * x**%d') % (coef, power)]
power +=1
createPoly.__name__ = ' + '.join(formulaParts[::-1])
createPoly.value = eval(createPoly.__name__)
return createPoly.value
return createPoly
As you can see when I set the attributes in the above code and use them there is no problem. However if I use code like the below that's when the error occurs
y = poly((5,10,5))
print(y.__name__)
It might be something REALLY simple I'm overlooking. Please help
Your code to set up the inner function can't be inside the inner function:
def poly(coefs):
def createPoly(x):
createPoly.value = eval(createPoly.__name__)
return createPoly.value
formulaParts = []
power = 0
for coef in coefs:
if power == 0:
formulaParts += [('%d') % (coef)]
elif power == 1:
formulaParts += [('%d * x') % (coef)]
else:
formulaParts += [('%d * x**%d') % (coef, power)]
power += 1
createPoly.__name__ = ' + '.join(formulaParts[::-1])
createPoly.coefs = coefs
return createPoly

Optimizing python code

Any tips on optimizing this python code for finding next palindrome:
Input number can be of 1000000 digits
COMMENTS ADDED
#! /usr/bin/python
def inc(lst,lng):#this function first extract the left half of the string then
#convert it to int then increment it then reconvert it to string
#then reverse it and finally append it to the left half.
#lst is input number and lng is its length
if(lng%2==0):
olst=lst[:lng/2]
l=int(lng/2)
olst=int(olst)
olst+=1
olst=str(olst)
p=len(olst)
if l<p:
olst2=olst[p-2::-1]
else:
olst2=olst[::-1]
lst=olst+olst2
return lst
else:
olst=lst[:lng/2+1]
l=int(lng/2+1)
olst=int(olst)
olst+=1
olst=str(olst)
p=len(olst)
if l<p:
olst2=olst[p-3::-1]
else:
olst2=olst[p-2::-1]
lst=olst+olst2
return lst
t=raw_input()
t=int(t)
while True:
if t>0:
t-=1
else:
break
num=raw_input()#this is input number
lng=len(num)
lst=num[:]
if(lng%2==0):#this if find next palindrome to num variable
#without incrementing the middle digit and store it in lst.
olst=lst[:lng/2]
olst2=olst[::-1]
lst=olst+olst2
else:
olst=lst[:lng/2+1]
olst2=olst[len(olst)-2::-1]
lst=olst+olst2
if int(num)>=int(lst):#chk if lst satisfies criteria for next palindrome
num=inc(num,lng)#otherwise call inc function
print num
else:
print lst
I think most of the time in this code is spent converting strings to integers and back. The rest is slicing strings and bouncing around in the Python interpreter. What can be done about these three things? There are a few unnecessary conversions in the code, which we can remove. I see no way to avoid the string slicing. To minimize your time in the interpreter you just have to write as little code as possible :-) and it also helps to put all your code inside functions.
The code at the bottom of your program, which takes a quick guess to try and avoid calling inc(), has a bug or two. Here's how I might write that part:
def nextPal(num):
lng = len(num)
guess = num[:lng//2] + num[(lng-1)//2::-1] # works whether lng is even or odd
if guess > num: # don't bother converting to int
return guess
else:
return inc(numstr, n)
This simple change makes your code about 100x faster for numbers where inc doesn't need to be called, and about 3x faster for numbers where it does need to be called.
To do better than that, I think you need to avoid converting to int entirely. That means incrementing the left half of the number without using ordinary Python integer addition. You can use an array and carry out the addition algorithm "by hand":
import array
def nextPal(numstr):
# If we don't need to increment, just reflect the left half and return.
n = len(numstr)
h = n//2
guess = numstr[:n-h] + numstr[h-1::-1]
if guess > numstr:
return guess
# Increment the left half of the number without converting to int.
a = array.array('b', numstr)
zero = ord('0')
ten = ord('9') + 1
for i in range(n - h - 1, -1, -1):
d = a[i] + 1
if d == ten:
a[i] = zero
else:
a[i] = d
break
else:
# The left half was all nines. Carry the 1.
# Update n and h since the length changed.
a.insert(0, ord('1'))
n += 1
h = n//2
# Reflect the left half onto the right half.
a[n-h:] = a[h-1::-1]
return a.tostring()
This is another 9x faster or so for numbers that require incrementing.
You can make this a touch faster by using a while loop instead of for i in range(n - h - 1, -1, -1), and about twice as fast again by having the loop update both halves of the array rather than just updating the left-hand half and then reflecting it at the end.
You don't have to find the palindrome, you can just generate it.
Split the input number, and reflect it. If the generated number is too small, then increment the left hand side and reflect it again:
def nextPal(n):
ns = str(n)
oddoffset = 0
if len(ns) % 2 != 0:
oddoffset = 1
leftlen = len(ns) / 2 + oddoffset
lefts = ns[0:leftlen]
right = lefts[::-1][oddoffset:]
p = int(lefts + right)
if p < n:
## Need to increment middle digit
left = int(lefts)
left += 1
lefts = str(left)
right = lefts[::-1][oddoffset:]
p = int(lefts + right)
return p
def test(n):
print n
p = nextPal(n)
assert p >= n
print p
test(1234567890)
test(123456789)
test(999999)
test(999998)
test(888889)
test(8999999)
EDIT
NVM, just look at this page: http://thetaoishere.blogspot.com/2009/04/finding-next-palindrome-given-number.html
Using strings. n >= 0
from math import floor, ceil, log10
def next_pal(n):
# returns next palindrome, param is an int
n10 = str(n)
m = len(n10) / 2.0
s, e = int(floor(m - 0.5)), int(ceil(m + 0.5))
start, middle, end = n10[:s], n10[s:e], n10[e:]
assert (start, middle[0]) == (end[-1::-1], middle[-1]) #check that n is actually a palindrome
r = int(start + middle[0]) + 1 #where the actual increment occurs (i.e. add 1)
r10 = str(r)
i = 3 - len(middle)
if len(r10) > len(start) + 1:
i += 1
return int(r10 + r10[-i::-1])
Using log, more optized. n > 9
def next_pal2(n):
k = log10(n + 1)
l = ceil(k)
s, e = int(floor(l/2.0 - 0.5)), int(ceil(l/2.0 + 0.5))
mmod, emod = 10**(e - s), int(10**(l - e))
start, end = divmod(n, emod)
start, middle = divmod(start, mmod)
r1 = 10*start + middle%10 + 1
i = middle > 9 and 1 or 2
j = s - i + 2
if k == l:
i += 1
r2 = int(str(r1)[-i::-1])
return r1*10**j + r2

Categories