Solved

Calculate the cost for word wrapping algorithm in Python using dynamic programming

Posted on 2014-11-16
7
304 Views
Last Modified: 2014-11-22
I am trying to implement word wrapping in Python using dynamic programming.

Given a sequence of words from a file, and a limit on the number of characters that can be put in one line (line width), put line breaks in the given sequence such that the lines are printed neatly. Assume that the length of each word is smaller than the line width This problem must be solved using dynamic programming.

This function should take in a list of strings (each string in the list is a word from the file) and an integer M (which represents the max number of characters per line, including spaces). This function should return the cost (an integer that represents the optimal value of the function, as seen in dynamic programming) and a string that contains the entire text from the file as one string with newline characters. It should not end with a blank line.

I have not implemented the word wrapping functionality yet, so the function currently only returns the cost, not the string of text contents.

I implemented an algorithm to calculate the cost of the function based on the pseudocode on page 15-38 of this document (http://www-bcf.usc.edu/~shanghua/teaching/Spring2010/public_html/files/HW3_Solutions_A.pdf) as well as an implementation in C++ (http://www.geeksforgeeks.org/dynamic-programming-set-18-word-wrap/).

My main issue is that I cannot get my function to compute the cost correctly using dynamic programming. I believe I have implemented the function correctly using dynamic programming, but I still cannot arrive to the correct answer. When I run printingneatly.py, it prints out the cost. I am not sure what specifically is wrong with my algorithm.

When you run the print_test_neatly.py file, the correct output for the cost of the function for each particular sample text file should be generated into a log file. The output is shown below (this can also be viewed in the output.log file that I have attached):

kubla_kahn.txt
cost =  1545
true cost =  1545
bad lines =  0

magna_carta.txt
cost =  13910
true cost =  13910
bad lines =  0

How can I optimize my word wrapping/cost algorithm so that it calculates the cost both correctly and efficiently (so that it takes only a couple of minutes to calculate it rather than an hour)?

Thank you for your help.
printingneatly.py
kubla-kahn.txt
magna-carta.txt
output.log
print-neatly-test.py
0
Comment
Question by:AttilaB
  • 4
  • 3
7 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 40447076
Do you have to use that algorithm?

I transformed the text into CrLf delimited strings, replacing the existing (CrLf) line breaks with space characters.  After applying the following regular expression, I get the word wrap results in the captured groups.
(.{1,50})(?: |\r\n|$)

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 40447097
You can even use the replace method to insert your linefeed characters
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40448896
An even simpler solution would be to use the textwrap.py module.

Example:
import textwrap

f=file("C:\Users\AikiMark\Downloads\kubla-kahn.txt","rb")
sData=f.read()
f.close
for l in textwrap.wrap(sData,40):
    print l

Open in new window


Of course, you would package this into a form where you would pass the desired width to the wrap() method.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:AttilaB
ID: 40448936
I have to make the word wrap function from scratch, though. I can't use a built-in library.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40448957
Sounds like coursework to me.  There is limited help for homework problems due to academic integrity issues.
0
 

Accepted Solution

by:
AttilaB earned 0 total points
ID: 40448981
I got it working using Java I have much more experience with and and managed to cross-code into Python.
Now it works. At least I found a solution.
0
 

Author Closing Comment

by:AttilaB
ID: 40459181
The person who looked like wanted to help refused to help later because  he felt that it would be against 'academic integrity'. I did not ask
for a solution handed to me just help to get to the solution. Luckily I found a solution myself.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now