I'm new, and I have a question about my python code. It doesn't execute like I think it should.
The part that's messing up is in the nested while:
import math
mu=4*math.pi*10**(-7) ##mu naught
I=
float(input('Enter a value for the current measured in Amperes: '))
R=abs(float(input('Enter a value for the radius
of the circle measured in meters: '))) ##radius
step=R/200 ##Step size for integration
B=[] ##array for B(r)
J=[]
Plot=[B,J]
k = 1 ##step counter for Circumfrence
j = 1 ##step counter for Radius
b_temp = 0 ##place holder to sum b(step*k)
D_Vector = [R*math.cos(k*math.pi/100),R*math.sin(k*math.pi/100)]
R_Vector = [R-j*step,0]
while(j*step<=R):
while(k*step<=2*math.pi*R):
r=(math.sqrt((R_Vector[0]-D_Vector[0])**2 +
(R_Vector[1]-D_Vector[1])**2))
b_temp = b_temp + (mu/(4*math.pi))*I*step*step/(r**2)
k=k+1
D_Vector = [R*math.cos(k*math.pi/100),R*math.sin(k*math.pi/100)]
R_Vector = [R-j*step,0]
print(round(r,3), j)
B.append(round(b_temp,8))
print('New j value!')
b_temp=0
J.append(round(step*j,3))
j=j+1
It's supposed to step down the radius (the first while loop) and then loop around the circle, summing the magnetic field contribution for each chunk of wire. For some reason, it's not looping through the inner loop each iteration of the outer loop like it should be, and I'm honestly not sure why. The new j value line is to let me see if it's looping properly, and this is my output:
...
13.657 1
13.884 1
14.107 1
14.327 1
14.543 1
14.756 1
14.965 1
15.17 1
15.372 1
New j value!
New j value!
New j value!
New j value!
New j value!
New j value!
New j value!
New j value!
...
The 1 at the end of each float (radius value) is the j value, which stays at 1 for each circle around the loop...
You are increasing k but never resetting it to 1. so k * step becomes bigger and bigger until the condition of the inner loop becomes false. At which point is clear that it's not getting executed anymore.
Note that you should avoid while loops when the you are simply iterating over an integer range. Just use for j in range(a, b). This avoid exactly the kind of bug you have in your code.
If I'm not mistaken you can replace the loops with:
area = 2*math.pi*R
for j in range(1, R//step + 1):
# j*step <= R holds
for k in range(1, area // step + 1):
# k * step <= area holds here
where a // b is quotient of a and b.
Related
I can't understand why iterations made by i depend on the value of j I write
I have been changing the value of j for a number of different values, for example j=-6 and my code does 6 loops for each i: 111111,222222 for the range I specified
p = 1
for i in range(1, 3):
j = -3
while abs(i * j) > 0:
p = i * j
j = j + 1
result = p
The output is right, but I need to understand why the value of j affects how many iterations of why there are
The j variable in no way affects your iterations of i, there will *always be two of those, one each for the values 1 and 2.
What will change is the inner loop, the while one. The expression abs(i * j) (where i is either 1 or 2) will always be positive until j reaches zero (counting up from some negative number.
Therefore, a j value of -99 will cause more iterations of this while loop than a vaule of -3.
New to python here, so not sure if I'm posting in the right place but I was wondering why the following loop does not give an answer. I am trying to add all of the numbers previous to i, (1 + 2 + 3 + ... + i), but the loop I came up with does not complete.
j = 0
i = 17
while i > 0:
j = j + i
i - 1
print(j)
I expect j = 153, but the loop doesn't put out anything.
The issue is with i--you're not properly reducing it by 1, so this is an infinite loop. Try this:
j = 0
i = 17
while i > 0:
j = j + i
i -= 1
print(j)
Notice that your line in the while statement,
i-1, does not assign (= operator) a value to i. i stays 17 forever and ever, and so you see no output because the program gets stuck in your while loop. Try to fix your i assignment, like so:
j = 0
i = 17
while i > 0:
j = j + i
i = i - 1 # <= i = i - 1
print(j)
One way to debug your loop in the future is to put a print statement in the middle, like in the code below. The extra print statements slow your program down a bit, but you'll be able to easily see that your loop is going way more than you want!
j = 0
i = 17
while i > 0:
j = j + i
i = i - 1
print(j)
print(i) # so that we can see that i is going down :P
print(j)
Good luck :)
As noted above, you're not re-assigning to your i within the loop. But I'd also note that it's generally a bad idea to modify an iterator within the iteration loop. You can achieve the expected results in a more pythonic way using a range expression, and wrap it in a function also :)
def foo(max_num):
j = 0
for i in range(max_num+1):
j += i
return j
But even better, without the loop:
def foo(max_num):
return sum(range(max_num+1))
There are a couple of ways to do what you want to do. The way I recommend you do it is as shown below.
total = 0
i = 18
for j in range(i):
total += j
print(total)
The range(i) takes a maximum number i, and allows you to iterate through every number starting from 0 to i-1 (e.g. i = 5 will add numbers 0,1,2,3,4), therefore always make i one more than you need it to be if you want to be if you want to include the max number.
As already stated i - 1 wasn't assigned back to i
Following is a pythonic way of implementing the loop with a list comprehension
Code:
j = 0
i = 17
j = sum([j + x for x in range(i, 0, -1)])
print(j)
>>> 153
As a Function:
def sum_of_steps(start: int, stop: int, step: int) -> int:
return sum([x for x in range(start, stop, step)])
j = sum_of_steps(17, 0, -1)
print(j)
>>> 153
The function, sum_of_steps uses type hints or function annotations.
Python Basics: List Comprehensions
I'm having trouble understanding the 2nd for loop in the code below:
di = [4,5,6]
for i in range(len(di)):
total = di[i]
for j in range(i+1,len(di)):
total += di[j]
curr_di = total / ((j-i+1)**2)
I can't visualize what happens in for j in range(i+1,len(di)):, in particular the i+1 portion confuses me. how does the i in the first loop affect the 2nd loop, if any?
The first loop is simply looping over the indices available in list di. For each entry in that loop, the second loop then examines the remaining portions of di.
So, on the first iteration, we're examining the value 4. The second loop will then walk the list starting at that position, and running to the end (it will examine items 5 and 6).
On the second iteration, we'll examine entry 5, then walk the remainder of the list in the second loop (in this case 6). Make sense?
As a commenter pointed out, print statements are your friend. Here's an example with some print statements to show how i and j change:
di = [4,5,6,7]
for i in range(len(di)):
print(f"i: {i}")
total = di[i]
for j in range(i+1, len(di)):
print(f" - j: {j}")
total += di[j]
curr_di = total / ((j-i+1)**2)
Output:
i: 0
- j: 1
- j: 2
- j: 3
i: 1
- j: 2
- j: 3
i: 2
- j: 3
i: 3
This is a typical "combinations of 2" loop. Each item in the list (index i) is processed with every subsequent item (index j).
It looks like sequence processing to compute a sum of partial sums:
total = ∑(i=1..n) ( ∑(j=i..n) a[j] )
I am a beginner to programming. I always encounter this problem for matrices. Please help me to correct this code and understand the concept behind this. Thank you.
def new_matrix(matrix_size, center_num):
c = matrix_size / 2
if matrix_size % 2 != 0:
p = matrix_size
print("enter a valid size for matrix")
else:
matrix = [[0] * matrix_size for z in range(matrix_size)]
for counting in range(center_num, (matrix_size ** 2) + center_num):
for i in range(2, matrix_size+1):
row = int(c)
column = int(c)
if (i % 2 == 0):
for k in range(1, i + 1): # moving right
column += 1
matrix[column][row] = counting
for k in range(1, i + 1): # moving up
row += 1
matrix[column][row] = counting
else:
for k in range(1, i + 1): # moving left
column -= 1
matrix[column][row] = counting
for k in range(1, i + 1): # moving down
row -= 1
matrix[column][row] = counting
print(matrix)
new_matrix(6, 2)
This issue here seems to be that your indexing is off. The end value in range(n) is n-1, which means that in your for loop when i = matrix_size and then you try to increment the column beyond this, you're trying to assign a value to an index in the matrix that doesn't exist.
You can either try and fix your loop, i.e. reduce it by one, or you can do something like the following,
try:
<your code goes here>
except IndexError:
pass
And then anytime the loop hits an index error it will skip that index and try the next. It's better to fix the code though!
So I'm working on some practice problems and having trouble reducing the complexity. I am given an array of distinct integers a[] and a threshold value T. I need to find the number of triplets i,j,k such that a[i] < a[j] < a[k] and a[i] + a[j] + a[k] <= T. I've gotten this down from O(n^3) to O(n^2 log n) with the following python script. I'm wondering if I can optimize this any further.
import sys
import bisect
first_line = sys.stdin.readline().strip().split(' ')
num_numbers = int(first_line[0])
threshold = int(first_line[1])
count = 0
if num_numbers < 3:
print count
else:
numbers = sys.stdin.readline().strip().split(' ')
numbers = map(int, numbers)
numbers.sort()
for i in xrange(num_numbers - 2):
for j in xrange(i+1, num_numbers - 1):
k_1 = threshold - (numbers[i] + numbers[j])
if k_1 < numbers[j]:
break
else:
cross_thresh = bisect.bisect(numbers,k_1) - (j+1)
if cross_thresh > 0:
count += cross_thresh
print count
In the above example, the first input line simply provides the number of numbers and the threshold. The next line is the full list. If the list is less than 3, there is no triplets that can exist, so we return 0. If not, we read in the full list of integers, sort them, and then process them as follows: we iterate over every element of i and j (such that i < j) and we compute the highest value of k that would not break i + j + k <= T. We then find the index (s) of the first element in the list that violates this condition and take all the elements between j and s and add them to the count. For 30,000 elements in a list, this takes about 7 minutes to run. Is there any way to make it faster?
You are performing binary search for each (i,j) pair to find the corresponding value for k. Hence O(n^2 log(n)).
I can suggest an algorithm that will have the worst case time complexity of O(n^2).
Assume the list is sorted from left to right and elements are numbered from 1 to n. Then the pseudo code is:
for i = 1 to n - 2:
j = i + 1
find maximal k with binary search
while j < k:
j = j + 1
find maximal k with linear search to the left, starting from last k position
The reason this has the worst case time complexity of O(n^2) and not O(n^3) is because the position k is monotonically decreasing. Thus even with linear scanning, you are not spending O(n) for each (i,j) pair. Rather, you are spending a total of O(n) time to scan for k for each distinct i value.
O(n^2) version implemented in Python (based on wookie919's answer):
def triplets(N, T):
N = sorted(N)
result = 0
for i in xrange(len(N)-2):
k = len(N)-1
for j in xrange(i+1, len(N)-1):
while k>=0 and N[i]+N[j]+N[k]>T:
k-=1
result += max(k, j)-j
return result
import random
sample = random.sample(xrange(1000000), 30000)
print triplets(sample, 500000)