Last active
September 2, 2023 18:40
-
Star
(0)
You must be signed in to star a gist -
Fork
(324)
You must be signed in to fork a gist
-
-
Save dbc-challenges/79236b8fc536fe0c4633 to your computer and use it in GitHub Desktop.
The N.S.A. just broke Kim Jong Un's cipher that he's been using to give instructions to his military commanders! We wrote the following program to decipher the messages. As the N.S.A.'s best programmer on staff, your job is to refactor the code.
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
# RUN THE CODE BEFORE YOU MAKE ANY CHANGES. | |
# What if the code doesn't even work? (It does, but you should still always run it before making changes)! | |
# HINTS: | |
# If you do not understand what a line of code is doing. Go into IRB and try to figure out how it works. | |
# Use "puts" statements to track what your program is doing at each step. See line 42 for an example. | |
# Original Code | |
def north_korean_cipher(coded_message) | |
input = coded_message.downcase.split("") # Check out this method in IRB to see how it works! Also refer to the ruby docs. | |
decoded_sentence = [] | |
cipher = {"e" => "a", # This is technically a shift of four letters...Can you think of a way to automate this? Is a hash | |
"f" => "b", # the best data structure for this problem? What are the pros and cons of hashes? | |
"g" => "c", | |
"h" => "d", | |
"i" => "e", | |
"j" => "f", | |
"k" => "g", | |
"l" => "h", | |
"m" => "i", | |
"n" => "j", | |
"o" => "k", | |
"p" => "l", | |
"q" => "m", | |
"r" => "n", | |
"s" => "o", | |
"t" => "p", | |
"u" => "q", | |
"v" => "r", | |
"w" => "s", | |
"x" => "t", | |
"y" => "u", | |
"z" => "v", | |
"a" => "w", | |
"b" => "x", | |
"c" => "y", | |
"d" => "z"} | |
input.each do |x| # What is #each doing here? | |
found_match = false # Why would this be assigned to false from the outset? What happens when it's true? | |
cipher.each_key do |y| # What is #each_key doing here? | |
if x == y # What is this comparing? Where is it getting x? Where is it getting y? What are those variables really? | |
puts "I am comparing x and y. X is #{x} and Y is #{y}." | |
decoded_sentence << cipher[y] | |
found_match = true | |
break # Why is it breaking here? | |
elsif x == "@" || x == "#" || x == "$" || x == "%"|| x == "^" || x == "&"|| x =="*" #What the heck is this doing? | |
decoded_sentence << " " | |
found_match = true | |
break | |
elsif (0..9).to_a.include?(x) # Try this out in IRB. What does " (0..9).to_a " do? | |
decoded_sentence << x | |
found_match = true | |
break | |
end | |
end | |
if not found_match # What is this looking for? | |
decoded_sentence << x | |
end | |
end | |
decoded_sentence = decoded_sentence.join("") | |
if decoded_sentence.match(/\d+/) #What is this matching? Look at Rubular for help. | |
decoded_sentence.gsub!(/\d+/) { |num| num.to_i / 100 } #He's been known to exaggerate... | |
end | |
return decoded_sentence # What is this returning? | |
end | |
p north_korean_cipher("m^aerx%e&gsoi!") == "i want a coke!" #This is driver code and should print true | |
# Find out what Kim Jong Un is saying below and turn it into driver code as well. Driver Code statements should always return "true" | |
p north_korean_cipher("syv@tistpi$iex#xli*qswx*hipmgmsyw*erh*ryxvmxmsyw%jsshw^jvsq^syv#1000000#tvsjmxefpi$jevqw.") | |
p north_korean_cipher("syv%ryoiw#evi#liph^xskixliv@fc^kveti-jpezsvih@xsjjii.*hsr'x%xipp&xli#yw!") | |
p north_korean_cipher("mj^csy&qeoi^sri*qmwxeoi,%kir.*vm@csrk-kmp,&csy^ampp*fi&vitpegih*fc@hirrmw&vshqer.") | |
p north_korean_cipher("ribx^wxst:$wsyxl%osvie,$xlir$neter,#xlir%xli%asvph!") | |
p north_korean_cipher("ger^wsqifshc*nywx^kix^qi&10000*fekw@sj$gssp%vergl@hsvmxsw?") | |
# Your Refactored Code goes below: | |
# BONUS: Write the cipher method to do encryption, not just decryption! | |
# Pass an english sentence, and get the encrypted text back. | |
# REVIEW/REFLECT | |
# Reflection is vital to learning and growth. These challenges should not be thought of as items | |
# to check off; they are your opportunities to learn and grow as a programmer. | |
# Use the following questions to reflect on this challenge. | |
# Was your strategy for solving the challenge successful? | |
# What part of your strategy worked? What parts were challenging? | |
# What questions did you have while coding? Did you find answers to them? | |
# Are there any concepts that are still confusing to you? | |
# Did you learn any new skills or ruby tricks? | |
# INCLUDE REFLECTION HERE: | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ruby 1.9.3 Documentation
Ruby 2.0 Documentation