Skip to content

Instantly share code, notes, and snippets.

@aubuchcl
Forked from bootcoder/gist:9966851
Created April 4, 2014 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aubuchcl/9966859 to your computer and use it in GitHub Desktop.
Save aubuchcl/9966859 to your computer and use it in GitHub Desktop.
def north_korean_cipher(coded_sentence)
input = coded_sentence.downcase.split("") # takes the input and splits each letter into its own string. Check out this method in IRB to see how it works! Also refer to the ruby docs.
decoded_sentence = [] #blank array
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| # iterating through each character of the coded mesage. What is #each doing here?
found_match = false # if statement outside of cipher key wont run. Why would this be assigned to false from the outset? What happens when it's true?
cipher.each_key do |y| # looks at each key and compares to value. 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?
decoded_sentence << cipher[y]
found_match = true
end
end
if !found_match # This is looking for leftovers. Which happen to be ints within string. What is this looking for?
decoded_sentence << x
end
end
decoded_sentence = decoded_sentence.join("").capitalize #takes the array of single character strings and puts them together.
decoded_sentence.gsub!(/\d+/) { |num| num.to_i / 100 } #He's been known to exaggerate...
decoded_sentence # This returns the final decoded sentence. What is this returning?
end
# Driver Code:
p north_korean_cipher("m^aerx%e&gsoi!") == "I want a coke!" #This is driver code and should print true
p north_korean_cipher("syv@tistpi$iex#xli*qswx*hipmgmsyw*erh*ryxvmxmsyw%jsshw^jvsq^syv#1000000#tvsjmxefpi$jevqw.") == "Our people eat the most delicious and nutritious foods from our 10000 profitable farms."
p north_korean_cipher("syv%ryoiw#evi#liph^xskixliv@fc^kveti-jpezsvih@xsjjii.*hsr'x%xipp&xli#yw!") == "Our nukes are held together by grape-flavored toffee. don't tell the us!"
p north_korean_cipher("mj^csy&qeoi^sri*qmwxeoi,%kir.*vm@csrk-kmp,&csy^ampp*fi&vitpegih*fc@hirrmw&vshqer.") == "If you make one mistake, gen. ri yong-gil, you will be replaced by dennis rodman."
p north_korean_cipher("ribx^wxst:$wsyxl%osvie,$xlir$neter,#xlir%xli%asvph!") == "Next stop: south korea, then japan, then the world!"
p north_korean_cipher("ger^wsqifshc*nywx^kix^qi&10000*fekw@sj$gssp%vergl@hsvmxsw?") == "Can somebody just get me 100 bags of cool ranch doritos?"
# 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?")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment