Dec 15, 2000, 2:56 PM
Post #1 of 8
Quiz 1 -- Equal Parts
String Division (2 Quizzes in 1!)
Given a string, how can you efficiently divide the string into x pieces, where each piece has y characters, except for possibly the last, which has between 1 and y characters?
For the love of Perl, please test your solutions before posting them -- if you find a place where your algorithm fails, post THAT along with your attempt, so that people might be able to help you.
Quiz 2 -- Approximate Parts
This is an introduction to greedy algorithms. Greedy algorithms do what is locally best, and, if they are successful, end up doing the right thing.
So you are given a string, and you are asked to split it into x pieces, but can only split on whitespace. And you are asked to make the pieces as close to the same length as possible. Example:
Splitting "would you like some pizza?" into two pieces results in "would you like" and "some pizza?", whereas splitting it into 3 pieces results in "would you", "like some", and "pizza?"
Assume the string is a list of non-whitespace characters joined by single spaces (that is, you'll never have two spaces in a row, and there is only space between words, not at the beginning of end of the string).
You might find it easier to approach the problem for splitting into 2 pieces, and then generalizing from there.
Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author