Created
June 6, 2011 12:59
-
-
Save thehack/1010191 to your computer and use it in GitHub Desktop.
I'm trying to split a phrase into 6 or less rows of 12 or less letters each.
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
#!/usr/bin/ruby | |
puts "Enter phrase. 60 characters or less." | |
phrase = gets.chomp | |
words = phrase.downcase.gsub(/[^a-z ]/, "").split(" ") | |
word_count = words.length | |
word_lengths = [] | |
words.each { |word| word_lengths << word.length } | |
# split the phrase into up to six lines, each no longer than twelve characters long. | |
line1, line2, line3, line4, line5, line6 = [], [], [], [], [], [] | |
for line in [line1, line2, line3, line4, line5, line6] | |
if (line << words.shift).join(" ").length <= 12 | |
line << words.shift | |
end | |
end | |
puts "line1: " + line1.join(" ") | |
puts "line2: " + line2.join(" ") | |
puts "line3: " + line3.join(" ") | |
puts "line4: " + line4.join(" ") | |
puts "line5: " + line5.join(" ") | |
puts "line6: " + line6.join(" ") | |
#I'm worried this is a bit hackish, especially the while loop. Is there a better way? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment