Divide a number by numbers from a list - python-3.x

I have this script, it outputs this list of numbers after running
x = 542357098500868790
y = 0
while x > y:
print(x)
x = x // 108
Result
542357098500868790
5021824986119155
46498379501103
430540550936
3986486582
36911912
341776
3164
29
I want to create a list of numbers like "108,109,110,111 and so on, so that the script takes a number from the script and divides it by what is in the list and saves the result in one file or displays everything on the screen, so that it looks like this
542357098500868790
5021824986119155
46498379501103
430540550936
3986486582
36911912
341776
3164
29
542357098500868790
4975753197255676
45649111901428
418799191756
3842194419
35249490
323389
2966
27

this is a good opportunity to use lists and a for loop.
nums = [108, 109, 110, 111]
for div in nums:
x = 542357098500868790
y = 0
while x > y:
print(x)
x = x // div
Alternatively use the range function to generate a range of numbers like so:
nums = range(108,112)

Related

How to check a list of lists against a list of lists and count their overlaps? (Python)

I have one list a containing 100 lists and one list x containing 4 lists (all of equal length). I want to test the lists in a against those in x. My goal is to find out how often numbers in a "touch" those in x. Stated differently, all the lists are points on a line and the lines in a should not touch (or cross) those in x.
EDIT
In the code, I am testing each line in a (e.g. a1, a2 ... a100) first against x1, then against x2, x3 and x4. A condition and a counter check whether the a's touch the x's. Note: I am not interested in counting how many items in a1, for example, touch x1. Once a1 and x1 touch, I count that and can move on to a2, and so on.
However, the counter does not properly update. It seems that it does not tests a against all x. Any suggestions on how to solve this? Here is my code.
EDIT
I have updated the code so that the problem is easier to replicate.
x = [[10, 11, 12], [14, 15, 16]]
a = [[11, 10, 12], [15, 17, 20], [11, 14, 16]]
def touch(e, f):
e = np.array(e)
f = np.array(f)
lastitems = []
counter = 0
for lst in f:
if np.all(e < lst): # This is the condition
lastitems.append(lst[-1]) # This allows checking the end values
else:
counter += 1
c = counter
return c
touch = touch(x, a)
print(touch)
The result I get is:
2
But I expect this:
1
2
I'm unsure of what exactly is the result you expect, your example and description are still not clear. Anyway, this is what I guess you want. If you want more details, you can uncomment some lines i.e. those with #
i = 0
for j in x:
print("")
#print(j)
counter = 0
for k in a:
inters = set(j).intersection(k)
#print(k)
#print(inters)
if inters:
counter += 1
#print("yes", counter)
#else:
#print("nope", counter)
print(i, counter)
i += 1
which prints
0 2
1 2

How to understand this simultaneous assignment evaluation in python 3? [duplicate]

This question already has answers here:
Multiple assignment and evaluation order in Python
(11 answers)
Closed 3 years ago.
I have recently started learning python. I was playing around with simultaneous assignments today and came across some results that were produced by my code that I cannot understand.
x, y = 3, 5
x, y = y, (x+y)
print(y)
The output is 8.
I am not understanding why y = 8 instead of y = 10 despite x = y = 5 being evaluated first. Since y = 8, this tell us that x = 3 when y = x + y is being evaluated ? How is that possible if x = y is first evaluated, since it is evaluated left to right ?
I first debugged the code (which produced the same result), which I cannot understand. I also have tried looking at the python documentation, where it states "simultaneous overlaps within the collection of assigned-to variables occur left-to-right, sometimes resulting in confusion."
The example that it presents follows my logic:
x = [0, 1]
i = 0
i, x[i] = 1, 2
print(x)
It outputs 2.
(Evaluated left to right) Since i is updated to 1. Then, this updated i is used and hence, x[1] = 2 not x[0] = 2.
I really would appreciate some help.
I am not understanding why y = 8 instead of y = 10 despite x = y = 5 being evaluated first
The right side of an assignment is evaluated first.
In your assignment with x = 3 and y = 5
x, y = y, (x+y)
the right side is evaluated to a tuple (5, 8) first and then assigned to the values on the left side. Therefore y is 8.
You could also think of it as
x, y = 3, 5
temp = y, x + y
x, y = temp
To see what really happens internal, you can disassemble your code:
>>> import dis
>>> def f(x, y):
... x, y = y, x + y
...
>>> dis.dis(f)
Outputs
2 0 LOAD_FAST 1 (y)
2 LOAD_FAST 0 (x)
4 LOAD_FAST 1 (y)
6 BINARY_ADD
8 ROT_TWO
10 STORE_FAST 0 (x)
12 STORE_FAST 1 (y)
14 LOAD_CONST 0 (None)
16 RETURN_VALUE
As you can see, the addition is performed before the assignment.
Python goes right-to-left, left-to-right. You can imagine that python pushes its operations to a stack-like data structure.
Looking at the first assignment: x, y = 3, 5
python will first push the right-hand side value onto the stack as a tuple.
run an unpacking-sequence for n values from the stack, and puts the values back onto the stack right-to-left. "push 5 to the stack, then 3". Current stack = [3, 5]
Finished with the right-hand side, python will assign values to the left-hand side left-to-right, while removing top-of-stack. So it will first tak 3 and store it in variable x, then 5 and store it in variable y.
You can inspect the operations python does in byte code using the dis module.
The following assignments:
x, y = 3, 5
x, y = y, (x + y)
Produce the following operations:
You can inspect the bytecode operations here: http://pyspanishdoc.sourceforge.net/lib/bytecodes.html

