Skip to content

Instantly share code, notes, and snippets.

@kangkyu
Created August 25, 2015 23:24
Show Gist options
  • Save kangkyu/f031c26a493fe4b7538a to your computer and use it in GitHub Desktop.
Save kangkyu/f031c26a493fe4b7538a to your computer and use it in GitHub Desktop.
# 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")
@charliemcelfresh
Copy link

👍 This is nice work -- clean, understandable, and delivers the results as an array -- rather than printing out stuff as you go. Very very good!!!!

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