Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm taking a course through Stackskills on python and I'm on iterators and loops. The teacher is getting a different outcome than I am and I was wondering why:
You have:
sum = sum + sum # you are only adding sum to itself
you should change it to:
sum = sum + num # here you will be adding the current list item to sum
In the loop your teacher is adding
sum = sum + num # Attention its an n
But you are adding:
sum = sum + sum # which is sum = 0 + 0
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed yesterday.
Improve this question
error in for loop in python
I wanted to run this code, but this error occurs in the for section
h want to know why i cant use unequal in for
x = 18
y = 12
for n !=0:
n = x%y
print(n)
A mentioned by #Gabio, you should use a while loop like:
x = 18
y = 12
while n !=0:
n = x%y
print(n)
A for loop requires a defined range/collection of items to iterate over, whereas a while loop executes until a condition is met:
for n in range(0,10):
# do something
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 days ago.
Improve this question
a = [1,3,4,5,6,7,8,9]
start = 0
lenth = len(a) -1
rolling = 1
while start < rolling:
if rolling == lenth:
print(f'start -> {a[start]} | end -> {a[rolling]}')
start += 1
rolling == start + 1
else:
print(f'start -> {a[start]} | end -> {a[rolling]}')
rolling += 1
I want to print two pointer algorithm. first round is ok. but in second round rolling variable can't re-assign next to start variable. anyone can suggest solution to print out a list according to the two pointer algorithm ?
I want solve above two pointer algorithm...
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
here I want to store the square root values of userDist in userDist list with this for loop like below:
userDist = []
for i in usersXcordd:
userDist[i].append(math.sqrt((usersXcordd[i] - baseStationX)^2 + (usersYcordd[i] -
baseStationY)^2))
You can just append a value to a list instead of appending it to a value of the list. For example, the following code would work.
userDist.append(value)
But the following would not work (unless userDist[i] itself is a list).
userDist[i].append(value)
You can read more about the append method here.
The complete code
The complete and working code should be as follows.
userDist = []
for i in usersXcordd:
value = math.sqrt((usersXcordd[i] - baseStationX)**2 + \
(usersYcordd[i] - baseStationY)**2)
userDist.append(value)
Use this instead(assuming Y coordinate list and X Xoordinate List have same length):
userDist = []
for j,i in enumerate(usersXcordd):
userDist.append(((i - baseStationX)**2 + (usersYcordd[j] -baseStationY)**2)**0.5)
Enumerate gives two values: index and element of iterable respectively
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I having some problems trying to understand generators.
Do these function's execution time differ when doing the same task?
def slow_sum(size):
x= 0
for i in range(size):
for j in range(size):
x += i + j
return x
def fast_sum(size):
return sum( [ (i+j) for j in range(size) for i in range(size)] )
size = 2000
slow_val = slow_sum(size)
fast_val = fast_sum(size)
assert slow_val == fast_val, "Values are not equal"
When profiling both functions on my computer using cProfile I got these result, but I expected them to be similar.
Total Time
slow_sum(2000)
0.85 ms
fast_sum(2000)
0.05 ms
Original File: https://pastebin.com/fDfaSqyZ
My Output: https://pastebin.com/wyy3v3iy
You're looking at the wrong column of the profiler output. tottime doesn't count all the time fast_sum spends inside the sum call or the list comprehension's stack frame. You should be looking at cumtime, which is near equal for the two functions.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
While reading an algorithm book, I found the following exercise.
Given a set of n elements, write an algorithm that finds number of
ways of partitioning it.
Example: When n = 2, there are 2 ways of partitioning the set(into
two sets with one element, or into the original set and the empty set).
And instead of the algorithm, I tried the python code using dynamic programming.
def ways(n):
dp = [0]*(n+1),
sum = [0]*(n+1) ## declaring 2 arrays of n+1 size
dp[0] = 0
dp[1] = 1
sum[0] = 0
sum[1] = 1
lastcalc = 1 # last calculated var
for i in range (2,n):
if lastcalc < i/2 :
for j in range (lastcalc, i/2):
sum[j] = sum[j-1] + dp[j]
lastcalc = (i/2) # update the lastcalculated variable
dp[i] = sum[i/2]
return dp[n]
print(ways(2))
But, the code won't work and gave me an error.
TypeError: 'tuple' object does not support item assignment
My question: how can I fix this? Can I say this code applied a dynamic programming?
You have a comma at the end of the declaration of dp. This makes it a tuple, not a list, and tuple are not modifiable. Just remove it, it's a typo.