Compsci 101, Fall 2014, Lab 5

The lab

In this lab you will:

Snarf the code for lab 5 before getting started. The files for lab5 are also here: loops.py, fileFun.py, and OneDWalker.py. The data files are simple.txt, lorax.txt, and athletes.txt.

Getting Credit for Lab 5

To get credit for this lab, you will need to enter your names and netids, and the answers to several questions on an online form.

Complete this form for credit for lab 5

(the same questions are accessible here as well).

Part 1: While loops

See the file loops.py for this part.

findFirst

Consider writing the function findFirst that has two parameters phrase and letter . This function returns the first index position of letter in phrase, or returns -1 if the letter is not there. This is essentially the string "find" function.

You are to write this function by using a while loop and examining letters in the string from left to right until you find the letter.

Here are several examples.

findFirst("The tragedy of Romeo and Juliet", "a") returns 6 

Questions to answer

  1. What should be the return value for these:

    findFirst("The tragedy of Romeo and Juliet", "m") returns 
    
    findFirst("The tragedy of Romeo and Juliet", "z") returns 
    
    findFirst("The tragedy of Romeo and Juliet", "t") returns 
    

  2. What should the condition for the while loop be:
  3. What is the update in the while loop to make sure there is not an infinite loop?
  4. What is your final code for the function?

digitsSmallerThanSum

Now consider writing the function digitsSmallerThanSum: that has two parameters number and sum . This function adds the digits up from the right end of the number as long as their total is less than or equal to sum, and returns the number that forms the digits that were added up.

You must use a while loop to solve this problem.

Consider the following examples:

digitsSmallerThanSum(5372173, 11)  returns 173 
since 3 + 7 + 1 is 11, which is less than or equal to 11.

digitsSmallerThanSum(5372173, 2) returns 0 
since the rightmost digit 3 > sum

digitsSmallerThanSum(5372173, 15) returns 2173 
since 3+7+1+2 <= 15 and 3+7+1+2+7 > 15


digitsSmallerThanSum(234, 15) returns 234
since 2+3+4 <= 15

Questions to answer

  1. What should be the return value for these:

    digitsSmallerThanSum(55555555, 32)
    digitsSmallerThanSum(55555555, 5)
    

  2. What should the condition for the while loop be:
  3. What is the update in the while loop to make sure there is not an infinite loop?
  4. What is your final code for the function?


Part 2: Files

Look at the file fileFun.py and answer the following questions.

  1. Look in eclipse to see how the files are stored for this project. What are the folders for this project?
  2. Explain how fileFun.py knows how to find the data file simple.txt?
  3. Explain what the function printLines does (how are the lines printed) and why there is the print statement after the for loop.

  4. Write the function numberLines that returns the number of lines in the given file. This function should open the file, calculate the number, close the file and return the answer.

  5. Write the function numberWords that returns the number of words in the given file. This function should open the file, calculate the number, close the file and return the answer.

  6. Write the function numberOfWord that returns the number of times the parameter word appears in the given file. This function should open the file, calculate the number, close the file and return the answer.

  7. Consider the following file called athletes.txt. Its format is
    firstName:lastName:gender:sport 
    

    where gender is men or women and sport is a sport such as golf, soccer, etc.

    Here is the file:

    Sean:Davis:men:soccer
    Quinn:Cook:men:basketball
    Adam:Wood:men:golf
    Elizabeth:Williams:women:basketball
    Cameron:Moseley:men:soccer
    Celine:Boutier:women:golf
    Amile:Jefferson:men:basketball
    Ashton:Miller:women:soccer
    Casey:Martinez:women:soccer
    Rebecca:Greenwall:women:basketball
    Alexander:Matlari:men:golf
    Jahlil:Okafor:men:basketball
    Kendall:Cooper:women:basketball
    Sandy:Choi:women:golf
    Matt:Slotnick:men:soccer
    Imani:Dorsey:women:soccer
    Marshall:Plumlee:men:basketball
    Lizzy:Raben:women:soccer
    

    If you were going to process each line in athletes by spliting the line into parts, what separator should you use?

  8. Change the input file to athletes.txt and consider the function listAthletes that has two parameters, the filename and the sport. This function prints all the athletes that have that sport in the following format: lastname, firstname - gender's sport

    For example, with the call listAthletes("athletes.txt", "soccer") the output would be:

    Athletes in soccer
    Davis, Sean - men's soccer
    Moseley, Cameron - men's soccer
    Miller, Ashton - women's soccer
    Martinez, Casey - women's soccer
    Slotnick, Matt - men's soccer
    Dorsey, Imani - women's soccer
    Raben, Lizzy - women's soccer
    

    Suppose one line of the file is put in the variable line. Since lines in the file have carriage returns, what would be the line of code that takes this line and strips off the carriage return and breaks the line into parts into a list?

    For example, the line

     
    Matt:Slotnick:men:soccer\n
    

    strips off the carriage return and converts the line into the list of parts:

     
    ["Matt", "Slotnick",  "men", "soccer"]
    

    The line of code is?

  9. What is the code for this function?


Part 3: The OneD Walker

Do this part if there is time. If not, you can leave this part blank and submit the first two parts.

For this problem you'll be asked to think about the code in OneDWalker.py

  1. Run the program five times and write the numbers that are printed (after the 11).

  2. If you change steps in the trials function to 6 describe how the results printed are different and why they are different.

  3. Describe in words why the test while sum(locs) != 2*n+1 in the loop in walk works.

  4. Describe two ways of determining how many steps are needed (average) to visit all locations when the walk goes from -500 to 500. One method is simple but might take a while, the other method involves predicting a time based on trends from runs that do not take so long.