An Array of Sequences(2)

1. Managing Ordered Sequences with bisect

The bisect module offers two main functions --- bisect and insort --- that use the binary serach algorithm to quickly find and insert items in any sorted sequence.

Example 2-17. bisect finds insertion points for items in a sorted sequence.

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 the sys.argv [-1] == " left " : 
        bisect_fn = bisect.bisect_left
     the else : 
        bisect_fn = bisect.bisect 

Print ( ' the DEMO: ' ., Bisect_fn the __name__ )
 Print ( ' of haystack -> ' , '  ' . join ( " % 2D " % n- for n- in HAYSTACK))      # Note here join usage: join list are not used [] symbol 
Demo (bisect_fn) 


"" "
# Output: script without parameters 
NEO: Array-of-CH2-Sequences zhenxink $ to python3 search_with_bisect.py 
the DEMO: bisect 
of haystack ->. 1. 4. 5. 6. 8 12 is 20 is 21 is 23 is 23 is 26 is 15 29 30 
31 is 14 @ | | | | | | | | | | | | | | 31 
30 @ 14 | | | | | | | | | | | | | | 30 
29 @ 13 | | | | | | | | | | | | | 29 
23 @ 11 | | | | | | | | | | | 23 is 
22 is @. 9 | | | | | | | | | 22 is 
10 @. 5 | | | | | 10 
 . 8 @. 5 | | | | |. 8 
 . 5 @. 3 | | |. 5 
 2 @ . 1 | 2 
 . 1. 1 @ |. 1 
 0 0 0 @ 
 
# output: parameter script plus left  
NEO: ch2-Array-of-Sequences zhenxink $ python3 search_with_bisect.py left
the DEMO: bisect_left 
of haystack ->. 1. 4. 5. 6. 8 12 is 20 is 21 is 23 is 23 is 26 is 15 29 30
31 @ 14      |  |  |  |  |  |  |  |  |  |  |  |  |  |31
30 @ 13      |  |  |  |  |  |  |  |  |  |  |  |  |30
29 @ 12      |  |  |  |  |  |  |  |  |  |  |  |29
23 @  9      |  |  |  |  |  |  |  |  |23
22 @  9      |  |  |  |  |  |  |  |  |22
10 @  5      |  |  |  |  |10
 8 @  4      |  |  |  |8 
 5 @  2      |  |5 
 2 @  1      |2 
 1 @  0    1 
 0 @  0    0 
"""

Example 2-18: Given a test score, grades returns the corresponding letter grade (a bisect the application example).

import bisect


def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):
    i = bisect.bisect(breakpoints, score)
    return grades[i]


letter_grade = [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]

print(letter_grade)

# 输出:
# ['F', 'A', 'C', 'C', 'B', 'A', 'A']

 

insort(seq, item) inserts items into seq so as to keep seq in ascending order.

Example 2-19: Insort keeps a sorted sequence always sorted.

import bisect
import random

SIZE = 7
random.seed(1729)

my_list = []
for i in range(SIZE):
    new_item = random.randrange(SIZE * 2)
    bisect.insort(my_list, new_item)
    print("%2d ->" % new_item, my_list)

'''
# 输出:
10 -> [10]
 0 -> [0, 10]
 6 -> [0, 6, 10]
 8 -> [0, 6, 8, 10]
 7 -> [0, 6, 7, 8, 10]
 2 -> [0, 2, 6, 7, 8, 10]
10 -> [0, 2, 6, 7, 8, 10, 10]
'''

# bisect.insort keeps a sorted sequence always sorted.

bisect module parameters link: https://www.cnblogs.com/skydesign/archive/2011/09/02/2163592.html 

 

 

 

 

 

 

end

Guess you like

Origin www.cnblogs.com/neozheng/p/12151052.html