import time,random
n=int(input("put your number:"))
X=[random.randint(-999,999) for i in range(n)]
print(X)
#pr1 start
before1 = time.clock()
def prefixSum1(X, n): # code for prefixSum1
for i = 0 to n-1 do
S[i] = 0
for j = 0 to i do
S[i]=S[i]+X[j]
return (S)
after1 = time.clock()
#pr2 start
before2 = time.clock()
def prefixSum2(X, n): # code for prefixSum2
S[0]= X[0]
for i = 1 to n-1 do
S[i]=S[i-1]+X[i]
return (S)
after2 = time.clock()
#
random.seed()
print(after1 - before1)
print(after2 - before2)
This is my code and what I am trying to make is that I randomly generate the array X from -999 to 999 and check the time of two algorithms. But whenever I run the code, it shows >
Makefile:6: recipe for target 'py3_run' failed
make: *** [py3_run] Error 1
File "Main.out", line 11
for i = 0 to n-1 do
^
SyntaxError: invalid syntax
I have no idea what is wrong with my code. I tried to find on the internet but it seems they are the same as me...
Your code does not use valid python syntax for your for
loops; the documentation on for
statements clearly describes the syntax. In short, you need to change
for i = 0 to n-1 do
to
for i in range(n - 1):
and
for i = 1 to n-1 do
to
for i in range(1, n - 1):
You can check the documentation for range
to learn about how it works.