Docsity

Assignment - Introduction to Computing | CS 1301, Assignments of Computer Science

Toggle sidebar
bg1
Homework 5 CS 1301 – Fall 2007 Page 1/5
CS 1301 – Fall 2007
Homework 5 – Python’s Silly String
Due: Friday, October 5, 2007 at 6 PM EST
Files to submit: hw5.py
Functions to include in hw5.py:
1. pigLatin()
2. myCapitalize()
3. myStartsWith()
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 Assignment - Introduction to Computing | CS 1301 and more Assignments Computer Science in PDF only on Docsity!

CS 1301 – Fall 2007

Homework 5 – Python’s Silly String

Due: Friday, October 5, 2007 at 6 PM EST

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

**1. pigLatin()

  1. myCapitalize()
  2. myStartsWith()** 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 count() return?

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

What is the maximum number of parameters that startswith() 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.

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, Fall 2007