Computation and Cognition

Homework 2

Due by class time Wednesday, September 20

Reading

Practice Problems

Answer discussion questions 1-4 on pages 116-117. You should try these out interactively at the Python prompt. Make sure you understand what's going on.

Programming Problems

  1. Study the Python examples from this week's class notes and be sure you understand them.

  2. Finish all of the lab exercises from class.

  3. Do exercise 17 on page 74 of the textbook (Newton's method). Instead of starting with a guess of x/2, however, just start with 1.0.

  4. Using Python's string operations, write a program called url() that asks the user for a Web URL address, such as http://science.slc.edu/jmarshall/info.html, and prints out the hostname of the address and the name of the file being accessed. For example, the hostname of the above address is science.slc.edu and the filename is info.html. In general, the hostname appears immediately after the double "//" and includes everything up to the first single "/". The filename includes everything after the last "/". Example:

    >>> url()
    Please enter a Web URL: http://science.slc.edu/jmarshall/info.html
    The hostname is science.slc.edu
    The filename is info.html
    >>>
    
  5. Write a program called remove that asks the user for a string, and a substring appearing in that string, and returns a new string with the first occurrence of the substring removed. Example:

    >>> remove()
    Please enter a string: pineapples
    Substring to remove: apple
    The new string is pines
    
  6. Write a program called reverse that asks the user for a phrase and then prints out the phrase in reverse. Hint: use a for-loop. Example:

    >>> reverse()
    Please enter a phrase: The rain in Spain
    The phrase backwards is niapS ni niar ehT
    

Turning in Your Homework

Put all of your program definitions into a single file called assign2.py. Print out this file and turn it in to me in class on Wednesday. You don't need to turn in your answers to the practice problems. If you have questions about anything, don't hesitate to ask!