Skip to content

Instantly share code, notes, and snippets.

@gavinmcgimpsey
Last active May 6, 2016 05:46
Show Gist options
  • Save gavinmcgimpsey/b2fde1fbd9e81cc3069069024c41fc9f to your computer and use it in GitHub Desktop.
Save gavinmcgimpsey/b2fde1fbd9e81cc3069069024c41fc9f to your computer and use it in GitHub Desktop.
Launch School challenge - crypto square
class Crypto
def initialize(str)
@input = str.downcase
.chars
.keep_if { |c| c =~ /[0-9a-z]/ }
end
def size
Math.sqrt(@input.length).ceil
end
def normalize_plaintext
@input.join
end
def plaintext_segments
@input.each_slice(self.size).map(&:join)
end
def normalize_ciphertext
rows = self.plaintext_segments.map(&:chars)
rows.last << " " until rows.last.length == rows.first.length
rows.transpose
.map(&:join)
.map(&:strip)
.join(" ")
end
def ciphertext
self.normalize_ciphertext
.split(" ")
.join
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment