-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab2.py
63 lines (50 loc) · 1.47 KB
/
lab2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'''
Avery Cunningham
Lab 2
2/18/2021
I pledge my honor that I have abided by the Stevens Honor System
'''
def dot(L, K):
"""Returns the dot product of the elements in lists L and K"""
if L == [] and K == []:
return 0.0
else:
return L[0] * K[0] + dot(L[1:], K[1:])
def explode(S):
"""Explodes a string into a respective list of single character strings"""
if S == "":
return []
else:
return [S[0]] + explode(S[1:])
def ind(e, L):
"""Returns the index of the first occurrence of e in L"""
if L == "" or L == []: # is there a way to check for a blank string or list with the same syntax? can we use 'not'?
return 0
elif L[0] == e:
return 0
else:
return 1 + ind(e, L[1:])
def removeAll(e, L):
"""Removes all top-level occurrences of element e in list L"""
if L == []:
return []
elif L[0] == e:
return removeAll(e, L[1:])
else:
return [L[0]] + removeAll(e, L[1:])
def myFilter(f, L):
"""Returns a list of elements in L where the function f returns True"""
if L == []:
return []
elif f(L[0]):
return [L[0]] + myFilter(f, L[1:])
else:
return myFilter(f, L[1:])
def deepReverse(L):
"""Reverses the order of a list and all lists contained within"""
if L == []:
return []
elif isinstance(L[0], list):
return deepReverse(L[1:]) + [deepReverse(L[0])]
else:
return deepReverse(L[1:]) + [L[0]]