- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpythonFunctions.py
More file actions
Latest commit
81 lines (68 loc) · 2.25 KB
/
Copy pathpythonFunctions.py
File metadata and controls
81 lines (68 loc) · 2.25 KB
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
defrecursiveArraySearch(toFind, theArray):
print("Calling recursiveArraySearch")
print("recursively search for "+str(toFind)+" in the array "+str(theArray))
allIndices= []
defrecursiveArraySearch1(toFind1, theArray1, allIndices, currentIndex):
#print("Searching within " + str(theArray1))
thingToAdd= [ifori, xinenumerate(theArray1) ifx==toFind1]
#print("To add to the result: " + str(thingToAdd))
#print("Current index: " + str(currentIndex))
allIndices+= [currentIndex+thingToAdd]
foriinrange(0, len(theArray1)):
iftype(theArray1[i]) ==list:
recursiveArraySearch1(toFind1, theArray1[i], allIndices, currentIndex+[i])
recursiveArraySearch1(toFind, theArray, allIndices, [])
#print("All indices: " + str(allIndices))
returnallIndices
defrepresentsInt(s):
try:
int(s)
returnTrue
exceptValueError:
returnFalse
defderp(hi):
return"derp "+hi
defisAParameter(theInput):
if(type(theInput) ==str):
iftheInput.startswith("<<") andtheInput.endswith(">>"):
returnTrue
returnFalse
defchunks(l, n):
""" Yield successive n-sized chunks from l.
"""
foriinxrange(0, len(l), n):
yieldl[i:i+n]
deffunctionsMatchingInputs(theFunctions, theInputs, toReturn):
theOutputDictionary= {}
forcurrentFunctionintheFunctions:
theOutputDictionary[currentFunction] = []
forcurrentInputintheInputs:
currentOutput=currentFunction(currentInput)
ifcurrentOutput==toReturn:
theOutputDictionary[currentFunction] += [currentInput]
returntheOutputDictionary
defgcd(a,b):
ifb==0:
returna
else:
returngcd(b, (a%b))
defgcd_list(l):
result=l[0]
foriinrange(1, len(l)):
result=gcd(result,l[i])
returnresult
deflcm(a, b):
"""Return lowest common multiple."""
returna*b//gcd(a, b)
defis_prime(a):
returnall(a%iforiinxrange(2, a))
printgcd_list([10, 5, 14,])
defwriteStringToFile(fileName, theText):
withopen(fileName, "w") astext_file:
text_file.write(theText)
defstringFromTextFile(fileName):
withopen (fileName, "r") asmyfile:
returnmyfile.read()
deffactors(n):
returnset(reduce(list.__add__,
([i, n//i] foriinrange(1, int(n**0.5) +1) ifn%i==0)))