Lab 2: String Operations, Functions, Modules, and Basic Loops

Goals

After the lab, you should know how to

  1. utilize some UNIX network utilities
  2. solve problems involving string operations
  3. format output nicely in Python
  4. utilize built-in and imported functions in Python
  5. solve basic looping problems in Python

Objective: Practice Using Linux

Set up for Lab 2

Create a directory called lab2 in your labs directory. Your programs and the output for this lab will all be saved in the lab2 directory.

Using UNIX Network Tools (15 pts)

We talked about WikiScanner in class on Friday. Today, you're going to do some of what WikiScanner was able to do.

You will use the UNIX commands host, nslookup, and whois.

Record your answers to the following questions in a text file called unix.txt. You can use jedit to edit the text file. Clearly label your answers.

  1. Use the host command to look up the IP address for www.espn.com:

    host www.espn.com

  2. Use the host command to look up the IP address(es) for www.cnn.com
  3. Use the host command to look up the IP address and alias for www.cs.wlu.edu
  4. Use the nslookup command to look up the IP address and "canonical" (alias) for www.wlu.edu

    nslookup www.wlu.edu

  5. Use the whois command to find out a) the administrative contact, b) the technical contact, c) the IP addresses of the name servers, and d) the date that the name wlu.edu was activated.

    whois wlu.edu

  6. What is common in the IP addresses for all of the *.wlu.edu machines (including the name servers)?
  7. Use the whois command to find out the date that the espn.com domain was activated.

Objective: Programming in Python

We'll practice writing several Python programs, each in their own text file. Name the files lab2.1.py through lab2.8.py.

Your programs will be graded on correctness, style, and how well you tested them. Make sure you adhere to the good development and testing practices we discussed in class. Your code should be readable and your output should be useful and well-formatted.

After you've developed a correct solution to each program, restart IDLE or close and reopen the IDLE "shell" by running the program again (using F5), demonstrate that the program works using several good test cases, and save the output to a file named lab2.x.out, where x is the problem number.

  1. (10 pts) Create a simple questionaire-development program that creates questionaire questions, based on what the user wants to compare. The generated question always has the format "Which do you like better: X or Y?"

    An example run is shown below:

    Welcome to the simple questionaire program!
    What do you want to compare? Apples
    What do you want to compare Apples to? Oranges
    Which do you like better: Apples or Oranges?
    
  2. (5 pts) Copy your program lab1.5.py that converted temperatures from Fehrenheit to Celcius. Modify the program so that it only prints temperatures to two decimal places.
  3. (10 pts) Copy your program lab1.6.py that calculated molecular weights. Modify your program to use constants for the molecular weights of carbon, hydrogen, and oxygen. (Recall what the conventions are for naming constants. Why does it make sense to make these values constants?) Then, modify your program so that it rounds the molecular weight to 3 decimal places and displays that number.
  4. (15 pts) Write a program that creates a table of Olympic competition running distances in meters, kilmeters, yards, and miles. The following distances should be used: 100 m, 200 m, 400 m, and 800 m.

    Note that 1 m = .001 km = 1.094 yds = .0006215 mi

    Calculate and display the results on the screen in the following manner:

    
    Meters   Kilometers   Yards    Miles
    --------------------------------------
       100        0.100   109.4    0.062
       200        -----   -----    -----
       400        -----   -----    -----
       800        -----   -----    -----
    

    Note: You will have all the converted values filled in.

  5. (10 pts) Write a program that calculates the area of a circle. Get the radius of the circle as input from the user. Use the most precise value of pi available to you, i.e., use the constant pi defined in the math module. Select a "reasonable" number of digits for precision in the result you display to the user.
  6. (10 pts) Write a program that sums all the odd integers between 0 and 100 and prints the total. Define a constant to stop your loop.
  7. (15 pts) Using three variables (i, j, and result), assign them values to calculate and display result = i % j. Use assignment and print statements and a for loop to show the results of i % j, where i = 4 and j increases from 1 to 8. Example output (with the appropriate values filled in):
    4 % 1 = ?
    4 % 2 = ?
    ...
    
  8. (10 pts) Using a for loop, draw a diagonal line that looks like:
    \
     \
      \
       \
        \
    
    After you have that working, have the user enter the size of the diagonal line and draw a line of the appropriate size.

Finishing up: What to turn in for this lab

  1. Copy your lab2 directory into the turnin directory. (Review the UNIX handout if you don't remember how to do that.)
  2. Cleanup: jEdit makes backup files and appends "~" to the name of your file. Delete any "~" files from your lab directory.
  3. Turn in your printed lab assignment, using the printLab.sh command:
    printLab.sh <labdirname>

    Again, you should probably print from the labs directory.

    View your file using the gv command.

    Print the file using the lpr command introduced in the first lab.

Labs are due at the beginning of Friday's class. You should hand in the printed copy at the beginning of class, and the electronic version should be in the turnin directory before 1:20 p.m. on Friday.

Ask well before the deadline if you need help turning in your assignment!

Grading (100 pts)