文字が重複していると思う前に、長い文字列を分割する方法を尋ねる質問がたくさんあります。問題は少し異なります。可能な限りすべての行を使用するために、単語に合わせてください。長い文字列を分割せずに完全な文字列を分割する
こんにちは、
私は言葉の順不同のセットをしましたし、私は以上の253個の文字を使用せずにそれらを結合します。
def compose(words):
result = " ".join(words)
if len(result) > 253:
pass # this should not happen!
return result
私の問題は、可能な限り線を埋めるようにしたいということです。例:
words = "a bc def ghil mno pq r st uv"
limit = 5 # max 5 characters
# This is good because it's the shortest possible list,
# but I don't know how could I get it
# Note: order is not important
good = ["a def", "bc pq", "ghil", "mno r", "st uv"]
# This is bad because len(bad) > len(good)
# even if the limit of 5 characters is respected
# This is equivalent to:
# bad = ["a bc", "def", "ghil", "mno", "pq r", "st uv"]
import textwrap
bad = textwrap.wrap(words, limit)
どうすればよいですか?
これは動的プログラミングの問題です。あなたが[コインの変更の問題](http://www.geeksforgeeks.org/dynamic-programming-set-7-coin-change/)を攻撃するのと同じ方法で攻撃してください。 –