-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.py
37 lines (31 loc) · 1010 Bytes
/
Utilities.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
# http://stackoverflow.com/a/1520716
from __future__ import print_function
import itertools
import operator
import sys
def most_common(L):
# get an iterable of (item, iterable) pairs
SL = sorted((x, i) for i, x in enumerate(L))
# print 'SL:', SL
groups = itertools.groupby(SL, key=operator.itemgetter(0))
# auxiliary function to get "quality" for an item
def _auxfun(g):
item, iterable = g
count = 0
min_index = len(L)
for _, where in iterable:
count += 1
min_index = min(min_index, where)
# print 'item %r, count %r, minind %r' % (item, count, min_index)
return count, -min_index
# pick the highest-count/earliest item
return max(groups, key=_auxfun)[0]
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def percentEncode(startingText):
encodedText = startingText
et = encodedText
et = et.replace(' ', '%20')
et = et.replace(',', '%2C')
encodedText = et
return encodedText