Last active
May 6, 2016 05:46
-
-
Save gavinmcgimpsey/b2fde1fbd9e81cc3069069024c41fc9f to your computer and use it in GitHub Desktop.
Launch School challenge - crypto square
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
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