Last active
May 3, 2019 23:03
-
-
Save ryanveroniwooff/33ff37735def7ad15830e65c8fcf6e85 to your computer and use it in GitHub Desktop.
A Simple Coding Project in Ruby
This file contains 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
# Your challenge should you choose to accept it: | |
#**************** | |
#* Preface ** | |
#************************************************************************************************** | |
# Build a cipher to decode the message: "J\\wgdm\x1Ehq\x1D>mlhne-\x1EP`nsdm`dp\x1D,1sg!\x1E9'" ** | |
# This message is passed as an argument(input) to the encrypt method along with a key ** | |
# pp is better for testing than puts xD ** ** | |
# program inherently outputs the return of the encrypt funcion by calling it in a pp statement ;)** | |
#************************************************************************************************** | |
#**************************** | |
#* Instruction ** | |
#**************************** | |
# The program should split the string(str) into an array(arr) | |
# The program should then map each value in the array to its associated ASCII value | |
# Now the program will rotate those ASCII values based on a KEY | |
# This key can be anything, so your code has to account for that | |
# To accomplish this you will need to put the number into an array with each spot only holding one digit: 1234 => [1, 2, 3, 4] | |
# Then make the key the same size as the phrase by deleting extra values or repeating values to make more(phrase "hello" has 5 letter, the key 123 would become [1,2,3,1,2]) | |
# Then with the key setup you can start the encryption(encoding really, but more fun to say encryption) | |
# Now lets consider the earlier example for this next step: | |
# - we're going to be using the key to change the strings ASCII value | |
# - and then we'll turn that ASCII back into letters and output it as a string | |
# So if we have the encrypt method getting called as such: encrypt("hello", 123) | |
# hello becomes===========> [h, e, l, l, o] (already done) | |
# then turns to ASCII=====> [104, 101, 108, 108, 111] (already done) | |
# the key becomes=========> [1, 2, 3, 1, 2] (already done) | |
# the values are combined=> [105, 103, 111, 109, 113] (not done) | |
# converted back to letters [i, g, o, m, q] (not done) | |
# and then back to string===> "igomq" (not done) | |
#************************************************************************************************** | |
require 'pp' | |
def encrypt(str, key) | |
# split string and store it in an array, hint: array equals the string split at each letter | |
# ---your code here--- | |
str | |
# map the array values to their ASCII equivalents, hint: use the map fucntion and find out how to convert to ASCII in Ruby | |
# ---your code here--- | |
# put the key into an array of integers so that it can be used in the encryption, hint: split the number and make it as long as the phrase(recursion) | |
# ---your code here--- | |
# create a new array of ASCII values made up of the strings ASCII values added to the KEY's digits | |
# ---your code here--- | |
# convert the new ASCII values back to characters and turn the array into a string, return the string and you're done!....almost >XD | |
# ---your code here--- | |
end | |
# BONUS CHALLENGE: Create a decryption method to turn your jumbled string back into normal text!(hint: it's easier than you think!) | |
pp encrypt("J\\wgdm\x1Ehq\x1D>mlhne-\x1EP`nsdm`dp\x1D,1sg!\x1E9'", 352110212) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment