dragon2.py


Below is the syntax highlighted version of dragon2.py from §1.3 Conditionals and Loops.


#-----------------------------------------------------------------------
# dragon2.py
#-----------------------------------------------------------------------

import stdio
import sys

# Accept n as a command-line argument. Write to standard output
# the instructions for drawing a dragon curve of order n.

n = int(sys.argv[1])

dragon = 'F'
nogard = 'F'

for i in range(1, n+1):
    temp = dragon                    # save away copy of dragon
    dragon = str(dragon) + 'L' + str(nogard)
    nogard = temp + 'R' + nogard     # use old value of dragon

stdio.writeln(dragon)

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

# python dragon2.py 1
# FLF

# python dragon2.py 2
# FLFLFRF

# python dragon2.py 3
# FLFLFRFLFLFRFRF

# python dragon2.py 4
# FLFLFRFLFLFRFRFLFLFLFRFRFLFRFRF

# python dragon2.py 5
# FLFLFRFLFLFRFRFLFLFLFRFRFLFRFRFLFLFLFRFLFLFRFRFRFLFLFRFRFLFRFRF


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