├── .gitignore ├── LICENSE ├── README.md └── placevalue_ascii.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.swp 3 | __pycache__/ 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Mouse Reeve 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Placevalue Ascii 2 | 3 | Creates an ascii representation of the binary ouput of two variable function at a certian place value, arrayed on a grid. 4 | 5 | ## What? 6 | Consider the function `f(x, y) = x + y`. 7 | Now, make a grid of the outputs of this function for incrementing integer values of `x` and `y`, like so: 8 | ``` 9 | _|_0___1___2___3__ 10 | 0| 0 1 2 3 11 | 1| 1 2 3 4 12 | 2| 2 3 4 5 13 | 3| 3 4 5 6 14 | ``` 15 | 16 | Convert each number to binary: 17 | ``` 18 | _|_0___1___2___3__ 19 | 0| 000 001 010 011 20 | 1| 001 010 011 100 21 | 2| 010 011 100 101 22 | 3| 011 100 101 110 23 | ``` 24 | 25 | Reduce the grid to only the digits at a certain place value, let's say 3: 26 | ``` 27 | _|_0___1___2___3__ 28 | 0| 0 0 0 0 29 | 1| 0 0 0 1 30 | 2| 0 0 1 1 31 | 3| 0 1 1 1 32 | ``` 33 | 34 | Now, replace the `1`s with an asterisk, `*` and the `0` with a space, ` `: 35 | ``` 36 | 37 | * 38 | ** 39 | *** 40 | ``` 41 | 42 | ## Script example 43 | This command produces a 16 by 16 ascii pattern for the function `f(x, y) = x + y` at place value. 44 | ``` bash 45 | $ python placevalue_ascii.py "x + y" 3 16 16 46 | **** 47 | **** 48 | **** 49 | **** 50 | **** 51 | **** * 52 | **** ** 53 | **** *** 54 | **** **** 55 | **** **** 56 | **** **** 57 | **** **** 58 | **** **** 59 | *** **** * 60 | ** **** ** 61 | * **** *** 62 | ``` 63 | -------------------------------------------------------------------------------- /placevalue_ascii.py: -------------------------------------------------------------------------------- 1 | ''' print out binary place value patterns ''' 2 | import argparse 3 | 4 | 5 | def placevalue_patterner(function, height, width, placevalue, offset_y=0): 6 | ''' create a visualization of a place value pattern ''' 7 | visual = [] 8 | for i in range(offset_y, offset_y + height): 9 | row = '' 10 | for j in range(width): 11 | value = function(i, j) 12 | binary = '{0:b}'.format(int(value)) 13 | if len(binary) > placevalue and binary[-1 * placevalue] == '1': 14 | row += '*' 15 | else: 16 | row += ' ' 17 | visual.append(row) 18 | return '\n'.join(visual) 19 | 20 | 21 | if __name__ == '__main__': 22 | parser = argparse.ArgumentParser() 23 | parser.add_argument('function', 24 | help='The function to plot, e.g. "x ** 2 * y ** 2"', 25 | type=lambda s: eval('lambda x, y: {}'.format(s))) 26 | parser.add_argument('placevalue', type=int) 27 | parser.add_argument('width', type=int) 28 | parser.add_argument('height', type=int) 29 | args = parser.parse_args() 30 | print(placevalue_patterner(args.function, args.height, 31 | args.width, args.placevalue)) 32 | 33 | --------------------------------------------------------------------------------