Non-Lab 6: Strings
Goals
After the non-lab, you should be proficient at
- solving basic string problems using methods, loops, and the substring operator
Objective: Review
Review the slides for today.
Objective: Linux
As usual, create a directory for the programs and output you
develop in this lab in your cs111
directory.
Objective: Help Client Set Up
Run runHelpClient
Objective: Programming in Python
We'll practice writing several Python programs, each in their own
text file. Name the files as lab6_x.py
, where x
is the problem number.
Your programs will be graded on correctness, style, efficiency, 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. I am getting tougher on these criteria as we develop larger programs.
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
lab6_x.out
, where x is the problem number.
- (15) Translate a string into pirate speak. Write a program that
takes a string as input, converts that string to pirate speak, and
displays it.
To convert a string to pirate speak, replace every 'r' in the string with 'rrr'. (You don't need to worry about uppercase R's, just lowercase.) For example:
Enter a string to convert to pirate speak: hello there, friends. hello therrre, frrriends.
Enter a string to convert to pirate speak: generals generrrals
If the string doesn't have the letter 'r' in it, add an ', arrrr' to the end of the string to make it more pirate like.
Enter a string to convert to pirate speak: washington and lee washington and lee, arrrr
(Did you know that programming had such practical uses?)
- (15) Web servers and Web browsers need to know a file's type so
they know how to display the file. The file's type is based on
the file name's extension. For example, we name our Python
scripts with the
.py
extension and our HTML files with the.html
extension.In the Python interpreter, run
help(str)
. Look at how to use therfind
method and compare that with how thefind
method is used.Then, use the
rfind
method to determine any file's type based on its name. Handle error cases appropriately.Example output:
What is the name of your file? index.html index.html is a html file.
What is the name of your file? picture.jpg picture.jpg is a jpg file.
What is the name of your file? 15.strings.pdf 15.strings.pdf is a pdf file.
- (15) Download/copy
the pick4winner.py
file and save as
lab6_3.py
. Refactor the code such that it has a function calledgenerateWinningNumber
that takes no parameters and returns a generated winning number. (Why can't we use thetestEqual
function to test this function?) Then, put the other, non-constants/non-imports code intomain
and callmain
. Demonstrate that your code works.
The next two problems are to focus on problem-solving with strings. You don't need to create functions for these problems.
Extra Credit (up to 7 pts)
Sometimes, lotteries pay out money for getting a few matches (the correct number in the same spot). For example, if the user guessed "1244" and the winning number is "1784", then the user had two correct numbers in the same spot (the first number and the last number) and maybe wins a little bit of money.
Copy your pick4winner.py (that you refactored above)
and name it lab6_ec.py
.
Modify it such that it reports how many numbers the user matched. If the user matched 2 or 3 numbers, congratulate the user on getting some numbers correct.
Include error handling to make sure that the user enters a valid number.
A solution that makes good use of functions will receive more credit.
************************************************** This program simulates the Pick 4 VA Lottery game ************************************************** What is your guess for the Pick 4 number (in format ####)? 1234 The Pick 4 Winner is 8272 You lose! Good thing you didn't bet any money.
************************************************** This program simulates the Pick 4 VA Lottery game ************************************************** What is your guess for the Pick 4 number (in format ####)? 123 Your pick does not have enough digits
************************************************** This program simulates the Pick 4 VA Lottery game ************************************************** What is your guess for the Pick 4 number (in format ####)? 1234 The Pick 4 Winner is 0232 You had 2 digits right! Here's a bit of cash.
Finishing up: What to turn in for this lab
- Create the printable lab assignment, using the
createPrintableLab
command:
createPrintableLab <labdirname> - View your file using the
evince
command. - Submit
your lab directory into your
turnin
directory. - Log out of your machine when you are done.
Note that each command links to a page with more information about using the command.
Labs are due at the beginning of Friday's class.
Ask well before the deadline if you need help turning in your assignment!
Grading (45 pts)
- Python programs: 45 pts; see above for breakdown