binaryconverter.py


Below is the syntax highlighted version of binaryconverter.py from §2.3 Recursion.


#-----------------------------------------------------------------------
# binaryconverter.py
#-----------------------------------------------------------------------

import stdio
import sys

#-----------------------------------------------------------------------

# Write to standard output the binary representation of n.

def convert(n):
    if n == 0:
        return
    convert(n // 2)
    stdio.write(n % 2)

#-----------------------------------------------------------------------

# Accept positive integer command-line argument n. Write to standard
# output the binary representation of n.

def main():
    n = int(sys.argv[1])
    convert(n)
    stdio.writeln()

if __name__ == '__main__':
    main()
    
#-----------------------------------------------------------------------

# python binaryconverter.py 1
# 1

# python binaryconverter.py 2
# 10

# python binaryconverter.py 10
# 1010

# python binaryconverter.py 100
# 1100100


Copyright © 2000–2015, Robert Sedgewick, Kevin Wayne, and Robert Dondero.
Last updated: Fri Oct 20 20:45:16 EDT 2017.