Created
August 6, 2022 13:26
-
-
Save everylittlefox/e9733fa10ec106daa404826f34cc3a68 to your computer and use it in GitHub Desktop.
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
| def is_upcase?(string) | |
| string.upcase == string | |
| end | |
| def caesar_cipher(string, key) | |
| upper = "A".ord | |
| lower = "a".ord | |
| cipher_arr = string.chars.map do |c| | |
| if "a".upto("z").include?(c.downcase) | |
| letter_case = is_upcase?(c) ? upper : lower | |
| diff = (c.ord - letter_case + key) % 26 | |
| mapped_value = letter_case + diff | |
| mapped_value.chr | |
| else | |
| c | |
| end | |
| end | |
| cipher_arr.join | |
| end | |
| p caesar_cipher("What a string!", 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment