2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,39 +1,27 @@
Implement a "rot-13" function (or procedure, class, subroutine,
or other "callable" object as appropriate to your programming environment).
;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 utility program (like [[:Category:Tr|tr]],
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 itsc "standard input."
Optionally wrap this function in a utility program   (like [[:Category:Tr|tr]],   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, e.g., [[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 [[wp:Spoiler (media)|spoiler]] or potentially offensive material.
(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, e.g.,   [[Perl]]   and   [[Python]]).
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   '''rot-13'''   encoding is commonly known from the early days of Usenet "Netnews" as a way of obfuscating text to prevent casual reading of   [[wp:Spoiler (media)|spoiler]]   or potentially offensive material.
The definition of the rot-13 function is to simply replace every letter
of the ASCII alphabet with the letter which is "rotated" 13 characters
"around" the 26 letter alphabet from its normal cardinal position
(wrapping around from "z" to "a" as necessary).
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.
Thus the letters "abc" become "nop" and so on.
Technically rot-13 is a "monoalphabetic substitution cipher"
with a trivial "key".
The definition of the rot-13 function is to simply replace every letter of the ASCII alphabet with the letter which is "rotated" 13 characters "around" the 26 letter alphabet from its normal cardinal position   (wrapping around from   '''z''''   to   '''a'''   as necessary).
A proper implementation should work on upper and lower case letters,
preserve case, and pass all non-alphabetic characters
Thus the letters   '''abc'''   become   '''nop'''   and so on.
Technically '''rot-13''' is a   "mono-alphabetic substitution cipher"   with 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.
;See also
* [[Caesar cipher]]
* [[Substitution Cipher]]
* [[Vigenère Cipher/Cryptanalysis]]
<br>
;Related tasks:
* &nbsp; [[Caesar cipher]]
* &nbsp; [[Substitution Cipher]]
* &nbsp; [[Vigenère Cipher/Cryptanalysis]]
<br><br>

View file

@ -0,0 +1,61 @@
-- rot13 :: String -> String
on rot13(str)
script rt13
on lambda(x)
if (x "a" and x "m") or (x "A" and x "M") then
character id ((id of x) + 13)
else if (x "n" and x "z") or (x "N" and x "Z") then
character id ((id of x) - 13)
else
x
end if
end lambda
end script
intercalate("", map(rt13, characters of str))
end rot13
-- TEST
on run
rot13("nowhere ABJURER")
--> "abjurer NOWHERE"
end run
-- GENERIC FUNCTIONS
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property lambda : f
end script
end if
end mReturn

View file

@ -0,0 +1,19 @@
import java.io.*
fun String.rot13() = map {
when {
it.isUpperCase() -> { val x = it + 13; if (x > 'Z') x - 26 else x }
it.isLowerCase() -> { val x = it + 13; if (x > 'z') x - 26 else x }
else -> it
} }.toCharArray()
fun InputStreamReader.println() =
try { BufferedReader(this).forEachLine { println(it.rot13()) } }
catch (e: IOException) { e.printStackTrace() }
fun main(args: Array<String>) {
if (args.any())
args.forEach { FileReader(it).println() }
else
InputStreamReader(System.`in`).println()
}

View file

@ -0,0 +1,3 @@
function rot13(s)
return (s:gsub("%a", function(c) c=c:byte() return string.char(c+(c%32<14 and 13 or -13)) end))
end

View file

@ -1,4 +1 @@
sub rot13 { $^s.trans: 'a..mn..z' => 'n..za..m', :ii }
multi MAIN () { print rot13 slurp }
multi MAIN (*@files) { print rot13 [~] map &slurp, @files }
.=trans: 'a..mn..z' => 'n..za..m', :ii

View file

@ -1,15 +1,14 @@
/*REXX program encodes several example text strings with the ROT-13 algorithm.*/
/*REXX program encodes several example text strings using the ROT-13 algorithm. */
@simple = 'simple text ='
@rot_13 = 'rot-13 text ='
$= 'foo' ; say @simple $; say @rot_13 rot13($); say
$= 'bar' ; say @simple $; say @rot_13 rot13($); say
$= "Noyr jnf V, 'rer V fnj Ryon." ; say @simple $; say @rot_13 rot13($); say
$= 'abc? ABC!' ; say @simple $; say @rot_13 rot13($); say
$= 'abjurer NOWHERE' ; say @simple $; say @rot_13 rot13($); say
$= 'foo' ; say @simple $; say @rot_13 rot13($); say
$= 'bar' ; say @simple $; say @rot_13 rot13($); say
$= "Noyr jnf V, 'rer V fnj Ryon." ; say @simple $; say @rot_13 rot13($); say
$= 'abc? ABC!' ; say @simple $; say @rot_13 rot13($); say
$= 'abjurer NOWHERE' ; say @simple $; say @rot_13 rot13($); say
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
rot13: return translate(arg(1), ,
'abcdefghijklmABCDEFGHIJKLMnopqrstuvwxyzNOPQRSTUVWXYZ', ,
'nopqrstuvwxyzNOPQRSTUVWXYZabcdefghijklmABCDEFGHIJKLM')
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
rot13: return translate( arg(1), 'abcdefghijklmABCDEFGHIJKLMnopqrstuvwxyzNOPQRSTUVWXYZ',,
"nopqrstuvwxyzNOPQRSTUVWXYZabcdefghijklmABCDEFGHIJKLM")

View file

@ -0,0 +1,23 @@
variable old = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
variable new = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm";
define rot13(s) {
s = strtrans(s, old, new);
return s;
}
define rot13_stream(s) {
variable ln;
while (-1 != fgets(&ln, s))
fputs(rot13(ln), stdout);
}
if (__argc > 1) {
variable arg, fp;
foreach arg (__argv[[1:]]) {
fp = fopen(arg, "r");
rot13_stream(fp);
}
}
else
rot13_stream(stdin);

View file

@ -1,9 +1,8 @@
@(do
(defun rot13 (ch)
(cond
((<= #\A (chr-toupper ch) #\M) (+ ch 13))
((<= #\N (chr-toupper ch) #\Z) (- ch 13))
(t ch)))
(defun rot13 (ch)
(cond
((<= #\A ch #\Z) (wrap #\A #\Z (+ ch 13)))
((<= #\a ch #\z) (wrap #\a #\z (+ ch 13)))
(t ch)))
(each ((l (gun (get-line nil))))
(put-line [mapcar rot13 l])))
(whilet ((ch (get-char)))
(put-char (rot13 ch)))