Problem Set 1

Author

Lukas Hager

Published

November 28, 2024

This problem set must be submitted on Canvas by 11:59 PM PST on April 3, 2024.

Exercise 0

Please write a function that takes no arguments and returns a link to your solutions on GitHub.

Use the following shell:

def github() -> str:
    """
    Some docstrings.
    """

    return "https://github.com/<user>/<repo>/blob/main/<filename.py>"

Exercise 1

Please ensure that you can run python1, can use Git, and install the following packages2 (we’ll install more as we go):

  • numpy
  • pandas
  • scipy
  • matplotlib
  • seaborn

Exercise 2

Please write a function called evens_and_odds that takes as argument a natural number n and returns a dictionary with two keys, “evens” and “odds”. “evens” should be the sum of all the even natural numbers less than n, and “odds” the sum of all natural numbers less than n.

For example, evens_and_odds(4) should return

{'evens': 2, 'odds': 4}

Use the following shell:

def evens_and_odds(n: int) -> dict:
    """
    Some docstrings.
    """

    return None
Caution

Do not import any packages to do this.

Exercise 3

Please write a function called time_diff that takes as arguments two strings in the format ‘YYYY-MM-DD’ and a keyword out dictating the output. If the keyword is “float”, return the time between the two dates (in absolute value) in days. If the keyword is “string”, return “There are XX days between the two dates”. If not specified, the out keyword should be assumed to be “float”. You should use the datetime package, and no others.

For example, time_diff('2020-01-01', '2020-01-02', 'float') should return

1

For example, time_diff('2020-01-03', '2020-01-01', 'string') should return

"There are 2 days between the two dates"

Use the following shell:

from typing import Union

def time_diff(date_1: str, date_2: str, out: str) -> Union[str,float]:
    """
    Some docstrings.
    """

    return None

Exercise 4

Please write a function called reverse that takes as its argument a list and returns a list of the arguments in reverse order (do not use any built-in sorting methods).

For example, reverse(['a', 'b', 'c']) should return

['c', 'b', 'a']

Use the following shell:

def reverse(in_list: list) -> list:
    """
    Some docstrings.
    """

    return None

Exercise 5

Write a function called prob_k_heads that takes as its arguments natural numbers n and k with n>k and returns the probability of getting k heads from n flips3.

For example, prob_k_heads(1,1) should return

.5

Use the following shell:

def prob_k_heads(n: int, k: int) -> float:
    """
    Some docstrings.
    """

    return None
Caution

Do not import any packages to do this.

Footnotes

  1. Feel free to use our JupyterHub, where these packages are installed. Just remember that you should submit a .py file and not a .ipynb file.↩︎

  2. Remember that you can do this by typing pip3 install <packagename> into a terminal window↩︎

  3. Hint: this is a binomial distribution.↩︎