Created
August 25, 2015 23:24
-
-
Save kangkyu/f031c26a493fe4b7538a to your computer and use it in GitHub Desktop.
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
# write a function called "x" that takes a string, and, if it has an odd number of characters, prints it out diagonally twice -- lower left to upper right, and upper left to lower right -- and those two strings share their middle character | |
def x(string) | |
if string.length.odd? | |
array = [] | |
string.length.times do |row| | |
array << [] | |
string.length.times do |col| | |
if col == row || col == (string.length - row - 1) | |
array[row][col] = string[col] | |
else | |
array[row][col] = " " | |
end | |
end | |
array[row] = array[row].join | |
end | |
array | |
else | |
"not an odd length string" | |
end | |
end | |
puts x("felix") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍 This is nice work -- clean, understandable, and delivers the results as an array -- rather than printing out stuff as you go. Very very good!!!!