-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplacevalue_ascii.py
32 lines (27 loc) · 1.11 KB
/
placevalue_ascii.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
''' print out binary place value patterns '''
import argparse
def placevalue_patterner(function, height, width, placevalue, offset_y=0):
''' create a visualization of a place value pattern '''
visual = []
for i in range(offset_y, offset_y + height):
row = ''
for j in range(width):
value = function(i, j)
binary = '{0:b}'.format(int(value))
if len(binary) > placevalue and binary[-1 * placevalue] == '1':
row += '*'
else:
row += ' '
visual.append(row)
return '\n'.join(visual)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('function',
help='The function to plot, e.g. "x ** 2 * y ** 2"',
type=lambda s: eval('lambda x, y: {}'.format(s)))
parser.add_argument('placevalue', type=int)
parser.add_argument('width', type=int)
parser.add_argument('height', type=int)
args = parser.parse_args()
print(placevalue_patterner(args.function, args.height,
args.width, args.placevalue))