Rot-13: Difference between revisions

From Rosetta Code
Content added Content deleted
(New page: {{task}} Implement a "rot-13" function (or procedure, class, subroutine, or other "callable" object as appropriate to your programming environment). Optionally wrap this function in a ut...)
 
(Adding Python and UNIX Shell examples)
Line 6: Line 6:


The definition of the rot-13 function is to simply replace every letter of the alphabet with the letter which is "rotated" 13 characters "around" the alphabet from it's normal cardinal position (wrapping around from "z" to "a" as necessary). Thus the letters "abc" become "nop" and so on. Technically rot-13 is a "monoalphabetic substitution cipher" will a trivial "key". A proper implementation should work on upper and lower case letters, preserve case, and pass all non-alphabetic characters in the input stream through without alteration.
The definition of the rot-13 function is to simply replace every letter of the alphabet with the letter which is "rotated" 13 characters "around" the alphabet from it's normal cardinal position (wrapping around from "z" to "a" as necessary). Thus the letters "abc" become "nop" and so on. Technically rot-13 is a "monoalphabetic substitution cipher" will a trivial "key". A proper implementation should work on upper and lower case letters, preserve case, and pass all non-alphabetic characters in the input stream through without alteration.


=={{header|Python}}==

#!/usr/bin/env python
import string
def rot13(s):
"""Implement the rot-13 encoding function: "rotate" each letter by the
letter that's 13 steps from it (wrapping from z to a)
"""
return string.translate( s,
string.maketrans(
string.uppercase + string.lowercase,
string.uppercase[13:] + string.uppercase[:13] +
string.lowercase[13:] + string.lowercase[:13]
)
)
if __name__ == "__main__":
"""Peform line-by-line rot-13 encoding on any files listed on our
command line or act as a standard UNIX filter (if no arguments
specified).
"""
import fileinput
for line in fileinput.input():
print rot13(line), # (Note the trailing comma; avoid double-spacing our output)!

The ''string.translate()'' and ''string.maketrans()'' functions make the function's definition almost trivial. It's a one-line function with some line wrapping for legibility. The ''fileinput'' module similarly makes the wrapper functionality trivial to implement. (This implementation is about seven logical lines long).

=={{header|UNIX Shell}}==
===[[Bourne Shell]]===

#!/bin/sh
function rot13 () {
tr '[a-m][n-z][A-M][N-Z]' '[n-z][a-m][N-Z][A-M]'
}
[ "$#" -lt 1 ] && {
rot13
} || for eachFile in "$@"; do
cat "$eachFile" | rot13
done

UNIX shell assumes availability of the standard UNIX utility commands (in the "coreutils" package on Linux systems, for example); thus the ''tr'' (translate) command is trivially provided with the proper arguments to perform the rotations. This example shows proper quoting around "$@" (magical argument list) and "$eachFile" such that this script work properly even if some of the files named on the command line contain embedded spaces or other such characters.

Revision as of 21:12, 20 November 2007

Task
Rot-13
You are encouraged to solve this task according to the task description, using any language you may know.

Implement a "rot-13" function (or procedure, class, subroutine, or other "callable" object as appropriate to your programming environment). Optionally wrap this function in a utility program which acts like a common Unix utility, performing a line-by-line rot-13 encoding of every line of input contained in each file listed on its command line, or (if no filenames are passed thereon) acting as a filter on its "standard input." (A number of UNIX scripting languages and utilities, such as awk and sed either default to processing files in this way or have command line switches or modules to easily implement these wrapper semantics, i.e. Perl and Python).

The "rot-13" encoding is commonly known from the early days of Usenet "Netnews" as a way of obfuscating text to prevent casual reading of spoilers or other potentially offensive material. Many news reader and mail user agent programs have built-in "rot-13" encoder/decoders or have the ability to feed a message through any external utility script for performing this (or other) actions.

The definition of the rot-13 function is to simply replace every letter of the alphabet with the letter which is "rotated" 13 characters "around" the alphabet from it's normal cardinal position (wrapping around from "z" to "a" as necessary). Thus the letters "abc" become "nop" and so on. Technically rot-13 is a "monoalphabetic substitution cipher" will a trivial "key". A proper implementation should work on upper and lower case letters, preserve case, and pass all non-alphabetic characters in the input stream through without alteration.


Python

#!/usr/bin/env python
import string
def rot13(s):
   """Implement the rot-13 encoding function: "rotate" each letter by the
      letter that's 13 steps from it (wrapping from z to a)
   """
   return string.translate( s,
       string.maketrans(
           string.uppercase + string.lowercase,
           string.uppercase[13:] + string.uppercase[:13] +
           string.lowercase[13:] + string.lowercase[:13]
           )
       )
if __name__ == "__main__":
   """Peform line-by-line rot-13 encoding on any files listed on our
      command line or act as a standard UNIX filter (if no arguments
      specified).
   """
   import fileinput
   for line in fileinput.input():
      print rot13(line),  # (Note the trailing comma; avoid double-spacing our output)!

The string.translate() and string.maketrans() functions make the function's definition almost trivial. It's a one-line function with some line wrapping for legibility. The fileinput module similarly makes the wrapper functionality trivial to implement. (This implementation is about seven logical lines long).

UNIX Shell

Bourne Shell

  1. !/bin/sh

function rot13 () {

 tr '[a-m][n-z][A-M][N-Z]' '[n-z][a-m][N-Z][A-M]'
 }

[ "$#" -lt 1 ] && {

 rot13
 } || for eachFile in "$@"; do
    cat "$eachFile" | rot13
    done

UNIX shell assumes availability of the standard UNIX utility commands (in the "coreutils" package on Linux systems, for example); thus the tr (translate) command is trivially provided with the proper arguments to perform the rotations. This example shows proper quoting around "$@" (magical argument list) and "$eachFile" such that this script work properly even if some of the files named on the command line contain embedded spaces or other such characters.