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.
Related
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.
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))
Can an if-else expression be the argument of 'return'?
Here's an example of what I'm trying to do:
return m +
if a:
x
elif b:
y
else c:
z
I could write as:
addend = m
if a:
m += x
elif b:
m += y
else c:
m += z
return m
Well, you can use Python's ternary method, such as:
return m + (x if a else y if b else z)
But it may be more readable to just do something like:
if a: return m + x
if b: return m + y
return m + z
As an aside, else c: is not really sensible code: you use if/elif if you have a condition, or else for default action (no condition).
For example, in terms of the code you posted in a comment, you could opt for the succinct, yet still self-documenting:
def rental_car_costs(days):
basecost = days * 40
discount = 50 if days >= 7 else 20 if days >= 3 else 0
return basecost - discount
def gcd(e, z):
if z == 0:
return e
else:
return gcd(z, e % z)
e = int(input("Please enter the first number:"))
z = int(input("Please enter the second number:"))
print ("The GCD of ",e," and ",z," is ",gcd(e,z))
d = 1
while d < e:
if d * e == 1 % z:
print (d," * ",e," = 1 (mod ",z,")")
d = d + 1
else:
d = d + 1
I am trying to use this code to find candidates for rsa by brute force it seems like it should work but it doesn't can anyone help me out?
z = (p − 1)(q − 1)
for the calculation of z is used prior to this
with p = 47 and q = 59, e = 17 and d = 157 but after the program runs it finds no matches but it should.
Where you have
if d * e == 1 % z:
you seem to want to check "Is d*e equal (mod z) to 1"
But what you're doing is performing 1 % z (which gives 1) and checking if d * e is equal to 1.
Change it to:
if (d*e) % z == 1:
and it should do the calculation you intend.
some problems from your code:
1) if z = (p-1)(q-1), d*e = 1 (mod z), then z and e are coprime, gcd(e, z) will always equal 1, see
2) if you say d*e = 1 (mod z), the code should be if d * e % z == 1
I am trying to run a test if the number ends in .0
I am running a program with numbers orders of magnitude apart so I can't estimate to a certain amount of digits. using % doesn't work either because then certain numbers are excluded. All the numbers in this program are floating point numbers so I need a way to check if it ends with .0, not with .00000000000001232 or something it has to end exactly in .0
The problem with the round function is that I am dealing with numbers of several orders of magnitude. I need something that checks if it has only 1 decimal after the . or something that checks if the that decimal is a 0.
code:
from myro import *
from math import *
def main():
z = 3
a = 2
b = 2
x = 3
y = 2 #starts at y = 3
lim = 25
c = (a**x + b**y)**(1.0/z)
resultnum = 0
while z <= lim:
while a <= lim:
while b <= lim:
while x <= lim:
while y <= lim:
y = y + 1
c = (a**x + b**y)**(1.0/z)
if float(int(c) + 1) != round(c, 6):
pass
else:
print str(a) + "^" + str(x) + " + " + str(b) + "^" + str(y) + " = " + str(int(c)+1) + "^" + str(z)
resultnum = resultnum + 1
print c
y = 3
x = x + 1
x = 3
b = b + 1
b = 3
a = a + 1
a = 3
z = z + 1
print z
print "code cycle complete"
print str(resultnum) + " results"
main()
>>> n1 = 2.0
>>> int(n1) == n1 and isinstance(n1, float)
True
>>> n1 = 2
>>> int(n1) == n1 and isinstance(n1, float)
False
>>> n1 = 2.01
>>> int(n1) == n1 and isinstance(n1, float)
False
>>> n1 = 1E1 #works for scientific notation as well
>>> int(n1) == n1 and isinstance(n1, float)
True
Python does this already. Going with what Python gives as a string might be what you want:
In [577]: def dot_zero(number):
.....: return str(number).endswith('.0')
.....:
In [578]: dot_zero(2.0)
Out[578]: True
In [579]: dot_zero(2)
Out[579]: False
In [580]: dot_zero(2.01)
Out[580]: False
EDIT
As pointed out by #jamylak this does not work for large numbers since the scientific notation used by str. Keeping the basic idea of conversion into a string, but also catering for large numbers, we end up with more verbose and admittedly rather ugly solution:
def dot_zero_string(number):
# tested and works with Python 3.3
split = str(number).split('e')
return len(split) == 2 or split[0].endswith('.0')
This is the solution in the answer from #AshwiniChaudhary
def dot_zero_inst(number):
return int(number) == number and isinstance(number, float)
Comparing different cases gives the same result:
numbers = [1, 1.0, 1000, 1000.0, 3e38, 1.5555555555555555555555e12,
1.5555555555555555555555e17, 0, 0.0]
numbers = numbers + [-number for number in numbers]
for number in numbers:
assert dot_zero_inst(number) == dot_zero_string(number)
Just to show another method, you can always split by the '.':
>>> num = 12.023
>>> str(num).split('.')[1] == '0'
False
>>> num = 12.0
>>> str(num).split('.')[1] == '0'
True
Note that this works because you said that all were floating points. This will provide an error num is an int
x = 26.5
b % math.floor(b) == 0
>>> False
x = 26.0
b % math.floor(b) == 0
>>> True
should also do it.