-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sorting.py
71 lines (42 loc) · 1.52 KB
/
Sorting.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
# list.sort sorted()
fruits = 'grape raspberry apple banana'.split()
sorted(fruits)
print(fruits) # unchanged
sorted(fruits, reverse=True)
sorted(fruits, key=len)
sorted(fruits, key=len, reverse=True)
print(fruits)
fruits.sort()
print(fruits) # changed
# Managing ordered sequences with bisect
import bisect
import sys
HAYSTACK = [1, 4, 5, 6, 8, 12, 15, 20, 21, 23, 23, 26, 29, 30]
NEEDLES = [0, 1, 2, 5, 8, 10, 22, 23, 29, 30, 31]
ROW_FMT = '{0:2d} @ {1:2d} {2}{0:<2d}'
def demo(bisect_fn):
for needle in reversed(NEEDLES):
position = bisect_fn(HAYSTACK, needle)
offset = position * ' |'
print(ROW_FMT.format(needle, position, offset))
if __name__ == '__main__':
if sys.argv[-1] == 'left':
bisect_fn = bisect.bisect_left
else:
bisect_fn = bisect.bisect
print('DEMO:', bisect_fn.__name__)
print('haystack ->', ' '.join('%2d' % n for n in HAYSTACK))
demo(bisect_fn)
# given a test score, grade() returns the corresponding letter grade
def grade(score, boundries=[60, 70, 80, 90], grades='FDCBA'):
position = bisect.bisect(boundries, score)
grade = grades[position]
print(grade)
def grade_(score, boundries=[60, 70, 80, 90], grades='FDCBA'):
i = bisect.bisect(boundries, score)
return(grades[i])
# this function is from 'bisect module documentation', look into
# docs for faster replacements for the index method
# INSERTING with bisect.insort
# Sorting is expensive, so once sorted, it's good to keep things
# that way when adding items