Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4202342

Browse files
committed
add array-seq
1 parent f41f5af commit 4202342

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

‎02-array-seq/bisect_demo.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import bisect
2+
import sys
3+
4+
5+
HAYSTACK = [1, 4, 5, 6, 8, 12, 15, 20, 21, 23, 23, 26, 29, 30]
6+
NEEDLES = [0, 1, 2, 5, 8, 10, 22, 23, 29, 30, 31]
7+
8+
ROW_FMT = '{0:2d} @ {1:2d} {2}{0:<2d}'
9+
10+
11+
def demo(bisect_fn):
12+
for needle in reversed(NEEDLES):
13+
position = bisect_fn(HAYSTACK, needle)
14+
offset = position * ' |'
15+
print(ROW_FMT.format(needle, position, offset))
16+
17+
18+
if __name__ == '__main__':
19+
20+
if sys.argv[-1] == 'left':
21+
bisect_fn = bisect.bisect_left
22+
else:
23+
bisect_fn = bisect.bisect
24+
25+
print('DEMO:', bisect_fn.__name__)
26+
print('haystack ->', ' '.join('%2d' % n for n in HAYSTACK))
27+
demo(bisect_fn)

‎02-array-seq/bisect_insort.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import bisect
2+
import random
3+
4+
SIZE = 7
5+
6+
random.seed(1729)
7+
8+
my_list = []
9+
for i in range(SIZE):
10+
new_item = random.randrange(SIZE * 2)
11+
bisect.insort(my_list, new_item)
12+
print('%2d ->' % new_item, my_list)

‎02-array-seq/listcomp_speed.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import timeit
2+
3+
TIMES = 10000
4+
5+
SETUP = """
6+
symbols = '$¢£\€¤'
7+
def non_ascii(c):
8+
return c > 127
9+
"""
10+
11+
12+
def clock(label, cmd):
13+
res = timeit.repeat(cmd, setup=SETUP, number=TIMES)
14+
print(label, *('{:.3f}'.format(x) for x in res))
15+
16+
17+
clock('listcomp :', '[ord(s) for s in symbols if ord(s) > 127]')
18+
clock('listcomp + func :', '[ord(s) for s in symbols if non_ascii(ord(s))]')
19+
clock('filter + lambda :', 'list(filter(lambda c: c > 127, map(ord, symbols)))')
20+
clock('filter + func :', 'list(filter(non_ascii, map(ord, symbols)))')

‎02-array-seq/metro_lat_long.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
metro_areas = [
2+
('Tokyo', 'JP', 36.933, (35.689722, 139.691667)),
3+
('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)),
4+
('Mexico City', 'MX', 20.142, (19.433333, -99.133333)),
5+
('New York-Newark', 'US', 20.104, (40.808611, -74.020386)),
6+
('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833)),
7+
]
8+
9+
print('{:15} | {:^9} | {:^9}'.format('', 'lat.', 'long.'))
10+
fmt = '{:15} | {:9.4f} | {:9.4f}'
11+
for name, cc, pop, (latitude, longitude) in metro_areas:
12+
if longitude <= 0:
13+
print(fmt.format(name, latitude, longitude))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /