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.
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).
See the file loops.py
for this part.
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
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
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
digitsSmallerThanSum(55555555, 32) digitsSmallerThanSum(55555555, 5)
Look at the file fileFun.py
and answer the following
questions.
fileFun.py
knows how to find the data file simple.txt
?
printLines
does (how are the lines printed)
and why there is the print statement after the for loop.
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.
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.
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.
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?
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?
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
steps
in the trials
function to 6 describe how the results printed are different and
why they are different.
while sum(locs) !=
2*n+1
in the loop in walk
works.