Docsity

Homework 5: Python's Silly String - Introduction to Computing | CS 1301, Assignments of Computer Science

Toggle sidebar
bg1
Homework 5 CS 1301 – Spring 2008 Page 1/5
CS 1301 – Spring 2008
Homework 5 – Python’s Silly String
Due: Friday, February 22, 2008 at 11:55 PM EST
Files to submit: hw5.py
Functions to include in hw5.py:
1. pigLatin()
2. myCapitalize()
3. myStartsWith()
4. printMP3()
For Help:
T-Square Forum – see course syllabus for more detailed information
TA Helpdesk – schedule is posted on class website
Email TAs
Notes:
Think through the logic of each of the problems before you start coding anything
Don’t forget to include the required comments and collaboration statement (as outlined
on the course syllabus)
Do not wait until the last minute to do this assignment in case you run into problems
If you find an error in the homework assignment, let a TA know immediately
Information you might need:
Look at the String methods in the Python documentation here:
http://docs.python.org/lib/string-methods.html
You might find these methods useful:
lower()
upper()
startswith()
Also, pay attention to string slicing:
>>> text = "Yellow Jacket"
>>> text[1]
'e'
>>> text[1:]
'ellow Jacket'
>>> text[0:3]
'Yel'
pf3
pf4
pf5

Partial preview of the text

Download Homework 5: Python's Silly String - Introduction to Computing | CS 1301 and more Assignments Computer Science in PDF only on Docsity!

CS 1301 – Spring 2008

Homework 5 – Python’s Silly String

Due: Friday, February 22, 2008 at 11:55 PM EST

Files to submit: hw5.py Functions to include in hw5.py:

  1. pigLatin()
  2. myCapitalize()
  3. myStartsWith()
  4. printMP3() For Help:  T-Square Forum – see course syllabus for more detailed information  TA Helpdesk – schedule is posted on class website  Email TAs Notes:  Think through the logic of each of the problems before you start coding anything  Don’t forget to include the required comments and collaboration statement (as outlined on the course syllabus)  Do not wait until the last minute to do this assignment in case you run into problems  If you find an error in the homework assignment, let a TA know immediately

Information you might need:

Look at the String methods in the Python documentation here: http://docs.python.org/lib/string-methods.html You might find these methods useful: lower() upper() startswith() Also, pay attention to string slicing: >>> text = "Yellow Jacket" >>> text[1] 'e' >>> text[1:] 'ellow Jacket' >>> text[0:3] 'Yel'

Part 1 – Python API Questions

Include the answers to the following questions AS A COMMENT at the BOTTOM of your hw5.py file. Be sure to comment the answers out or your python code will NOT run. If your code does not run, you will receive a 0. All of the answers can be found by looking here: http://docs.python.org/lib/string-methods.html

How does the API denote an optional parameter?

Does capitalize() print anything?

What does center() return?

What is the difference between find() and rfind()?

What is the maximum number of parameters that rsplit() takes?

Part 3 – myCapitalize()

The Python string method capitalize() returns a copy of the string with only its first character capitalized. We are going to write our own version of this built in method to understand better how it works. Write a function, myCapitalize() that takes in a single word as its parameter with the same functionality as the built in method, capitalize(). Sample Output: >>> "hello".capitalize() 'Hello' >>> "hELLo".capitalize() 'Hello' >>> myCapitalize("hello") 'Hello' >>> myCapitalize("hELLo") 'Hello' Note: The first two examples are using the built in method while the second two are using the function you write. You can check your function against the built in method in this way. Hint: You may find lower() and upper() to be helpful methods to use—you are most definitely allowed to use these.

Part 4 – myStartsWith()

We will write a simplified version of the startswith() method. (Look it up on the API for full functionality). Write a function, myStartsWith() that takes in two strings—a word and a prefix to look for in that word. If the prefix is the beginning of the word, return True. Otherwise, return False. Sample Output: >>> "Hi".startswith("h") False >>> "king".startswith("kings") False >>> "king".startswith("ki") True >>> "king".startswith("knight") False >>> myStartsWith("king", "kings") False >>> myStartsWith("king", "king") True >>> myStartsWith("king", "kong") False Hint: You may find string slicing (word[::]) and len() to be helpful.

Part 5 – Finding those MP3’s!

Write a function called printMP3 that takes in a path and prints out all of the MP3’s located in that directory. If there are no MP3’s found, print “No MP3’s found!” As we have not gone over pathways in class yet, we are providing you with most of the code—the red commented out sections are where you need to add your own code. Here is a code skeleton to get you started: import os from os.path import isdir, isfile, join def printMP3(path): names = os.listdir(path) for name in names: longname = join(path, name) if isfile(longname) and #check to see if name ends with .mp3: #print out the file name

also check if there were no MP3’s found

Sample Output: >>> printMP3("/Users/pythonwarrior/Desktop") 02 Mr. Brightside.mp 03 Smile Like You Mean It.mp 04 Somebody Told Me.mp 05 All These Things That I've Done.mp >>> printMP3("/Users/pythonwarrior") No MP3's found!

Extra Credit:

Pick another method from the Python API and write your own version of it.

Final thoughts

Be sure to name your files and functions exactly as indicated. READ THROUGH THE INSTRUCTIONS CAREFULLY. If you need help, be sure to come to office hours. Good Luck! Written/Adapted by Beth York, Spring 2008