I'm trying to run a for loop within a string, and I get that error.
Here's my code and the error I get:
code:
from cmath import inf
from tkinter import Y
from typing import Counter
def function(x, y):
x = 0
y = 0
s = "LRLRLRGLRGRLGRRRGRGLRG"
n = range(-100,100)
n_R = s.count("R", 0, 10)
n_L = s.count("L", 0, 10)
print(n_R)
print(n_L)
for c in s:
if n_R - n_L == 4 + 4*n:
return y += 1
elif n_R - n_L == 3 + 4*n:
return x -= 1
elif n_R - n_L == 2 + 4*n:
return y -= 1
else n_R - n_L == 1 + 4*n:
return x += 1
return x
return y
Error:
return y += 1
^
SyntaxError: invalid syntax
I have tried y = y + 1, and I get the same error.
Any ideas?
There are a number of errors in the code, but focusing on the error specifically referred to, this would resolve:
for c in s:
if n_R - n_L == 4 + 4*n:
y += 1
return y
The reason why this is the case is because y += 1 is actually a statement. Whereas, the OP needs to increment y before it is returned as a value.
Related
def factorial(n):
if n == 1:
return n
else:
return n*factorial(n-1)
def sin(x):
OddNumber = 3
counter = 1
NumberOfTerms = 80
while counter < NumberOfTerms:
x -= (x**OddNumber)/(factorial(OddNumber))
OddNumber += 2
counter += 1
x += (x**OddNumber)/(factorial(OddNumber))
OddNumber += 2
counter += 1
return x
theta = float(input("input angle in degrees: "))
theta = float((theta*math.pi/180))
print(sin(theta))
This is the code I'm using to try to find the approximation of sin of an angle x. I want to increase the variable NumberOfTerms to increase the precision but when I do i get this error
OverflowError: int too large to convert to float
How can I get around this? Is there a way to get around this?
When you use the reciprocal factorial value the code works fine with higher NumberOfTerms
import math
def reciproc_factorial(n):
if n == 1:
return n
else:
return 1/n*reciproc_factorial(n-1)
def sin(x):
OddNumber = 3
counter = 1
NumberOfTerms = 150
while counter < NumberOfTerms:
x -= (x**OddNumber) * (reciproc_factorial(OddNumber))
OddNumber += 2
counter += 1
x += (x**OddNumber) * (reciproc_factorial(OddNumber))
OddNumber += 2
counter += 1
return x
theta = 23
theta = float((theta*math.pi/180))
print(sin(theta))
For context, I am trying to find all viable solutions to this question:
here's the code I have so far, but I am having trouble with the part that is supposed to iterate through every possible combination.
x = 1
y = 1
z = 10
a = 10
while x < 10 and y < 10 and z < 100 and a < 100: #iterates through every possible combination
x = x + 1
y = y + 1
z = z + 1
a = a + 1
if x != y: #checks if x and are the same
if a/x == z/y or z/x == a/y: #checks if x and y are proportional to a and z
a = str(a) #converting each int to string
z = str(z)
x = str(x)
y = str(y)
if a.count(x) < 1 and a.count(y) < 1 and z.count(y) <1 and z.count(x) < 1: #checks if any number reapeats
print(x, y, z, a) #prints viable solution```
You have six boxes to fill. Just run through all permutations of 6 members and check the conditions:
import itertools
for a,b,c,d,e,f in itertools.permutations([0,1,2,3,4,5,6,7,8,9],6):
if a == 0 or b == 0 or c == 0 or e == 0:
continue
if (10*c + d) / a == (10*e + f) / b:
print( a, b, c*10+d, e*10+f )
It looks like there are 57 solutions.
I'm a bit stuck on a python problem.
I'm suppose to write a function that takes a positive integer n and returns the number of different operations that can sum to n (2<n<201) with decreasing and unique elements.
To give an example:
If n = 3 then f(n) = 1 (Because the only possible solution is 2+1).
If n = 5 then f(n) = 2 (because the possible solutions are 4+1 & 3+2).
If n = 10 then f(n) = 9 (Because the possible solutions are (9+1) & (8+2) & (7+3) & (7+2+1) & (6+4) & (6+3+1) & (5+4+1) & (5+3+2) & (4+3+2+1)).
For the code I started like that:
def solution(n):
nb = list(range(1,n))
l = 2
summ = 0
itt = 0
for index in range(len(nb)):
x = nb[-(index+1)]
if x > 3:
for index2 in range(x-1):
y = nb[index2]
#print(str(x) + ' + ' + str(y))
if (x + y) == n:
itt = itt + 1
for index3 in range(y-1):
z = nb[index3]
if (x + y + z) == n:
itt = itt + 1
for index4 in range(z-1):
w = nb[index4]
if (x + y + z + w) == n:
itt = itt + 1
return itt
It works when n is small but when you start to be around n=100, it's super slow and I will need to add more for loop which will worsen the situation...
Do you have an idea on how I could solve this issue? Is there an obvious solution I missed?
This problem is called integer partition into distinct parts. OEIS sequence (values are off by 1 because you don't need n=>n case )
I already have code for partition into k distinct parts, so modified it a bit to calculate number of partitions into any number of parts:
import functools
#functools.lru_cache(20000)
def diffparts(n, k, last):
result = 0
if n == 0 and k == 0:
result = 1
if n == 0 or k == 0:
return result
for i in range(last + 1, n // k + 1):
result += diffparts(n - i, k - 1, i)
return result
def dparts(n):
res = 0
k = 2
while k * (k + 1) <= 2 * n:
res += diffparts(n, k, 0)
k += 1
return res
print(dparts(201))
I was trying to make a progam that reads "raiz"(means square root) and writes ""(1/2)**"
Just wanted to have 24 as result, but occurs an error saying that i can't convert "(1/2)**576" into a float/int.
def main(args):
a = input("Qual expressão quer simplificar? \n")
i = 0
x = ""
while i < len(a):
c = a[i]
r = a[i: i + 5]
b = a[i: i + 4]
g = a[i: i + 8]
h = a[i: i + 7]
if g == "raiz de ":
c = "(1/2)**"
i += 7
elif h == "raiz de":
c = "(1/2)**"
i += 6
elif b == "raiz":
c = "(1/2)**"
i += 3
if r == "vezes":
c= "*"
i += 4
i += 1
x += c
z = float(x)
print(z)
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
enter code here
If the question is why are you receiving this error, it is because of the line z = float(x). You are passing in x, which is a string with non-decimal characters. In one case you are trying to convert "(1/2)**" into a float.
float() takes a number or a string, but the string must be digits.
float('(1/2)**')
# ValueError: could not convert string to float: (1/2)**
float('2.5')
# 2.5
float(4/2)
# 2.0
So I have this function and I have done much of the work on it so far.
I am trying to feed the results from the addseries function into the power function to give me for example:
6^6 + 5^5 + 4^4 + 3^3 + 2^2 + 1+ 0 ..
I am having a problem with getting it to work. Any suggestions?
def power(n):
if(n<=0):
return 0
else:
return n**n
def addseries(num):
if(num == 0):
return 0
else:
print(num)
return sumseries(num - 1) + power(num)
Your code would actually work if you simply fix the indentation and typos (sumseries should be addseries, for example):
def power(n):
if(n<=0):
return 0
else:
return n**n
def addseries(num):
if(num == 0):
return 0
else:
return addseries(num - 1) + power(num)
so that:
print(addseries(6))
would output:
50069
which you can verify in Python shell:
>>> 1**1 + 2**2 + 3**3 + 4**4 + 5**5 + 6**6
50069
>>>
A bit simplified:
def addseries(num):
if(num == 0):
return 0
else:
print(num)
return num**num + addseries(num - 1)
Ignoring recursion:
n = 6
>>> sum(x ** x for x in range(n, 0, -1))
50069
# 6 ** 6 + 5 ** 5 + 4 ** 4 + 3 ** 3 + 2 ** 2 + 1 ** 1
# = 46656 + 3125 + 356 + 27 + 4 + 1
# = 50069
Using recursion:
def power_function(n):
if n < 1:
return 0
return n ** n + power_function(n - 1)
>>> power_function(6)
50069