-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjustifyText.rb
53 lines (44 loc) · 1.21 KB
/
justifyText.rb
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
42
43
44
45
46
47
48
49
50
51
52
53
def full_justify(words, max_width)
lines = []
cumulative_length = -1
current_words = []
line_lengths = []
words.each do |word|
current_length = cumulative_length + word.length + 1
if current_length > max_width
lines.append(current_words)
line_lengths.append(cumulative_length)
cumulative_length = -1
current_words = []
end
cumulative_length += word.length + 1
current_words.append(word)
end
# if there are words left append them to a new line.
if current_words
lines.append(current_words)
line_lengths.append(cumulative_length)
end
justified_lines = []
lines.zip(line_lengths).each do |(words, length)|
missing_spaces = max_width - length
line = ""
if words.length > 1
required_spaces = 1 + (missing_spaces / (words.length - 1))
bonus_space = missing_spaces % (words.length - 1)
words[0..-2].each.with_index do |word, index|
line += word
line += " " * required_spaces
if index < bonus_space
line += " "
end
end
line += words[-1]
else
line += words[-1]
line += " " * missing_spaces
end
justified_lines.append(line)
end
justified_lines
end