Course Home | Assignments | Computing Resources | Lab Hours/Tutoring | Python | Schedule | Submit

Saint Louis University

Computer Science 150
Introduction to Object-Oriented Programming

Michael Goldwasser

Spring 2013

Dept. of Math & Computer Science

Hands-on Day

Using Python's Built-in Types

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.

Problems

  1. Write a program that asks a person for his or her name, and issues a greeting. A run of the program might produces the following (with the underlined portion being the user's response).

    What is your name? Bugs Bunny
    Hello Bugs Bunny!
    


  2. Write a program that asks a person for his or her name, and then reformats that name to appear in the form last, first, as shown in the following.

    What is your name? Bugs Bunny
    Bunny, Bugs
    

    Can you write a program in a way so that it works on the above example but also works when the user specfies a middle name or initial, or multiple names?

    What is your name? Elmer J. Fudd
    Fudd, Elmer J.
    

    What is your name? George Herbert Walker Bush
    Bush, George Herbert Walker
    


  3. Write a program that can add two integers given by the user. Try to mimic the user interface from the following example:

    Enter an integer:  42
    Enter another integer:  29
    42 + 29 = 71
    


  4. Write a program that relies on the Pythagorean Theorem (A2 + B2 = C2) to compute the length of the hypotenuse of a right triangle, given the lengths of the other two sides

    Enter length of first side: 42
    Enter length of second side: 29
    Hypotenuse length is 51.0392
    

    Note: square roots can be computed with the sqrt function imported from the math module.


  5. Write a program that takes a date of the numeric form month/date/year and converts it to the equivalent format date monthname year, demonstrated as follows.

    What is today's date [mm/dd/yyyy]? 01/26/2010
    26 January 2010
    

    Try to design the program so that if there is a leading zero in the date, it is nor printed in the newly formatted string, as in

    What is today's date [mm/dd/yyyy]? 01/05/2010
    5 January 2010
    

    Would your program still work if the user omits leading zeros in the month and/or year, as in the following?

    What is today's date [mm/dd/yyyy]? 1/1/2010
    1 January 2010
    


  6. Write a program that lets the user enter a date in the form monthname date, year, and converts that date to the numeric form m/d/y, demonstrated as follows.

    What is today's date? January 26, 2010
    1/26/2010
    

    If you are interested in always ensuring that the new format uses two digits for the month and year (e.g., 01/26/2010) read the documentation for help(str.rjust).


Solution Set


Additional Problems

  1. Write a program that asks the user to enter a word and then determines if it is a palindrome (that is, a word that is spelled the same backward as forward). Sample session might appear as follows.

    Enter a word? kayak
    Palindrome: True
    

    Enter a word? boat
    Palindrome: False
    

  2. Write a program that asks the user to enter a sentence and reports the number of words entered (assuming that whitespace is used to separate words) A sample might be:
    Enter a sentence: The quick brown fox jumps over the lazy dog
    You entered 9 words.
    

  3. Develop a program that tests a user's typing accuracy and speed. After giving a specific prompt, the user is to type the precise statement. You are then to report both on whether they accurately typed the statement, and what their average "words per minute" is based on that (where words per minute is calculated not based on actual words, but on the calculation of five characters per word.
    You are to precisely type the following:		
    The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog
    Accurate: True
    WPM: 101.981446514		
    
    (this particular result was based on a 5.06 second response time). You can measure the response time as follows. Do the command
    from time import time
    initially. Then you may call the function time() to get the current time measured in seconds (since 1970). The point is that if you record the time immediately before getting input and again just after the input was received, you can calculate the elapsed time (and thus the WPM).

Michael Goldwasser
CSCI 150, Spring 2013
Last modified: Friday, 04 February 2011
Course Home | Assignments | Computing Resources | Lab Hours/Tutoring | Python | Schedule | Submit