Course Home | Class Schedule | Assignments | Git Submission | Perusall | Python | Tutoring

Saint Louis University

Computer Science 1300/5001
Introduction to Object-Oriented Programming

Michael Goldwasser

Fall 2018

Computer Science Department

Hands-on Day

User-defined Functions


All of these problems are for practice. You are freely encouraged to work together and you do not need to submit any of this work for a grade.

Each of the following problems asks you to define a new function with the described behavior. To help you, we've created a framework that will automatically test all of your implementations. Download the file functionLab.py and then place all of your function definitions in the first half of that file. You may run the script to test your work (with the file only testing those functions which you've written).


Problems

Functions that return information

  1. Write a function f(n) which returns the value $\frac{n(n+1)}{2}$.

    Spoiler: my code

  2. Write a function sumOfSquares(n) which returns the sum of the first $n$ squares, $1 + 4 + 9 + \cdots + n^2$.

    Spoiler: my code

  3. Write a function with calling signature dramatic(word) that returns a string that has two copies of each letter of the given word. For example, dramatic('argh') should return the string 'aarrgghh'.

    Spoiler: my code

  4. Write a function with calling signature factorial(n) that returns the quantity $n! = n \cdot (n-1) \cdots 2 \cdot 1$. (But do not use the math module's version.)

    Spoiler: my code

  5. Write a function with calling signature countVowels(word) that returns the number of vowels in the given string (where we consider only aeiou as vowels, although allowing upper or lower case.

    Spoiler: my code

  6. Write a function with calling signature pairSum(data, goal) that returns True if there are two distinct values in the given data sequence that add to precisely goal.

    Spoiler: my code

Functions that modify a parameter

  1. Write a function with signature adjust(data, increment), where data is a numeric list and increment is a number. The goal is to modify the contents of the given list by adding the increment to each number.

    For example if data were [3, 8, 5, 2] and you call adjust(data, 10) then after the call you should find that data has contents [13, 18, 15, 12].

    Spoiler: my code

  2. Write a function with signature calibrate(data), where data is a numeric list. The goal is to find the minimum value on the list and then to adjust all numbers by that amount.

    For example if data were [3, 8, 5, 2] and you call calibrate(data) then after the call you should find that data has contents [1, 6, 3, 0].

    Spoiler: my code

  3. The remove function of the list class only removes the first occurrence of a specified value from the list. Write your own function, removeAll(data, val) that removes all occurrences of the given val from the given list. Note well, you are not to produce a new list, but instead are to modify the given list instance.

    Note: there is a relatively easy implementation of this function if relying on other list behaviors, as you can use a while loop that effectively states that while the value occurs on the list, remove the first occurrence of it. It takes more care to implement this without relying so heavily on other list methods.

    Spoiler: my code


Michael Goldwasser
CSCI 1300/5001, Fall 2018
Last modified: Sunday, 14 October 2018
Course Home | Class Schedule | Assignments | Git Submission | Perusall | Python | Tutoring