The goal of this assignment is to allow you to practice utilizing lists
and strings in the Python programming language. To record your
solutions and answers, create a new Jupyter Notebook titled
Notebook05.ipynb
and use this notebook to complete the following
activities and answer the corresponding questions.
Make sure you label your activities appropriately. That is for Activity 1, have a header cell that is titled Activity 1. Likewise, use the Markdown cells to answer the questions (include the questions above the answer).
This Notebook assignment is due midnight Friday, September 30, 2016 and is to be done individually.
For this activity, you are to write a function that determines if a list contains any duplicates (that is, if any elements in the list are the same).
The following is a skeleton of the code you must implement to complete this activity.
def has_duplicates(lst):
''' Returns whether or not list contains duplicates. '''
# TODO
Given a list
lst
, this function returns whether or notlst
contains any duplicates (True
if there are duplicates andFalse
if there are not any repeats).
Once you have defined the has_duplicates
function, you should be able to
do the following to test it:
>>> has_duplicates([0, 1])
False
>>> has_duplicates([1, 1])
True
The following are hints and suggestions that will help you complete this activity:
Nested Loop: The most straightforward way is to use a nested loop to
check each item in lst
with every other item in lst
.
Sort: Another method is to first sort lst
and then scan the sorted
items for duplicates.
Python's built-in set
data structure would make solving this problem
pretty straightforward, but we ask that you do not use it for this
activity. Instead stick to a regular list
.
This is a common programming interview question.
After completing the activity above, answer the following questions:
Briefy describe how your function works and what challenges you faced implementing it. In particular, discuss:
Which approach did you chose?
How did you detect if there is a duplicate in the list?
Demonstrate your has_duplicates
function by writing a loop that checks
each of the following lists and outputs '"{test}" has duplicates!' if the
list has duplicates, otherwise '"{test} does not have duplicates!'.
tests = [
[],
[0],
[0, 0],
[0, 1, 2, 3, 4],
[0, 1, 0, 3, 4],
[0, 1, 2, 4, 4],
]
For this activity, you are to implement a function that determines if a string is a palindrome, which is a phrase that reads the same forwards and backwards (ignoring whitespace and punctuation). The phrase "taco cat" is an example of a palindrome.
The following is a skeleton of the code you must implement to complete this activity.
def is_palindrome(s, case_sensitive=False):
''' Returns whether or not a string is a palindrome '''
# TODO
Given a string
s
, this function determines whether or nots
is a palindrome. If the parametercase_sensitive
isTrue
, then the letters must match exactly, otherwise the function should ignore differences in case (lower vs upper).
Once you have defined the is_palindrome
function, you should be able to
do the following to test it:
>>> is_palindrome('Taco cat')
True
>>> is_palindrome('Taco cat', True)
False
The following are hints and suggestions that will help you complete this activity:
To make comparing letters easier, pre-process the input by removing all whitespace and punctuation from the string before performing the comparison.
One way to ignore case when case_sensitive
is False
is to convert
the string to one case.
Determine what exactly needs to be compared. Do you need a loop and if
so, what should be compared in each iteration? Could you utilize slices or
other list
and string
methods to help you solve this problem?
Note: There is more than one way to implement this function; the important thing is that you come up with a strategy that you understand and can implement effectively.
This is also a common programming interview question.
After completing the activity above, answer the following questions:
Briefy describe how your function works and what challenges you faced implementing it. In particular, discuss:
How does the case_sensitive
argument affect your function?
How did you handle whitespace and punctuation?
How did you test if the string was a palindrome?
Demonstrate your is_palindrome
function by writing a loop that checks
each of the following phrases and outputs '"{phrase}" is a palindrome!' if
the phrase is a palindrome, otherwise '"{phrase} is not a palindrome!'.
phrases = [
'Taco cat',
'Race car',
'Not a palindrome',
'Never odd or even',
'Bob',
'Be calm',
'Eva, Can I Stab Bats In A Cave?',
'Mr. Owl Ate My Metal Worm',
'Was It A Rat I Saw?',
"C'est La Vie"
]
For this activity, you are to write a function that processes a Mad Libs template string, by prompting the user to input the appropriate phrases, replaces the text, and then outputs the completed text.
The following is a skeleton of the code you must implement to complete this activity.
def process_madlib(template):
''' Return completed Mad Libs template string '''
# TODO
Given a string
template
, replace any instance of|DESCRIPTION|
by prompting the user for a replacement phrase and return the completed Mad Libs text.
For instance, given the following Mad Libs template string:
Hello |name|! This a very |adjective| Mad Libs generator!
You can process the template with your process_madlib
function:
>>> process_madlib('Hello |name|! This a very |adjective| Mad Libs generator!')
Please enter name? Breanna
Please enter adjective? sassy
'Hello Breanna! This a very sassy Mad Libs generator!'
In the example above, we called the process_madlibs
function with
argument 'Hello |name|! This a very |adjective| Mad Libs generator!'
.
This function check if each word in the string is surrounded by |
such as
|name|
and |adjective|
in the example above. These words are then
replaced by asking the user to enter a replacement. Once all the
placeholders are substituted, the completed Mad Libs template is
returned.
Use the string split method to separate words.
Use the string startswith method to check if a word needs to be replaced.
Use slicing and the index method to extract the DESCRIPTION
from
the placeholder.
Use raw_input to read input from the user. Be sure to handle the case where the user enters in nothing.
Store the contents of the story in a list, replace the appropriate text using string replace, and then print the list all at once after all the prompts have been completed.
After completing the activity above, answer the following questions:
Briefy describe how your function works and what challenges you faced implementing it. In particular, discuss:
How did you determine if a word need to be replaced?
How did you extract the DESCRIPTION
from the placeholder?
How did you handle if the user entered in nothing for a replacement?
How did you substitude the placeholder with the replacment?
How did you construct the completed Mad Libs template?
Write your own Mad Libs template and use your function to generate a story!
To submit your notebook, follow the same directions for Notebook 01, except store this notebook in the notebook05 folder.