Skip to content

Instantly share code, notes, and snippets.

@amaseda
Last active October 12, 2016 12:51
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 amaseda/773420b28941c6a290986fd1f1e5560d to your computer and use it in GitHub Desktop.
Save amaseda/773420b28941c6a290986fd1f1e5560d to your computer and use it in GitHub Desktop.
encryption code challenges

Encryption Code Challenges

Team up with a partner and pick a code challenge. You will spend 15 minutes solving it individually. After 15 minutes -- even if you are not finished -- you will spend 10 minutes reviewing review each other's code. Some things to discuss

  • Approach
  • Where you got stuck
  • Opportunities for refactoring

Maskify

For security purposes, you rarely want to show somebody's full credit card or social security number online. Instead, you usually display only the last four digits and hide the rest (e.g., ************1234).

Define a method that takes in a string. It should return a string which leaves the final four characters untouched, while the rest are converted to asterisks.

maskify("12345678")
# => "****5678"

maskify("18240981253")
# => "*******1253"

maskify("my dog's name is fido")
# => "*****************fido"

If the method is fed a string shorter than 4 characters, return the string as is.

maskify("123")
# => "123"

Caesar Cipher II

Define a method that, given a string (str), returns a string in which each letter is incremented by some number (num). So, if num = 2, "a" turns into "c" and "x" turns into "z").

caesar("abcd", 5)
# => "fghi"

caesar("hello", 2)
# => "jgnnq"

Bonus

The method should ignore any character that is not a letter.

caesar("hello, world.", 2)
# => "jgnnq, yqtnf."

Atbash Cipher

Define a method that, given a string (str), returns a string in which each character is converted to its inverse. For example, "c" (the third letter in the alphabet) should be converted to "x" (third-to-last letter). Or "y" (second-to-last) should be converted to "b" (second).

atbash("abcd")
# => "zyxw"

atbash("hello")
# => "svool"

It might help to include the following somewhere in your code...

alphabet = "abcdefghijklmnopqrstuvwxyz".to_a

You can learn more about the Atbash Cipher here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment