data_util Module

Introduction

In this assignment you’ll practice

Problem Description

You need to manipulate Python data (and you’re not familiar with Python’s standard library).

Solution Description

Create a module named data_util that implements the functions described below.

doctest

The specification for each function is given as a docstring – which you should include in your code – and the types of arguments and return values are given using type hints documented in PEP 484 – Type Hints.

Because each function will have a docstring that includes usage examples formatted as Python interactive sessions, you can use doctest to test your code using this command line invocation:

python -m doctest -v data_util.py

If all the tests pass, then you’ll probably (but not defnitely) get a 100 on this homework.

Required Functions

Implement the following functions in your data_util.py file. For convenience you can simply copy the code below.

IMPORTANT: Do not modify the provided docstrings!

def split(text, delim=","):
    """Return a list of fields in text separated by delim.

    Parameters:
    text: str -- the string to split into fields

    Return:
    list[str]

    Usage examples:
    >>> split("foo, bar, baz")
    ['foo', ' bar', ' baz']
    """

def zip(xs, ys):
    """Return [(x0, y0), ..., (xn, yn)] where n is 1 - min(len(xs), len(ys))

    Parameters:
    xs: Sequence -- the "left" list
    ys: Sequence -- the "right list

    Return:
    list[tuple]

    Usage examples:
    >>> zip(['a', 'b', 'c', 'd'], [1,2,3])
    [('a', 1), ('b', 2), ('c', 3)]
    """

def zip_with_indexes(xs):
    """Return [(0, x0), ..., (n, xn)] where n is 1 - len(xs)

    Parameters:
    xs: Sequence -- a sequence

    Return:
    list[tuple]

    Usage examples:
    >>> zip_with_indexes(['a', 'b', 'c', 'd'])
    [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
    """

def lookup_key(v, d):
    """Return a key in dict d which maps to v, or None if v isn't present

    Parameters:
    v: Any -- a value which may be in dictionary d
    d, : dict -- a dictionary which may contain the value v

    Return:
    Any

    Usage examples:
    >>> lookup_key(1, {'a': 1, 1: 'b', 'c': 2})
    'a'
    """

def lookup_keys(v, d):
    """Return list of keys in dict d which map to value

    Parameters:
    v: Any -- a value which may be in dictionary d
    d, : dict -- a dictionary which may contain the value v

    Return:
    list[Any]

    Usage examples:
    >>> lookup_keys(1, {'a': 1, 1: 'b', 'c': 2, 'd': 1})
    ['a', 'd']
    """

Sample Solution