Skip to content

Commit 7f7afaa

Browse files
committed
solved: Text Justification
1 parent 529113a commit 7f7afaa

File tree

2 files changed

+244
-0
lines changed

2 files changed

+244
-0
lines changed

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,4 +515,68 @@ bool isNumber(String s) {
515515
516516
return num.tryParse(s) != null;
517517
}
518+
```
519+
520+
### Text Justification
521+
522+
```dart
523+
List<String> fullJustify(List<String> words, int maxWidth) {
524+
final List<String> result = [];
525+
String temp = '';
526+
527+
for (int index = 0; index < words.length; index++) {
528+
final String word = words[index];
529+
530+
if (temp.length + word.length + (temp.isEmpty ? 0 : 1) <= maxWidth) {
531+
temp += '${temp.isEmpty ? '' : ' '}$word';
532+
} else {
533+
if (temp.trim().isNotEmpty) {
534+
result.add(temp.trim());
535+
}
536+
temp = word;
537+
}
538+
539+
if (index == words.length - 1 && temp.isNotEmpty) {
540+
result.add(temp.trim());
541+
}
542+
}
543+
544+
return fillSpace(result, maxWidth);
545+
}
546+
547+
List<String> fillSpace(List<String> words, int maxWidth) {
548+
for (int i = 0; i < words.length; i++) {
549+
final String word = words[i];
550+
if (word.length < maxWidth) {
551+
final List<String> splits = word.split(' ');
552+
if (splits.length < 2 || i == words.length - 1) {
553+
words[i] =
554+
word + List.generate(maxWidth - word.length, (index) => ' ').join();
555+
} else {
556+
int leftSpace = maxWidth - word.length + splits.length - 1;
557+
String temp = '';
558+
559+
for (int j = 0; j < splits.length; j++) {
560+
final double div = leftSpace / (splits.length - j);
561+
int avgSpace = div.round();
562+
if (leftSpace / avgSpace > 0) {
563+
avgSpace = div.ceil();
564+
}
565+
566+
if (j == 0) {
567+
temp += splits[j];
568+
} else if (leftSpace > 0) {
569+
final int cal = leftSpace < avgSpace ? leftSpace : avgSpace;
570+
temp += '${List.generate(cal, (index) => ' ').join()}${splits[j]}';
571+
leftSpace -= cal;
572+
}
573+
}
574+
575+
words[i] = temp;
576+
}
577+
}
578+
}
579+
580+
return words;
581+
}
518582
```

src/text_justification.dart

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
void main(List<String> args) {
2+
List<List> cases = [
3+
[
4+
["This", "is", "an", "example", "of", "text", "justification."],
5+
16,
6+
],
7+
[
8+
["a", "b", "c", "d", "e"],
9+
3
10+
],
11+
[
12+
["Listen", "to", "many,", "speak", "to", "a", "few."],
13+
6,
14+
],
15+
[
16+
[
17+
"ask",
18+
"not",
19+
"what",
20+
"your",
21+
"country",
22+
"can",
23+
"do",
24+
"for",
25+
"you",
26+
"ask",
27+
"what",
28+
"you",
29+
"can",
30+
"do",
31+
"for",
32+
"your",
33+
"country"
34+
],
35+
16,
36+
],
37+
[
38+
[
39+
"Give",
40+
"me",
41+
"my",
42+
"Romeo;",
43+
"and,",
44+
"when",
45+
"he",
46+
"shall",
47+
"die,",
48+
"Take",
49+
"him",
50+
"and",
51+
"cut",
52+
"him",
53+
"out",
54+
"in",
55+
"little",
56+
"stars,",
57+
"And",
58+
"he",
59+
"will",
60+
"make",
61+
"the",
62+
"face",
63+
"of",
64+
"heaven",
65+
"so",
66+
"fine",
67+
"That",
68+
"all",
69+
"the",
70+
"world",
71+
"will",
72+
"be",
73+
"in",
74+
"love",
75+
"with",
76+
"night",
77+
"And",
78+
"pay",
79+
"no",
80+
"worship",
81+
"to",
82+
"the",
83+
"garish",
84+
"sun."
85+
],
86+
25
87+
],
88+
[
89+
["What", "must", "be", "acknowledgment", "shall", "be"],
90+
16,
91+
],
92+
[
93+
[
94+
"Science",
95+
"is",
96+
"what",
97+
"we",
98+
"understand",
99+
"well",
100+
"enough",
101+
"to",
102+
"explain",
103+
"to",
104+
"a",
105+
"computer.",
106+
"Art",
107+
"is",
108+
"everything",
109+
"else",
110+
"we",
111+
"do"
112+
],
113+
20,
114+
]
115+
];
116+
117+
for (final testcase in cases) {
118+
print(fullJustify(testcase.first, testcase.last));
119+
}
120+
}
121+
122+
List<String> fullJustify(List<String> words, int maxWidth) {
123+
final List<String> result = [];
124+
String temp = '';
125+
126+
for (int index = 0; index < words.length; index++) {
127+
final String word = words[index];
128+
129+
if (temp.length + word.length + (temp.isEmpty ? 0 : 1) <= maxWidth) {
130+
temp += '${temp.isEmpty ? '' : ' '}$word';
131+
} else {
132+
if (temp.trim().isNotEmpty) {
133+
result.add(temp.trim());
134+
}
135+
temp = word;
136+
}
137+
138+
if (index == words.length - 1 && temp.isNotEmpty) {
139+
result.add(temp.trim());
140+
}
141+
}
142+
143+
return fillSpace(result, maxWidth);
144+
}
145+
146+
List<String> fillSpace(List<String> words, int maxWidth) {
147+
for (int i = 0; i < words.length; i++) {
148+
final String word = words[i];
149+
if (word.length < maxWidth) {
150+
final List<String> splits = word.split(' ');
151+
if (splits.length < 2 || i == words.length - 1) {
152+
words[i] =
153+
word + List.generate(maxWidth - word.length, (index) => ' ').join();
154+
} else {
155+
int leftSpace = maxWidth - word.length + splits.length - 1;
156+
String temp = '';
157+
158+
for (int j = 0; j < splits.length; j++) {
159+
final double div = leftSpace / (splits.length - j);
160+
int avgSpace = div.round();
161+
if (leftSpace / avgSpace > 0) {
162+
avgSpace = div.ceil();
163+
}
164+
165+
if (j == 0) {
166+
temp += splits[j];
167+
} else if (leftSpace > 0) {
168+
final int cal = leftSpace < avgSpace ? leftSpace : avgSpace;
169+
temp += '${List.generate(cal, (index) => ' ').join()}${splits[j]}';
170+
leftSpace -= cal;
171+
}
172+
}
173+
174+
words[i] = temp;
175+
}
176+
}
177+
}
178+
179+
return words;
180+
}

0 commit comments

Comments
 (0)