Homework 3: Implement Functions Related to Strings, Lists, Sets, & Tuples | Introduction to Python Programming

 Introduction to Python Programming
University of Pennsylvania

Homework 3: Instructions

Homework 3: Implement Functions Related to Strings, Lists, Sets, & Tuples

This assignment is designed to give you practice writing code and applying lessons and topics for the current module.

This homework deals with the following topics:

  • Strings

  • Lists

  • Sets

  • Tuples

The Assignment

In this assignment, you will implement some functions related to strings, lists, sets and tuples. Each function has been defined for you, but without the code. See the docstring in each function for instructions on what the function is supposed to do and how to write the code. It should be clear enough.  In some cases, we have provided hints to help you get started.

For example, we have defined a “concatenate” function for you (see below) which concatenates a given list of strings into a single string and returns it.  Read the docstring, which explains what the function is supposed to do.  Then write your code where it says “# TODO” to implement the function.  You’ll do this for each function in the program.


def concatenate(strings):
    """
    Concatenates the given list of strings into a single string.
    Returns the single string.
    If the given list is empty, returns an empty string.
    For example:
    - If we call concatenate(["a","b","c"]), we'll get "abc" in return
    - If we call concatenate([]), we'll get "" in return
    """
    # TODO

We’ll run a Python test script against your program to test whether each function implementation is correct and whether you have considered enough cases.  Examples of cases have been provided in the docstrings.

The main function has been completely implemented for you (see example snippet below).  Use it to run and interact with your program, and to see if your functions are working as expected.  Spend some time on testing.

def main():
    """
    Calls all the functions above to see whether they've been implemented correctly.
    """
    # test concatenate
    print("test concatenate")
    word = concatenate(["b""e""a""t""l""e""s"])
    print(word == "beatles")
    print("=" * 50)

Solution-


Module 3

In this assignment, you will implement some functions related to strings, lists, sets and tuples. Each function has been defined for you, but without the code. See the docstring in each function for instructions on what the function is supposed to do and how to write the code. It should be clear enough. In some cases, we have provided hints to help you get started.

In [1]:
In [6]:
In [7]:
Success!
In [8]:
In [9]:
Success!
In [12]:
In [13]:
Success!
In [14]:
In [15]:
Success!
In [16]:
In [17]:
Success!
In [30]:
In [31]:
Success!
In [22]:
In [23]:
Success!
In [ ]:

Post a Comment

0 Comments