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, October 9, 2015 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
.
After completing the activity above, answer the following questions:
Briefy describe how your function works and what challenges you faced implementing it. Which approach did you chose and how do you know 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 '"{sublist}" has duplicates!' if
the list has duplicates, otherwise '"{sublist} does not have duplicates!'.
lists = [
[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.
After completing the activity above, answer the following questions:
Briefy describe how your function works and what challenges you faced
implementing it. How does the case_sensitive
argument affect your
function? How did you handle whitespace and punctuation?
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',
'Derp',
]
For this activity, you are to write a function that displays a HTML that displays the following information for a given list of numbers:
Count: the amount of items in the list.
Minimum: the smallest item in the list.
Maximum: the largest item in the list.
Median: the item halfway in the list after ordering from least to greatest.
Mode: the item that appears the most often in the list.
Mean: the average of the items in the list.
Total: the total sum of the items int the list.
Data: the list itself.
The list of numbers will be given to the program via Jupyter's
interaction module in the form of a string. To separate individual items
in the list, the user may select between any of the following separators:
' ', ',', ':', '|'
.
The following is a skeleton of the code you must implement to complete this activity.
# Imports
from IPython.display import HTML, display
from IPython.html.widgets.interaction import interact
# Functions
def calculate_mode(data):
''' Return the mode of a list of numbers '''
# TODO
def print_statistics(raw_data, separator):
''' Calculate and display the statistics of the input data '''
# TODO
# Run interactive loop
interact(print_statistics, raw_data='', separator=(' ', ',', ':', '|'))
Your completed program should look like the following:
The following are hints and suggestions that will help you complete this activity:
You can use some of Python's built-in functions such as [len], [min], [max], and [sum].
To compute the mode, you will need to implement the calculate_mode
function which uses a nested loop to determine the mode of a list of
numbers.
To create a long string for your HTML, you can do something like this:
html = '''
<table>
<thead>
<th>Count</th>
...
<th>Data</th>
</thead>
<tbody>
<tr>
<td>{}</td>
...
<td>{}</td>
</tr>
</toby>
</table>
'''.format(count, ..., data)
The triple quotes string in Python let's you define long multi-line strings.
After completing the activity above, answer the following questions:
Explain how you converted the raw_data
into a form you can use to
calculate the statistics. How did you handle different separators
or
incomplete data?
Describe how your calculate_mode
function works. How exactly does it
compute the mode
of a list of numbers?
Demonstrate your interactive print_statistics
program on the following
set of data:
10, 2, 1, 7, 2, 4, 2, 3, 0, 8, 9, 2
To submit your notebook, follow the same directions for Notebook00, except store this notebook in the notebook05 folder.