-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
85 lines (69 loc) · 1.99 KB
/
test.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def listSum(numList):
theSum = 0
for i in numList:
theSum += i
return theSum
def listSum2(numList):
if len(numList) == 1:
return numList[0]
else:
return numList[0] + listSum2(numList[1:])
# a = [1,3,5,7,9]
# print(listSum(a))
# print(listSum2(a))
def toStr(n, base):
convertString = "0123456789ABCDEF"
if n < base:
return convertString[n]
else:
return toStr(n // base, base) + convertString[n % base]
# print(toStr(1453, 16))
def reverse(aString):
if len(aString) <= 1:
return aString
return reverse(aString[1:]) + aString[:1]
# print(reverse('abcd'))
def removeWhite(s):
if s == '':
return ''
if s[0].lower() in 'abcdefghijklmnopqrtsuvwzxyz':
return s[0] + removeWhite(s[1:])
else:
return removeWhite(s[1:])
def isPal(s):
if len(s) <= 1:
return True
if s[0] == s[-1]:
return isPal(s[1:-1])
else:
return False
# print(isPal('hello'))
def recDC(coinValueList, change, knownResults):
print(change, knownResults)
minCoins = change
if change in coinValueList:
knownResults[change] = 1
return 1
elif knownResults[change] > 0:
return knownResults[change]
else:
for i in [c for c in coinValueList if c <= change]:
numCoins = 1 + recDC(coinValueList, change - i, knownResults)
print(minCoins, numCoins)
if numCoins < minCoins:
minCoins = numCoins
knownResults[change] = minCoins
return minCoins
# print(recDC([1, 5, 10, 25], 63, [0] *64))
def sequentialSearch(alist, item):
pos = 0
found = False
while pos < len(alist) and not found:
if alist[pos] == item:
found = True
else:
pos += 1
return found
testlist = [1,2,32,8,17,19,42,13,0]
print(sequentialSearch(testlist, 3))
print(sequentialSearch(testlist, 13))