langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
51
Task/Caesar-cipher/OCaml/caesar-cipher-1.ocaml
Normal file
51
Task/Caesar-cipher/OCaml/caesar-cipher-1.ocaml
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
let islower c =
|
||||
c >= 'a' && c <= 'z'
|
||||
|
||||
let isupper c =
|
||||
c >= 'A' && c <= 'Z'
|
||||
|
||||
let rot x str =
|
||||
let upchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
and lowchars = "abcdefghijklmnopqrstuvwxyz" in
|
||||
let rec decal x =
|
||||
if x < 0 then decal (x + 26) else x
|
||||
in
|
||||
let x = (decal x) mod 26 in
|
||||
let decal_up = x - (int_of_char 'A')
|
||||
and decal_low = x - (int_of_char 'a') in
|
||||
let len = String.length str in
|
||||
let res = String.create len in
|
||||
for i = 0 to pred len do
|
||||
let c = str.[i] in
|
||||
if islower c then
|
||||
let j = ((int_of_char c) + decal_low) mod 26 in
|
||||
res.[i] <- lowchars.[j]
|
||||
else if isupper c then
|
||||
let j = ((int_of_char c) + decal_up) mod 26 in
|
||||
res.[i] <- upchars.[j]
|
||||
else
|
||||
res.[i] <- c
|
||||
done;
|
||||
(res)
|
||||
|
||||
(* or in OCaml 4.00+:
|
||||
let rot x =
|
||||
let upchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
and lowchars = "abcdefghijklmnopqrstuvwxyz" in
|
||||
let rec decal x =
|
||||
if x < 0 then decal (x + 26) else x
|
||||
in
|
||||
let x = (decal x) mod 26 in
|
||||
let decal_up = x - (int_of_char 'A')
|
||||
and decal_low = x - (int_of_char 'a') in
|
||||
String.map (fun c ->
|
||||
if islower c then
|
||||
let j = ((int_of_char c) + decal_low) mod 26 in
|
||||
lowchars.[j]
|
||||
else if isupper c then
|
||||
let j = ((int_of_char c) + decal_up) mod 26 in
|
||||
upchars.[j]
|
||||
else
|
||||
c
|
||||
)
|
||||
*)
|
||||
9
Task/Caesar-cipher/OCaml/caesar-cipher-2.ocaml
Normal file
9
Task/Caesar-cipher/OCaml/caesar-cipher-2.ocaml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
let () =
|
||||
let key = 3 in
|
||||
let orig = "The five boxing wizards jump quickly" in
|
||||
let enciphered = rot key orig in
|
||||
print_endline enciphered;
|
||||
let deciphered = rot (- key) enciphered in
|
||||
print_endline deciphered;
|
||||
Printf.printf "equal: %b\n" (orig = deciphered)
|
||||
;;
|
||||
Loading…
Add table
Add a link
Reference in a new issue