forked from mikechau/practice_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_strings_vertically.rb
More file actions
41 lines (36 loc) · 1.14 KB
/
print_strings_vertically.rb
File metadata and controls
41 lines (36 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
puts "Enter a sentence you want to display vertically:"
array = gets.chomp
puts "=" * array.length
array = array.split(/ /)
#array = ["let", "the", "bodies", "hit", "the", "floor"]
# find the largest word
largest_word_length = 0
array.each do |word|
if word.length > largest_word_length
largest_word_length = word.length
end
end
#break words down into letter arrays and put them back into a new array
broken_words_array = []
array.each_with_index do |word,idx|
broken_words = word.scan(/./)
broken_words_array << broken_words
end
#expand the array if is not the length of the largest word
broken_words_array.each_with_index do |word,idx|
if word.count < largest_word_length
difference = largest_word_length - word.count
difference.times do
word << ' '
end
end
end
#collect each letter from their corresponding index and place into array
new_word_array = []
largest_word_length.times do |idx|
new_word_array << broken_words_array.collect {|ind| ind[idx]}
end
#give the illusion of printing the words vertically by taking the array and displaying it as a string with spaces
new_word_array.each do |word|
puts word.join(" ")
end