import bisectimport randomimport sysHAYSTACK = [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) # <1>offset = position * ' |' # <2>print(ROW_FMT.format(needle, position, offset)) # <3>def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):i = bisect.bisect(breakpoints, score)return grades[i]if __name__ == '__main__':if sys.argv[-1] == 'left': # <4>bisect_fn = bisect.bisect_leftelse:bisect_fn = bisect.bisectprint('DEMO: ', bisect_fn.__name__) # <5>print('haystack ->', ' '.join('%2d' % n for n in HAYSTACK))demo(bisect_fn)print([grade(score) for score in [33, 99, 77, 70, 89, 90, 100]])SIZE = 7random.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)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。