Created
February 26, 2023 02:29
-
-
Save tarmacjupiter/4120e8afb57db0559174b3caadbf426d to your computer and use it in GitHub Desktop.
Python Algorithm Anaylsis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Anthony Behery | |
www.freecodecamp.org/news/python-vs-c-a-time-complexity-analysis/ | |
""" | |
import time | |
def ex1(A): | |
sum = A[0] | |
for i in A: | |
sum = sum + A[i] | |
return sum | |
def ex2(A): | |
sum = A[0] | |
for i in range(2, len(A), 2): | |
sum = sum + A[i] | |
return sum | |
def ex3(A): | |
sum = 0 | |
for i in range(len(A)): | |
sum = sum + A[0] | |
for j in range(1, i): | |
sum = sum + A[j] | |
return sum | |
def ex4(A): | |
s = A[0] | |
t = s | |
for i in range(len(A)): | |
s = s + A[i] | |
t = t + s | |
return t | |
def ex5(A, B): | |
c = 0 | |
for i in range(len(A)): | |
s = 0 | |
for j in range(len(A)): | |
s = s + A[0] | |
for k in range(1, j): | |
s = s + A[k] | |
if B[i] == s: | |
c = c + 1 | |
return c | |
# Use a for loop to insert values for arrays | |
# Recording the amount of time | |
start_time = time.time() | |
# insert algorithm function here | |
print(f"{time.time() - start_time}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment