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.
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
I can't find where the missing ":" is.
My code is:
def A_star_Traversal(cost, heuristic, start_point, goals):
path = []
hashTable = {}
for i in goals:
hashTable[i] = 1
n = len(cost)
visited = [False for j in range (n)]
pq = [[0+heuristic[0],start_point]]
heapq.heapify(pq)
while(len(pq)):
v = heapq.heappop(pq)
if(hashTable.get(v)):
path.append(v)
break
visited[v] = True
path.append(v)
f=1
for i in range n: '''this is where i get the error'''
if(visited[i]==False and cost[v][i]>0):
pq.append([cost[v][i]+heuristic[i],i])
heapq.heapify(pq)
f=0
if(f):
path.pop()
return path
I am getting the error at for i in range n:.
I looked through all the for and while loop and if else condition statements making sure that each one had ':'. However, despite finding no missing ':', the compilation clearly indicates that I have missed it somewhere. I am hoping that it would not be the same for others.
You appear to be missing brackets around the n. In Python 3 (if that is what you are using) it should be:
for i in range(n):
the error comes because u didn't enclosed the n in brackets
for i in range(n):
Try doing it like this
for i in range(n):
#rest of the code
I think you are getting the error because you missed the parenthesis ()
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
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.