Compsci 101, Fall 2014, Lab 5

Answer the following questions on the online google form for lab 5.

Part 1: While loops

findFirst

  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

  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

  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.

    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.

    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?

  9. What is the code for this function?


Part 3: The OneD Walker

  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.