Official Analysis (Python)

Video Solution

By Jeffrey Meng

Video Solution Code

Implementation

with open("word.in") as read:
max_width = int(read.readline().split()[1])
essay = read.readline().split()
formatted = ""
rn_length = 0
for w in essay:
# ok, this word exceeds the limit, let's wrap it around
if rn_length + len(w) > max_width:
formatted = formatted[:-1] + "\n"

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!