LMU ☀️ CMSI 2130
ALGORITHMS
HOMEWORK #1

Instructions

Turn in all solutions (including those requiring code) neatly typeset on 8.5" × 11" paper, with a staple in the upper left corner.

Store your files in a private GitHub repository named <yourgithubname>/cmsi282. At the top level of this repository create a folder called homework1 in which your files will be placed. During grading, I may clone your repository and run my own test suite on your code, so don’t forget to give me access to your repo. My github name is rtoal. I will be looking for your code in the branch master, of course. Problems that do not involve code should be answered in the file README.md; problems involving code should be placed in properly named files according to the convention of your chosen implementation language (which may be JavaScript, CoffeeScript, Java, or Python).

I encourage you to work in pairs. Please submit only one solution set for your team.

This is due at the beginning of class on Tuesday, February 9, 2016.

Readings, Videos, and Practice

Just read, watch, and do. You do not have to take notes or write reports on them; however, don’t cheat yourself by skipping them. These independent learning units must be done individually, but please feel free to discuss these items with your partner, your friends, or even with the rest of the class on the Discussion Boards.

Problems To Turn In

Please note that this is an unusual assignment because I will sometimes (but not always) be prescribing an approach for a problem, rather than letting you solve it any way you want to.

  1. (Answer in README.md) Dasgupta Problem 0.1
  2. (Answer in README.md) Dasgupta Problem 1.13
  3. (Answer in README.md) Levitin Problem 2.1.5b
  4. (Answer in README.md) Levitin Problem 2.1.10
  5. Evaluate a polynomial a value $x$ using Horner’s rule. Represent polynomials with dictionaries mapping exponents to coefficients, e.g. $3x^5-2x^3+5x-3$ would be {5:3,3:-2,1:5,0:-3}. Use the following templates:
    # Python, in poly.py
    class Polynomial:
        def __init__(self, coefficients):
            self.coefficients = coefficients
        def evaluate(self, x):
            # your code here
    
    // JavaScript, in poly.js
    class Polynomial {
      constructor(coefficients) {
        this.coefficients = coefficients;
      }
      evaluate(x) {
        // your code here
      }
    }
    
    // CoffeeScript, in poly.coffee
    class Polynomial
      constructor: (@coefficients) ->
      evaluate: (x) ->
        # your code here
    }
    
    // Java, in Polynomial.java
    class Polynomial {
        public Polynomial(Map<Integer,Integer> coefficients) {
            this.coefficients = coefficients;
        }
        public double evaluate(double x) {
            // your code here
        }
    }
    
  6. Suppose you were given a list of size $3n$ integers. Write a function to determine whether or not you can partition the list into $n$ triples such that the sum of each triple was the same. For example, given:

    [2,4,8,12,15,2,0,6,3,2,9,1]
    

    the answer is true because you can partition this list as follows:

    [(4,3,9),(15,0,1),(8,2,6),(2,2,12)]
    

    while the answer would be false for:

    [6, -1, 8, 3455, 11, 7]
    

    The answer is also false if the length of the input list is not a multiple of 3. Make sure to correctly handle the empty list and the list with exactly three elements! Use the following templates:

    # Python, in three.py
    def three_partition(numbers):
        # your code here
    
    // JavaScript, in three.js
    function threePartition(numbers) {
      // your code here
    }
    
    // CoffeeScript, in three.coffee
    threePartition = (numbers) ->
      # your code here
    }
    
    // Java, in ThreePartitionSolver.java
    class ThreePartitionSolver {
        public static boolean threePartition(List<Integer> numbers) {
            // your code here
        }
    }
    
  7. (Answer in README.md) Consider the function: $$ C(n, k) = \begin{cases} 1 & \mathrm{if}\;k = 0 \\ 1 & \mathrm{if}\;k = n \\ C(n-1,k)+C(n-1,k-1) & \mathrm{if}\;1 \leq k \leq n-1 \end{cases} $$ If memoization is not used, and the function is implemented directly from the definition, how many function calls are made to compute $C(20,11)$? If memoization is used, how many calls would be made?
  8. Implement a memoized version of the function in the previous problem. Your solution should be in the file c.py, c.js, c.coffee, or CombinationComputerWithMemoization.java.