DSC 40B
Problems tagged with lecture-01

Problems tagged with "lecture-01"

Problem #001

Tags: time complexity, lecture-01

What is the time complexity of the following function?


def foo(arr):
    """`arr` is an array with n elements."""
    x = max(arr) * min(arr)
    if sum(arr) > 10:
        return x
    else:
        return 0

Solution

\(\Theta(n)\)

Problem #002

Tags: time complexity, lecture-01

What is the time complexity of the following function?


def also_the_variance(data):
    """
    computes the variance of `data`
    `data` is a list of size n
    """
    mu = sum(data) / len(data)
    total = 0
    for x in data:
        total += (x - mu)**2
    return total / len(data)

Solution

\(\Theta(n)\). sum(data) takes \(\Theta(n)\) time but is computed only once, before the loop. The loop runs \(n\) times and does constant work on each iteration.

Problem #003

Tags: time complexity, lecture-01

What is the time complexity of the following function?


def variance(data):
    """
    computes the variance of `data`
    `data` is a list of size n
    """
    # compute the variance again
    total = 0
    for x in data:
        mu = sum(data) / len(data)
        total += (x - mu)**2
    return total / len(data)

Solution

\(\Theta(n^2)\). sum(data) takes \(\Theta(n)\) time and is recomputed on each of the \(n\) iterations of the loop.

Problem #004

Tags: time complexity, lecture-01

What is the time complexity of the following function?


def foo(arr):
    """arr is an array of size n"""
    n = len(arr)
    for i in range(n):
        r = sum(arr) * sum(arr)
        print(r)

Solution

\(\Theta(n^2)\). Each iteration of the loop calls sum(arr) twice, and each call takes \(\Theta(n)\) time. The loop runs \(n\) times, so the total is \(\Theta(n^2)\).

Problem #005

Tags: time complexity, lecture-01

Suppose numbers is a list of integers of length \(n\). What is the time complexity of the following function in terms of \(n\)? State your answer using asymptotic notation (e.g., \(\Theta(n)\)) in the simplest terms possible.


def foo(numbers):
    for x in numbers:
        r = max(numbers) * min(numbers) * x
        print(r)

Solution

\(\Theta(n^2)\)

Problem #006

Tags: time complexity, lecture-01

What is the time complexity of the following function in terms of \(n\)?


def foo(arr):
    """`arr` is a list containing n numbers."""
    for x in arr:
        if x > max(arr) / 2:
            print('large!')
        elif x < min(arr) * 2:
            print('small!')
        else:
            print('neither!')

Solution

\(\Theta(n^2)\)

Problem #007

Tags: time complexity, lecture-01

What is the time complexity of the following function in terms of \(n\)?


def foo(arr):
    """`arr` is a list containing n numbers."""
    for x in arr:
        n = len(arr)
        if x > sum(arr) / n:
            print('large!')
        elif x < sum(arr) / n:
            print('small!')
        else:
            print('neither!')

Solution

\(\Theta(n^2)\)