﻿ CS 111: Exercises

2016 September 20,

# CS 111: Assignment: Exercises

Finish all of the Getting Started with Python tutorials before attempting this homework. This assignment consists of five programming exercises, to solidify the skills that you learned in those tutorials. You are encouraged, but not required, to work with one other student. Your work is due at 11:59 on Tuesday night. Instructions for submitting your work can be found at the end of the assignment.

## isOdd

Question A: Write a function isOdd that takes an integer as input and returns a Boolean as output. It returns True if the input is odd and False if the input is even. (I predict that your solution will be 5 lines of code. Try to get it down to 2 lines, but don't fret if you can't.)

## isPrime

In the following exercise, "number" means "integer greater than 1". A number is called composite if it is evenly divisible (with 0 remainder) by some other number. A number is called prime if it is not composite. For example, 15 is composite, because it is divisible by 3, while 17 and 19 are prime.

Question B: Write a function isPrime that takes an integer as input and returns a Boolean as output. It returns True if the input is prime and False if the input is composite.

## titleCase

In the following exercise, title case refers to a way of capitalizing strings, so that the first letter in each word is upper-case and the remaining letters are lower-case. For example, "What's Going On" and "Svefn-g-englar Olsen Olsen" are title-case, while "exile in guyville" and "MEAT Dress" are not. (In English-language titles it is also common to de-capitalize small, common words such as "the" and "of". To keep the exercise simple, let's ignore that issue.)

Question C: Write a function titleCase that takes a string as input and returns a string as output. The output string is the title-case version of the input string.

## dot

I just realized that we've never written a function that takes more than one input. Fortunately it's not hard to explain. For example, here's a function that takes two numbers (integers or floating-point) as input, and returns the distance between them on the real number line.

```def dist(x, y):
return abs(x - y)

dist(5, 9)
dist(3, -13)```

In the following exercise, a vector is a list of numbers. The length of the list is called the dimension of the vector. For example, [2, -1.2, 5.0] is a 3-dimensional vector and [-7.1, -1.1, 0.0, 0.0, 3.0] is a 5-dimensional vector.

A basic mathematical operation with vectors (that I use millions of times per day) is the dot product. It's easiest to explain with a couple of examples. Suppose that

```v = [2, 5, -1]
w = [4, 3, 3]```

To compute the dot product, we first multiply those vectors entry-by-entry, to get [8, 15, -3]. Then we add up those numbers to get 8 + 15 + -3, which equals 20. The answer is 20. Similarly, the dot product of the following two vectors is 23. Make sure you understand how, before attempting the exercise.

```x = [0, -7, 2, 2, 1, 4]
y = [12, 2, 3, 1, 5, 6]```

Question D: Write a function dot that takes two vectors as input and returns a number as output. The output number is the dot product of the two vectors. Your code may assume that the two vectors are of the same dimension, but your function should work regardless of what that dimension is.

## Varying paul

For any integer k, we can define a modified paul function as follows:

```def paul(n, k):
if n % 2 == 0:
return n // 2
else:
return 3 * n + k```

So, to be clear, the original paul function is the case where k = 1. We're already learned that k = 1 produces very complicated behavior, that is not completely understood.

Question E: Try several other small values of k — positive, negative, and zero. For which k does the monologue seem to end always? For which k does it seem to end never? Write a short report describing your findings. Hint: All of the cases where k is even are similar to each other. How and why?