Course Home | Class Schedule | Assignments | Git Submission | Perusall | Python | Tutoring

Saint Louis University

Computer Science 1300/5001
Introduction to Object-Oriented Programming

Michael Goldwasser

Fall 2018

Computer Science Department

Hands-on Day

Strings and Lists


Note: For all of the following problems, you may assume that the user input adheres to the expected format, although for some problems there is some intended variation in the precise input format.

All of these problems are for practice. You are freely encouraged to work together and you do not need to submit any of this work for a grade.


  1. Write a program that performs a simple version of conversion to Pig Latin, where the user enters a single word and you output a new word in which the first letter of the original word is moved to the end and then followed by the letters 'ay'. Finally, the result should be capitalized in its new form. The precise format should match the following sample session. (Note well the treatment of capitalization.)

    Enter a word: Book
    Ookbay

    You do not need to concern yourself with more complicated special cases for Pig Latin (such as when the word starts with a vowel)

    Answer:

  2. Write a program that takes a word and prints its first three characters (if those exist) in uppercase. Sample executions appears as follows:
    Enter a word: March
    MAR
    Enter a word: no
    NO

    Answer:

  3. Write a program that accepts two integers on a single line, separated by a space, and which prints the sum of the two values. A sample execution appears as follows:
    Enter two integers: 15 3
    Their sum is 18

    Answer:

  4. Write a program which accepts a word from the user and which reports whether that word is a palindrome (that is, the same forward or backward). Sample executions appears as follows:
    Enter a word: kayak
    Palindrome: True
    Enter a word: boat
    Palindrome: False

    Hint: you can print a boolean value.

    Answer:

  5. Write a program that accepts a person's height in traditional notation for feet/inches, and which reports the number of inches that represents. A sample execution appears as follows:
    What is your height (e.g., 5'8")? 5'10"
    That is 70 inches.

    Answer:

  6. Write a program that asks the user for a date, in the form Month day, year, and which echos that date in numeric form mm/dd/yyyy. A sample execution appears as follows:
    What is today's date? September 17, 2018
    9/17/2018

    Your program may rely on use of the following tuple.

    monthNames = ('January', 'February', 'March', 'April',
         'May', 'June', 'July', 'August', 'September',
         'October', 'November', 'December')

    Answer:

  7. Write a program that takes a date of the numeric form mm/dd/yyyy and converts it to the equivalent format date monthname year, demonstrated as follows:
    What is today's date [mm/dd/yyyy]? 09/17/2018
    17 September 2018

    Try to design the program so that if there is a leading zero in the date, it is omitted in the newly formatted string, as in
    What is today's date [mm/dd/yyyy]? 09/03/2018
    3 September 2018

    Would your program still work if the user omits leading zeros in the month and/or day, as in the following?
    What is today's date [mm/dd/yyyy]? 01/01/2000
    1 January 2000

    Your program may rely on use of the following tuple.

    monthNames = ('January', 'February', 'March', 'April',
         'May', 'June', 'July', 'August', 'September',
         'October', 'November', 'December')

    Answer:

  8. Write a program that asks the user for the name of a month, and which reports the appropriate birthstone. A sample execution appears as follows:
    Enter a month: March
    March's birthstone is Aquamarine.
    Your program can rely on use of the following two tuples.

    monthNames = ('January', 'February', 'March', 'April',
         'May', 'June', 'July', 'August',
         'September', 'October', 'November', 'December')
    birthstones = ('Garnet', 'Amethyst', 'Aquamarine', 'Diamond',
        'Emerald', 'Pearl', 'Ruby', 'Peridot',
        'Sapphire', 'Opal', 'Topaz', 'Turquoise')

    Answer:

  9. The replace(old,new) method of strings replaces all occurrences of old with new. However, if that method did not exist, we could accomplish the task by splitting on the old pattern to get the remaining pieces, and then joining those back together with the new replacement.

    Demonstrate this technique by writing a program that asks the user for a sentence and which replaces all spaces with underscore characters. Sample executions appear as follows:
    Enter a sentence: The quick brown fox jumps over the lazy dog
    The_quick_brown_fox_jumps_over_the_lazy_dog
    Enter a sentence: What if   there are   extra spaces ?
    What_if___there_are___extra_spaces_?

    Answer:


Michael Goldwasser
CSCI 1300/5001, Fall 2018
Last modified: Sunday, 16 September 2018
Course Home | Class Schedule | Assignments | Git Submission | Perusall | Python | Tutoring