Last active
November 18, 2017 18:57
-
-
Save salmanx/be1334e0a385badb096e0267d8271f4c to your computer and use it in GitHub Desktop.
Simple ruby program for caesar cipher sollution
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
# Julius Caesar protected his confidential information by encrypting it in a cipher. Caesar’s cipher rotated every letter | |
# in a string by a fixed number, K , making it unreadable by his enemies. Given a string, S , and a number, K , | |
# encrypt S and print the resulting string. | |
# Note: The cipher only encrypts letters; symbols, such as − , remain unencrypted. | |
# Input Format | |
# The first line contains an integer, N , which is the length of the unencrypted string. | |
# The second line contains the unencrypted string, S . | |
# The third line contains the integer encryption key, K , which is the number of letters to rotate. | |
# Constraints | |
# 1 ≤ N ≤ 100 | |
# 1 ≤ K ≤ 100 | |
# S is valid ASCII string and doesn’t contain any spaces. | |
# Output Format | |
# For each test case, print the encoded string. | |
# Sample Input | |
# 11 | |
# middle-Outz | |
# 2 | |
# Sample Output | |
# okffng-Qwvb | |
# Explanation | |
# Each unencrypted letter is replaced with the letter occurring K spaces a er it when listed alphabetically. Think | |
# of the alphabet as being both case-sensitive and circular; if K rotates past the end of the alphabet, it loops | |
# back to the beginning (i.e.: the letter a er z is a , and the letter a er Z is A ). | |
# Selected Examples: | |
# m (ASCII 109) becomes o (ASCII 111). | |
# i (ASCII 105) becomes k (ASCII 107). | |
# − remains the same, as symbols are not encoded. | |
# O (ASCII 79) becomes Q (ASCII 81). | |
# z (ASCII 122) becomes b (ASCII 98); because z is the last letter of the alphabet, a (ASCII 97) is the next letter a er | |
# it in lower-case rotation. | |
######### NB. This is very generic sollution. I just solved this as fun. You can do this more better way ################ | |
def convert_alphabet_to_asci(alphabet, rotation_number)rotation_number | |
asci = alphabet.chars.map { |c| c.ord + rotation_number } | |
end | |
def convert_asci_to_alphabet(asci, rotation_number) | |
alphabet = [] | |
capital_alphabet = ('A'..'Z').to_a | |
small_alphabet = ('a'..'z').to_a | |
asci.each do |c| | |
asci_c = (c - rotation_number).chr | |
if capital_alphabet.include?(asci_c) | |
if c > "Z".ord | |
reverse_value = (c - "Z".ord) | |
c = ("A".ord - 1) + reverse_value | |
end | |
alphabet << c | |
elsif small_alphabet.include?(asci_c) | |
if c > "z".ord | |
reverse_value = (c - "z".ord) | |
c = ("a".ord - 1) + reverse_value | |
end | |
alphabet << c | |
else | |
alphabet << c | |
end | |
end | |
return alphabet | |
end | |
def init | |
puts "Enter the number of message length : " | |
msg_length = gets.chomp.to_i | |
puts "Give your encrypted message : " | |
encrypted_msg = gets.strip | |
if msg_length < encrypted_msg.length | |
puts "Your encrypted message length is greater than your provided message length." | |
exit | |
end | |
puts "give yor rotation number : " | |
rotation_number = gets.chomp.to_i | |
if encrypted_msg.include?('-') | |
msg = encrypted_msg.split('-') | |
first_half_asci = convert_alphabet_to_asci(msg[0], rotation_number) | |
second_half_asci = convert_alphabet_to_asci(msg[1], rotation_number) | |
first_half_alphabet = convert_asci_to_alphabet(first_half_asci, rotation_number) | |
second_half_alphabet = convert_asci_to_alphabet(second_half_asci, rotation_number) | |
first_half_unencrypted_msg = first_half_alphabet.map { |c| c.chr }.join | |
second_half_unencrypted_msg = second_half_alphabet.map { |c| c.chr }.join | |
unencrypted_msg = "#{first_half_unencrypted_msg}-#{second_half_unencrypted_msg}" | |
else | |
asci = convert_alphabet_to_asci(encrypted_msg, rotation_number) | |
alphabet = convert_asci_to_alphabet(asci, rotation_number) | |
unencrypted_msg = alphabet.map { |c| c.chr }.join | |
end | |
puts unencrypted_msg | |
end | |
init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment