Overview

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.

Activity 1: Checking For Duplicates

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).

Code Skeleton

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 not lst contains any duplicates (True if there are duplicates and False 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

Hints

The following are hints and suggestions that will help you complete this activity:

  1. Nested Loop: The most straightforward way is to use a nested loop to check each item in lst with every other item in lst.

  2. Sort: Another method is to first sort lst and then scan the sorted items for duplicates.

Don't use a Set

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.

Questions

After completing the activity above, answer the following questions:

  1. 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?

  2. 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],
    ]
    

Activity 2: Checking For Palindromes

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.

Code Skeleton

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 not s is a palindrome. If the parameter case_sensitive is True, 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

Hints

The following are hints and suggestions that will help you complete this activity:

  1. To make comparing letters easier, pre-process the input by removing all whitespace and punctuation from the string before performing the comparison.

  2. One way to ignore case when case_sensitive is False is to convert the string to one case.

  3. 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.

Questions

After completing the activity above, answer the following questions:

  1. 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?

  2. 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',
    ]
    

Activity 3: Calculating Statistics

For this activity, you are to write a function that displays a HTML that displays the following information for a given list of numbers:

  1. Count: the amount of items in the list.

  2. Minimum: the smallest item in the list.

  3. Maximum: the largest item in the list.

  4. Median: the item halfway in the list after ordering from least to greatest.

  5. Mode: the item that appears the most often in the list.

  6. Mean: the average of the items in the list.

  7. Total: the total sum of the items int the list.

  8. 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: ' ', ',', ':', '|'.

Code Skeleton

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:

Hints

The following are hints and suggestions that will help you complete this activity:

  1. You can use some of Python's built-in functions such as [len], [min], [max], and [sum].

  2. 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.

  3. 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.

Questions

After completing the activity above, answer the following questions:

  1. 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?

  2. Describe how your calculate_mode function works. How exactly does it compute the mode of a list of numbers?

  3. Demonstrate your interactive print_statistics program on the following set of data:

    10, 2, 1, 7, 2, 4, 2, 3, 0, 8, 9, 2
    

Submission

To submit your notebook, follow the same directions for Notebook00, except store this notebook in the notebook05 folder.