Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a variable which has an equation .I'm trying convert the equation that the varibale has into a string and compute the results
Eg
def func1(x):
y = x + 5
return y, 'x+5'
x as input can vary since I'm iterating
through multiple values
Say
h[0][1] = 5
func1(h[0][1])
Output - 10, "h[0][1]+5"
Required result
I need x+5 as string and the computed result of y as a while calling func1
Eval and exec seemed like a probable solution but they perform the inverse of what is needed
I'm not sure I understand why you'd want this but given that the variable holding the equation would be known when coding, you could just wrap the equation in a function. Eg:
def add_five(x):
return (x + 5, "x + 5")
x = 10
y = add_five(x)
print("Answer is", y[0])
print("Equation is", y[1])
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
This post was edited and submitted for review 8 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I am trying to compute the square root of 2 (up to nearly 10 million digits) with the following code
import os
os.chdir('/home/username/Desktop')
def sqroot(a, digits):
a = a * (10**(2*digits))
x_prev = 0
x_next = 1 * (10**digits)
while x_prev != x_next:
x_prev = x_next
x_next = (x_prev + (a // x_prev)) >> 1
return x_next
sqrt2 = sqroot(2,10000000)
file = open("Root_2",'w')
file.write(sqrt2)
file.close()
The problem is, I cannot write it to a file as an integer (TypeError: write() argument must be str, not int). I tried to convert it to a string but got an OverflowError as it is too big to convert to a string. Is there any way to work around this?
If you write to the file like the following:
n = 10**10000000
with open("Root_2",'w') as f:
print(n, file=f)
It will run without errors. Change n to the 10000000 digits number you get from your function.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 months ago.
Improve this question
I wrote this code to sum a number and its reverse:
#sum the multiple of the first number and its reversed
zipped_lists = zip(primo, reversedList)
res = [x + y for (x, y) in zipped_lists]
print (res)
#search common values in "res" and "secondo"
common_list = set(res).intersection(secondo)
Now that I isolated significant numbers, I need to return to the original number listed in primo. I've no idea how to do that. Please, help me :(
You need to keep track of x against its corresponding value of x + y. Instead of checking for the presence in secondo after you create res, you could do the filtering in a single comprehension.
res = [
x
for x, y in zip(primo, reversedList)
if (x + y) in secondo
]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
def thing():
_list = [1,1,5,6,8,3,4,6,10,23,5,12,67,3,25,2,6,5,4,3,2,1]
_list1 = [str(i) for i<=5 in lista]
return " ".join(_list1)
print(thing()))
I am new to this type of list managment, I am trying to put in _list1 only integers that are less then 5
so like the name of your function, it's created to calculate the sum of integers.
So basically, if your n=0 then your program will return 0 as result.
If not (else), it gonna give you a result of the calculation in return.
And here you want to print the interger sum of 451 which will give you the result of n % 10 + integer_sum(int(n / 10))
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Suppose I have a class called Cube and I create three objects:
class Cube():
def __init__(self, volume):
self.volume = volume
a = Cube(param_a)
b = Cube(param_b)
c = Cube(param_c)
I was wondering if I can write a function, to which the user can give a format, and the function can apply the format to the objects? For example:
>>> print_all(dummy_cube.volume)
a.volume
b.volume
c.volume
So basically the function contains a loop which will replace dummy_cube to a, b, c.
I know I can use something like getattr(), but it has limits. For example, I hope that the function can print whatever format the user gives:
>>> print_all( (dummy_cube.volume)**2 + 1 )
(a.volume)**2 + 1
(b.volume)**2 + 1
(c.volume)**2 + 1
Is there any good way to do this?
Edit: As the comments pointed out, yes the function can take a list, and I intend it to return the numerical values instead of the format itself.
With a list I can do like:
cube_list = [a, b, c]
for cube in cube_list:
print( (cube.volume)**2 + 1 )
But I am still not sure how I can do it with arbitrary expressions given by the user.
The user can supply a function.
def print_all(f):
for cube in cube_list:
print(f(cube))
import operator
print_all(operator.attrgetter('volume'))
print_all(lambda c: c.volume**2 + 1)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Please help I have no idea on how to write this function. I tried a ceaser cypher function and it didn't work. Any ideas?
Write a function cycle( S, n ) that takes in a string S of '0's and '1's and an integer n and returns the a string in which S has shifted its last character to the initial position n times. For example, cycle('1110110000', 2) would return '0011101100'.
The function you are looking for is:
def cycle(s, n):
return s[-n:] + s[:-n]
You could use Python's deque data type as follows:
import collections
def cycle(s, n):
d = collections.deque(s)
d.rotate(n)
return "".join(d)
print cycle('1110110000', 2)
This would display:
0011101100