Problem Set 1

Problem 0

Problem 0 (20 pts)

Write a function underliner that takes as input a string and prints out an ‘underlined’ version of that string as follows:

  • For every character print a corresponding - (dash) character on the next line under that character.
  • You may assume the input string is a single line (no \n newline characters)

Examples:

Calling underliner("Carl's Diner") should yield:

Carl's Diner
------------

Calling underliner("the quick brown fox") should yield:

the quick brown fox
-------------------

Use this starter code:

def underliner(in_str):
    print(in_str)
    print("-")
    # this function doesn't work yet

Submit as underliner.py.

  • Python’s printed output to the console does not recognize “above” and “under”. It is sequential output, line by line.
  • The dashes underlining the input string form a string of the same length as the input string.

Problem 1

Problem 1 (30 pts)

Write a function check_prime takes as input an integer and returns 1 if the integer is prime and 0 otherwise. (Prime numbers are numbers that are evenly divisible only by themselves and by 1.)

Starter code:

def check_prime(x):
    # your code here

Submit as check_prime.py.

  • In Python, one can check if x is evenly divisible by y by evaluating x % y == 0. If True, x is evenly divisible by y.

Problem 2

Problem 2 (70 pts)

Write a function second_shortest that takes as input a list of strings and returns the second-shortest string length.

Starter code:

def second_shortest(str_list):
    second_shortest_string = 0
    # your code goes here
    return second_shortest_string

Some example output:

second_shortest(['cat', 'mouse', 'dog', 'house', 'bicycle']) should return 5.

second_shortest(['a', 'b', 'cc', 'd', 'ee']) should return 2.

Submit as second_shortest.py.

Problem 3

Problem 3 (30 pts)

Write a function fibonacci_list that takes as input an integer and returns an ordered list of the Fibonacci sequence less than that integer.

You may assume the integer argument will always be greater than or equal to 2.

Some starter code:

def fibonacci_list(maximum):
    sequence = [1, 1]
    next = 2
    return sequence

Submit as fibonacci_list.py.

Problem 4

Problem 4 (50 pts)

Write a function common_char that takes as input a string and returns the character most often present in the string. If two or more characters are tied for most-frequent, you may return any one of them.

Examples:

  • common_char("apple") would return p
  • common_char("cats and dogs") would return a or s or (space) or d, but it would only return one of these.
  • common_char("Market Gardener") would return e.

Submit as common_char.py.

Problem 5

Problem 5 (30 pts)

A scientific instrument collects values for an experiment. It occasionally collects an erroneous value, but we understand that when the instrument has such an error, the value is always much too large.

Write a function identify_outliers that takes as input a list of numbers (they could be ints or floats) and returns a list of “outlying values,” which are defined as values greater than 10 times the average. All of the numbers will be greater than 0.

As an example, with data = [0.005, 0.02, 1.5, 0.03, 0.01, 0.01, 0.021, 0.01, 0.028, 0.031, 0.003, 0.016], identify_outliers(data) returns list [1.5].

With data = [1, 2, 3, 4, 5], identify_outliers(data) returns an empty list.

Submit as identify_outliers.py.