इससे पहले कि आपको लगता है कि यह डुप्लिकेट किया गया है (कई प्रश्न पूछ रहे हैं कि शब्दों को तोड़ने के बिना लंबे तारों को कैसे विभाजित किया जाए) ध्यान रखें कि मेरी समस्या थोड़ा अलग है: आदेश महत्वपूर्ण नहीं है और मैं जितनी ज्यादा हो सके प्रत्येक लाइन का उपयोग करने के लिए शब्दों को फिट करना है।पूर्ण भरने वाली लाइनों को तोड़ने के बिना लंबी स्ट्रिंग को विभाजित करना
हाय,
मैं शब्दों का एक अव्यवस्थित निर्धारित किया है और मैं एक से अधिक 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/) पर हमला करें। –