update from atlas
This commit is contained in:
27
02-array-seq/bisect_demo.py
Normal file
27
02-array-seq/bisect_demo.py
Normal file
@@ -0,0 +1,27 @@
|
||||
# BEGIN BISECT_DEMO
|
||||
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) # <1>
|
||||
offset = position * ' |' # <2>
|
||||
print(ROW_FMT.format(needle, position, offset)) # <3>
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
if sys.argv[-1] == 'left': # <4>
|
||||
bisect_fn = bisect.bisect_left
|
||||
else:
|
||||
bisect_fn = bisect.bisect
|
||||
|
||||
print('DEMO:', bisect_fn.__name__) # <5>
|
||||
print('haystack ->', ' '.join('%2d' % n for n in HAYSTACK))
|
||||
demo(bisect_fn)
|
||||
|
||||
# END BISECT_DEMO
|
||||
12
02-array-seq/bisect_insort.py
Normal file
12
02-array-seq/bisect_insort.py
Normal file
@@ -0,0 +1,12 @@
|
||||
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)
|
||||
13
02-array-seq/metro_lat_long.py
Normal file
13
02-array-seq/metro_lat_long.py
Normal file
@@ -0,0 +1,13 @@
|
||||
metro_areas = [
|
||||
('Tokyo', 'JP', 36.933, (35.689722, 139.691667)), # <1>
|
||||
('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)),
|
||||
('Mexico City', 'MX', 20.142, (19.433333, -99.133333)),
|
||||
('New York-Newark', 'US', 20.104, (40.808611, -74.020386)),
|
||||
('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833)),
|
||||
]
|
||||
|
||||
print('{:15} | {:^9} | {:^9}'.format('', 'lat.', 'long.'))
|
||||
fmt = '{:15} | {:9.4f} | {:9.4f}'
|
||||
for name, cc, pop, (latitude, longitude) in metro_areas: # <2>
|
||||
if longitude <= 0: # <3>
|
||||
print(fmt.format(name, latitude, longitude))
|
||||
Reference in New Issue
Block a user