Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using list of characters instead of a string for Problem 24.8 #151 #152

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions epi_judge_python_solutions/left_right_justify_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@

def justify_text(words: List[str], L: int) -> List[str]:

# Join a list of lists of characters to list of strings
def get_strings(curr_line):
return [''.join(word) for word in curr_line]

curr_line_length, result = 0, []
curr_line: List[str] = []
curr_line: List[List[str]] = []
for word in words:
if curr_line_length + len(word) + len(curr_line) > L:
# Distribute equally between words in curr_line.
for i in range(L - curr_line_length):
curr_line[i % max(len(curr_line) - 1, 1)] += ' '
result.append(''.join(curr_line))
curr_line[i % max(len(curr_line) - 1, 1)].append(' ')
# Join the list of strings to a string
result.append(''.join(get_strings(curr_line)))
curr_line, curr_line_length = [], 0
curr_line.append(word)
# Add word as list of characters to avoid extra time complexity
# of string concatenation while padding with spaces
curr_line.append(list(word))
curr_line_length += len(word)
# Use ljust(L) to pad the last line with the appropriate number of blanks.
return result + [' '.join(curr_line).ljust(L)]

return result + [' '.join(get_strings(curr_line)).ljust(L)]

if __name__ == '__main__':
exit(
Expand Down