Program of the Day #13

Implement the following function:

def line_wrap(in_str, line_length):
    """ Wrap in_str onto multiple lines such that each line has a maximum of
    line_length characters. Return the resulting string.
    Preconditions: in_str contains no newlines; line_length > 0. """

Notice that the result is returned, not printed. Also implement a simple main program that takes the string as the first argument and the line_length as the second argument. You can provide command line arguments that include spaces (and avoid having them be treated as separate arguments) by surrounding the argument in quotes as illustrated in this example run:

>>> %Run P13_wrap.py 'The quick brown fox jumped over the lazy dog' 15
The quick brown
 fox jumped ove
r the lazy dog

Other Practice Problems

  1. (*) POTD, Challenge extension 1: Solve the same problem, but assume that the input is broken up into words separated by whitespace and ensure that lines are not broken mid-word. This means that you may need to end a line earlier than line_length if the next word is long enough to go over that maximum.

  2. (*) POTD, Challenge extension 2: Write an add_banner function that takes the same arguments as the POTD and wraps the text but also surrounds it with a rectangle of # symbols. As before, assume the string does not contain any newlines. This can be combined with #1 to respect whole words, or not; an example output that is not combined with #1 might look something like the following:

    ``` >>> import P13_practice >>> P13_practice.add_banner(“The quick brown fox jumped over the lazy dog”, 16) ################ # The quick br # # own fox jump # # ed over the # # lazy dog # ################ >>>

  3. Implement the following function:

    def remove_vowels(string):
        """ Print string, but with all vowels removed. Don't count y as a vowel. """
  4. Implement the following function, which differs only in that it returns the string without vowels:

    def remove_vowels(string):
        """ Return string, but with all vowels removed. Don't count y as a vowel. """
  5. Implement the following function:

    def count_vowels(string):
        """ Return the number of vowels (not including y) in string. """
  6. Implement the following function:

    def remove_comments(string):
        """ Return a copy of string, but with all characters starting with and following
            the first instance of ‘#’ removed. If there is no # in the string, return
            the input unchanged."""
  7. Write a function that takes a string and prints all prefixes of the string, including the string itself.

    >>> prefixes("abcd")
    a
    ab
    abc
    abcd
    >>> prefixes("banana")
    b
    ba
    ban
    bana
    banan
    banana
    >>>
  8. Implement the following function:

    def house_number(address_line):
        """ Return the house number (i.e., digits only) portion of 
            the given address line.
            Examples:
                house_number("1600 Pennsylvania Ave")
                  => 1600
                house_number("221B Baker St")
                  => 221
        """
        # your code here
        return result