-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseful.py
More file actions
executable file
·252 lines (207 loc) · 5.25 KB
/
useful.py
File metadata and controls
executable file
·252 lines (207 loc) · 5.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
from random import random, randint, choice
import sys
import os
def build( extension = None ):
"""
#Appends desired path (default = python docs file) to sys filepath
#Place in main python folder, then import as is needed
"""
# import sys
filePath = "C:\Users\Owner\Documents\Python"
if extension:
filePath += '\\' + extension
sys.path.append(filePath)
print 'Filepath added: ' + filePath
def fill( i, buffer=3 ):
"""
Given a number, returns the equivalent string, preceded
by zeroes until it fills the buffer length.
"""
c = str(i)
while(len(c) < buffer):
c = "0"+c
return c
def rAlloc( points, alloc, allocMax = [] ):
"""
Function to allocate points randomly into a range of categories.
Takes a dictionary with zeroed values, and the number of points to
allocate, and returns the appropriately incremented dictionary.
'allocMax', if entered, is a list with the maximum value allowed for
each dictionary value. Requires an entry for each category.
"""
if allocMax:
total = 0
for v in allocMax:
total += v
if points > total:
points = total
sml = min(allocMax)
aTemp = []
for i in range(len(allocMax)):
aTemp.append(allocMax[i]/sml)
while points > 0:
index = lottery(aTemp)
alloc[index] += 1
points -= 1
if alloc[index] > allocMax[index]:
alloc[index] -= 1
points += 1
else:
while points > 0:
stat = randint(0,(len(alloc)-1))
alloc[stat] +=1
points -= 1
return alloc
def lottery(items):
"""
Simulates a lottery draw
Input 'items' is a list, with each index position
representing a unique entrant in the lottery, and
each value representing the number of tickets held
by that entrant.
Returns the winning entrant's index number.
"""
l = len(items)
pointer = 0
votes = []
for n in items:
for i in range(n):
votes.append(pointer)
pointer += 1
winningIndex = choice(votes)
return winningIndex
def freq(li):
#returns a dictionary showing how often each element occurs in a list
fDict = {}
for i in li:
if i in fDict:
fDict[i] += 1
else: fDict[i] = 1
return fDict
def cFreq(items,n):
"""
Given a 'lottery' list and a number of lottery runs,
function returns a dictionary showing how many times
each entrant was selected.
"""
choiceList = []
freqDict = {index:0 for index in items}
for i in range(n):
choiceList.append(lottery(items))
for i in choiceList:
freqDict[items[i]] += 1
return freqDict
def listFromFreqDict( dic, minToMax=False ):
"""
Returns an ordered list from a frequency dictionary
(Ordered from most to least frequent by default)
"""
freqList = [[dic.values()[i],dic.keys()[i]] for i in range(len(dic.items()))]
freqList.sort( reverse = not minToMax )
li = []
for entry in range(len(freqList)):
li.append(freqList[entry][1])
return li
def percentCheck( n ):
"""
Generates a random number between 0-100, and checks
whether it is less than a given value.
Returns 1 if random number < n, otherwise returns 0
"""
r = randint(0,100)
if r < n:
return 1
else: return 0
def tournament( pl, game ):
"""
Stages a tournament in which each player competes with every other player.
Takes a list of players, and the desired game to be played, and returns a
zipped list of each player and their score.
'game' syntax:
Parameters: List of players
return: 0 if pl[0] wins
1 if pl[1] wins
-1 if tie
"""
losses=[0 for p in pl]
for i in range(len(pl)):
for j in range(len(pl)):
if i==j: continue
winner=game([pl[i],pl[j]])
if winner==0:
losses[j]+=2
elif winner==1:
losses[i]+=2
elif winner==-1:
losses[i]+=1
losses[j]+=1
pass
z=zip(losses,pl)
z.sort()
return z
def listify( self, *entity ):
#Returns a list of non-list entities
if isinstance(entity,list):
return entity
list = []
for e in entity:
if isinstance(e,list) == False:
list.append(e)
return list
def unzip(li):
#turns list: [[a,b],[c,d]...[y,z]] into list [[a,c...y],[b,d...z]]
x = []
y = []
for i in li:
x.append(i[0])
y.append(i[1])
return [x,y]
def match(vn,wn):
#from [[v1,n1],[v2,n2]...] and [[w1,n1],[w2,n2]...]
#to [v1,v2,v3,v4],[w1,w2,w3,w4]
v = []
w = []
for i in vn:
for j in wn:
if i[1] == j[1]:
v.append(i[0])
w.append(j[0])
return [v,w]
def pfac(n):
#returns list of the prime factors of 'n' (non-unique)
r = n
f = 2
pfs = []
while r > 1:
if r%f == 0:
pfs.append(f)
r = r/f
else:
if f==2: f+=1
else: f+=2
return pfs
def checkdiv(n,x = [3,5]):
#returns the sum of all number smaller than 'n' that are divisible by all the numbers in list 'x'
li = []
s = 0
for i in range(n):
for j in x:
if (i/float(j))%1 == 0:
li.append(i)
break
return sum(li)
def findLargest(string,ss=5):
#Given a string of integers, returns the largest product of every possible 'n'-sized sequence
sarray = []
seq = 0
p = 0
for i in string:
sarray.append(int(i))
for j in range(len(sarray)-(ss-1)):
tseq = 1
for k in range(ss):
tseq *= sarray[j+k]
if tseq > seq:
seq = tseq
p = j
return seq, p