How to solve Equation without any Modules in Python?

I want to solve this equation without any Modules(NumPy, Sympy... etc.)
Px + Qy = W
(ex. 5x + 6y = 55)
Thanks.
It is a very crude way to do this, but you can use brute-force technique, as I said in comment under your question. It can probably be optimized a lot, gives only int outputs, but overall shows the method:
import numpy as np
# Provide the equation:
print("Provide a, b and c to evaluate in equation of form {ax + by - c = 0}")
a = float(input("a: "))
b = float(input("b: "))
c = float(input("c: "))
x_range = int(input("x-searching range (-a, a): "))
y_range = int(input("y-searching range (-b, b): "))
error = float(input("maximum accepted error from the exact solution: "))
x_range = np.arange(-x_range, x_range, 1)
y_range = np.arange(-y_range, y_range, 1)
for x in x_range:
for y in y_range:
if -error <= a * x + b * y - c <= error:
print("Got an absolute error of {} or less with numbers x = {} and y = {}.".format(error, x, y))
Example output for a = 1, b = 2, c = 3, x_range = 10, y_range = 10, error = 0.001:
Got an error of 0.001 or less with numbers x = -9 and y = 6.
Got an error of 0.001 or less with numbers x = -7 and y = 5.
Got an error of 0.001 or less with numbers x = -5 and y = 4.
Got an error of 0.001 or less with numbers x = -3 and y = 3.
Got an error of 0.001 or less with numbers x = -1 and y = 2.
Got an error of 0.001 or less with numbers x = 1 and y = 1.
Got an error of 0.001 or less with numbers x = 3 and y = 0.
Got an error of 0.001 or less with numbers x = 5 and y = -1.
Got an error of 0.001 or less with numbers x = 7 and y = -2.
Got an error of 0.001 or less with numbers x = 9 and y = -3.
I am using numpy, but not a built-in function to solve the equation itself, just to create an array. This can be done without it, of course.
There are thousands of ways to solve an equation with python.
One of those is:
def myfunc (x=None, y=None):
return ((55-6*y)/5.0) if y else ((55-5*x)/6.0)
print(myfunc(x=10)) # OUTPUT: 0.833333333333, y value for x == 10
print(myfunc(y=42)) # OUTPUT: -39.4, x value for y == 42
You simply define inside a function the steps required to solve the equation.
In our example, if we have y value we subtract 6*y to 55 then we divide by 5.0 (we add .0 to have a float as result), otherwise (means we have x) we subtract 5*x from 55 and then we divide by 6.0
with the same principle, you can generalize:
def myfunc (x=None, y=None, P=None, Q=None, W=None):
if not W:
return P*x + Q*y
elif not x:
return (W-Q*y)/float(P)
elif not y:
return (W-P*x)/float(Q)
elif not P:
return (W-Q*y)/float(x)
elif not Q:
return (W-P*x)/float(y)
print(myfunc(x=10, P=5, Q=6, W=55)) # OUTPUT: 0.833333333333, y value for x == 10
print(myfunc(y=42, P=5, Q=6, W=55)) # OUTPUT: -39.4, x value for y == 42
check this QA for some other interesting ways to approach this problem

how to define a certain value to equal to constant just for the first iteration?

I have a the following code (as an example):
answ = []
for i in range(1, 3):
x = y + 2
y = 3 + x
answ.append(y)
Where x and y are simultaneously determined. How can I determine them simultaneously? Or how can I assume that for the first loop y=0 (so x will equal to 2) and then starting from the second iteration 'y' = 3 + x.
Just set y to 0 before the for loop:
answ = []
y = 0
for i in range(1, 3):
x = y + 2
y = 3 + x
append.answ(y)
Like this:
x, y = y + 2, 3 + x
Right now, x and y are local variables, meaning they are deleted at the end of each iteration of the for loop. If you want them to carry over from the previous iteration, you need to define x and y outside of the for loop. This puts them above and the scope of the loop, so they don't get redefined.
Another thing, your for loop runs 2 times, because computers count from 0 and don't include the last number. Because i is not used in your loop at all, it would be better to do:
for i in range(2):
# code
But I am guessing that you want your loop to run three times, in which case you would write:
for i in range(3):
# code
I also noticed that you wrote append.answ(y) instead of answ.append(y). append is a member function of answ, so you would call it the second way.
Anyways, here is the final code for your program:
answ = []
x = 0
y = 0
for i in range(3):
x = y + 2
y = 3 + x
answ.append(y)

Function to pass a list of numbers and return the average of the numbers at each iteration

I am working to program this function and I seem to be close. The problem is that even though I have defined y = 0, when the first loop iteration runs, y is being returned as 10. It seems to be pulling from the first number in the list, as when I change it, both i and y change.
def Meanlist(x):
y = 0
z = 1
for i in x:
y += i
Avg=(y+i)/z
print('For Iteration', z, 'Average is', Avg)
print('For Iteration', z, 'i is', i)
print('For Iteration', z, 'y is', y)
z+=1
L1 = [10, 56.7, 56, 89, 100, 99, 87.5, 34, 985, 10]
Meanlist(L1)
This is returning these values for the first iteration. What am I doing wrong with y? Everything else appears to be working with the function.
For Iteration 1 Average is 20.0
For Iteration 1 i is 10
For Iteration 1 y is 10
The first thing you do in your loop is
y += i
In the first iteration, y is 0 but i equals to 10, so after doing "y += i", y equals to 10.
Then you do:
Avg=(y+i)/z
Since y equals to 10, (y + i) equals to 20.

Resources