Last active
July 2, 2020 03:05
-
-
Save Karrq/24617dd24ff63b047211444b49099956 to your computer and use it in GitHub Desktop.
Rot13 Decode/Encode implementation for July 2020 Competition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #lang racket/base | |
| #| | |
| Date: 02/07/2020 | |
| Author: Francesco "Karrq" Dainese | |
| github: @Karrq | |
| This is a rot13 cipher implementation for quickscript. | |
| License: MIT/Apache 2 | |
| Made for July 2020 Quickscript Competition | |
| |# | |
| (require quickscript) | |
| ;rot13 alphabet | |
| (define alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ") | |
| ;find the index in the alphabet of a given character | |
| (define (find-pos char) | |
| (for/first ([c (in-string alphabet)] | |
| [i (in-range (string-length alphabet))] | |
| #:when (eq? (char-upcase char) c)) | |
| i)) | |
| ;retrieve a character from the alphabet given the index | |
| ;and if the character was uppercase | |
| (define (get-letter idx upcase) | |
| (define c (string-ref alphabet idx)) | |
| (if upcase | |
| c | |
| (char-downcase c))) | |
| ;apply rot13 to a single character | |
| (define (rot13-impl c) | |
| (define pos (find-pos c)) | |
| (if pos | |
| (get-letter (remainder (+ 13 pos) 26) (char-upper-case? c)) | |
| c)) | |
| ;apply rot13 to selection | |
| (define-script rot13 | |
| #:label "Rot13 Encode/Decode" | |
| #:help-string "Encodes or decodes a string using rot13 cipher" | |
| (λ (selection) | |
| (list->string (map rot13-impl (string->list selection))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment