kamasutra.py


Below is the syntax highlighted version of kamasutra.py from §3.1 Using Data Types.


#-----------------------------------------------------------------------
# kamasutra.py
#-----------------------------------------------------------------------

import stdio
import sys

# Accept two strings as command-line arguments to be used as key
# strings in a Kama Sutra cipher. Then repeatedly read a line from 
# standard input, encode it using the cipher, and write the result
# to standard output.
    
top = sys.argv[1]
bot = sys.argv[2]

while stdio.hasNextLine():
    line = stdio.readLine()
    for c in line:
        if top.find(c) >= 0:
            stdio.write(bot[top.find(c)])
        elif bot.find(c) >= 0:
            stdio.write(top[bot.find(c)])
        else:
            stdio.write(c)
    stdio.writeln()
    
#-----------------------------------------------------------------------
   
# python kamasutra.py THEQUICKBROWNFOX FXJMPSVLAZYDG
# MEET AT ELEVEN
# QJJF BF JKJCJG
# Ctrl-d


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