Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.

Commit f317cc6

Browse files
authored
Merge pull request #2 from rmrt1n/cryptography
added cryptography directory, caesar-cypher and factorial algorithms
2 parents 7d3076f + 637f3dd commit f317cc6

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#lang racket
2+
3+
(provide caesar-cypher)
4+
5+
;; Turn plain text into cypher text with caesar cypher algorithm.
6+
;; Numbers, symbols and whitespace are not changed.
7+
;; args:
8+
;; str: string to encode
9+
;; shift: optional shift length, default is 3
10+
(define (caesar-cypher str [shift 3])
11+
(let* ([shift-uppercase
12+
(lambda (c)
13+
(integer->char
14+
(+ 65 (remainder (+ shift (char->integer c) -65) 26))))]
15+
[shift-lowercase
16+
(lambda (c)
17+
(integer->char
18+
(+ 97 (remainder (+ shift (char->integer c) -97) 26))))]
19+
[shift
20+
(lambda (c)
21+
(cond [(and (char-alphabetic? c)
22+
(char-upper-case? c))
23+
(shift-uppercase c)]
24+
[(and (char-alphabetic? c)
25+
(char-lower-case? c))
26+
(shift-lowercase c)]
27+
[else c]))]
28+
[char-list (string->list str)])
29+
(let loop ([acc '()]
30+
[ls char-list])
31+
(if (null? ls)
32+
(list->string (reverse acc))
33+
(loop (cons (shift (car ls)) acc) (cdr ls))))))
34+

algorithms/math/factorial.rkt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#lang racket
2+
3+
(provide factorial)
4+
5+
(define (factorial n)
6+
(let loop ([acc 1]
7+
[n n])
8+
(if (zero? n)
9+
acc
10+
(loop (* acc n) (sub1 n)))))
11+

0 commit comments

Comments
 (0)