Skip to content

Instantly share code, notes, and snippets.

@matfournier
Created June 18, 2017 06:31
Show Gist options
  • Save matfournier/0a243576de72cd23514f2bce6a201c61 to your computer and use it in GitHub Desktop.
Save matfournier/0a243576de72cd23514f2bce6a201c61 to your computer and use it in GitHub Desktop.
''' print out binary place value patterns '''
''' glitchy infinite scroll version '''
''' hit any key to exit '''
''' see https://github.com/mouse-reeve/placevalue_ascii '''
import math
import argparse
import time
import sys
import os
import select
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('delay', type=float)
args = parser.parse_args()
i = 0
while True:
os.system('cls' if os.name == 'nt' else 'clear')
if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
line = input()
break
print(placevalue_patterner(args.function, i, args.width,
args.placevalue))
time.sleep(args.delay)
i += 1
@matfournier
Copy link
Author

matfournier commented Jun 18, 2017

python placevalue_inf.py "x2 - y2" 6 100 0.25

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment