In [ ]:
a=[1, 2, 3]
In [ ]:
b=sum(a)
b
In [ ]:
c=[1, 2, 3.045, 'achilles']
In [ ]:
l=a+c
l
In [ ]:
len(l)
In [ ]:
l[0]
In [ ]:
l[len(l)-1]
In [ ]:
l[-2]
list[start_index:end_index]
In [ ]:
l[0:5]
In [ ]:
l[0:-2]
In [ ]:
l[-2:]
In [ ]:
matrix=[[1,2],[3,4]]
In [ ]:
matrix
In [ ]:
(matrix[0])[0]
In [ ]:
stack=[1, 2, 3, 4]
In [ ]:
stack.append(5)
In [ ]:
stack
In [ ]:
stack.pop()
In [ ]:
queue=['nelson', 'stefan', 'marina', 'valerian']
In [ ]:
queue.pop(0)
In [ ]:
queue
In [ ]:
# def fibonacci(n):
# f
LIMIT = 0.20
alcohol = 0.80
In [ ]:
if alcohol > LIMIT:
print('You are arrested')
else:
print('You pass')
In [11]:
### Recursive Functions, and how to optimize using existent data structures
In [20]:
fibolist = [0, 1]
# fibolist[0] = 0
# fibolist[1] = 1
# fibolist[100] = 100th number
def fibonacci(n):
if n > (len(fibolist) - 1):
value = fibonacci(n - 1) + fibonacci(n - 2)
fibolist.append(value)
return fibolist[n]
In [24]:
import time
import matplotlib.pyplot as pplt
% matplotlib inline
seconds = [ ]
numbers = [ ]
for i in range(0, 10):
start = time.time()
n = fibonacci(i)
end = time.time()
print(n)
seconds.append(end - start)
numbers.append(i)
pplt.plot(numbers, seconds)
Out[24]:
In [29]:
factlist=[1, 1]
def factorial(n):
if n>len(factlist)-1:
value=factorial(n-1)*n
factlist.append(value)
return factlist[n]
In [30]:
import time
import matplotlib.pyplot as pplt
% matplotlib inline
seconds = [ ]
numbers = [ ]
for i in range(0, 200):
start = time.time()
n = factorial(i)
end = time.time()
#print(n)
seconds.append(end - start)
numbers.append(i)
pplt.plot(numbers, seconds)
Out[30]:
In [35]:
sortlist=[1, 5, 3, 7, 2, 0]
In [32]:
sortlist.sort()
In [33]:
sortlist
Out[33]:
In [36]:
sorted(sortlist)
Out[36]:
In [37]:
sortlist
Out[37]:
In [38]:
high=['a', 'b','c','d']
In [40]:
high.insert(3,'nelson')
In [41]:
high
Out[41]:
In [ ]: