Homework 3 (due 4/23)
CSC 241

We have talked about user-defined functions (3.3), iteration (3.2), types and objects (2.4), and parameter passing (3.5). Next week, we'll talk more about basic types, lists, strings and formatted output (4.1, 4.2), files (Section 4.3), and exceptions (Section 4.4), before we'll discuss some basic programming patterns (Chapter 5).

Submission: The homework is due by midnight (I will not accept late homeworks). You can submit your homework through d2l into the drop-box for this homework. Please prepare your homework as a single file containing all answers (e.g. doc, docx, or pdf, not a zip file).

When submitting programs, include the programs, together with screenshots of test-runs of your program (make sure screenshots are sized so the text is legible; resize and/or crop the images).

1. (Reading Assignment) Read Sections 2.4, and 3.2-3.5. If you want to read ahead, check out 4.1-4.3. of the textbook.

2. (Functions, 15pt) Write code for the following functions. Include screenshots of code and test-runs of your function.

  1. [7pt] Write a function triangleArea(a,b,c) that computes and return the area of a triangle given the lengths of its sides a,b,c. Hint: by Heron's formula, the area is

    where s = (a+b+c)/2.

  2. [8pt] Write a function password_check(newpassword, oldpassword) that has two parameters, newpassword and oldpassword and that accepts the password (returns True) if the newpassword is different from the oldpassword and the newpassword is at least 8 letters long. If the new password fails the check, your function should return False.

3. (More functions, 10pt)  Write a functions powers(lb, ub) which prints all the powers of 2 from 2**lb to 2**ub in the following form:

Note: lb and ub are short for lower bound and upper bound; the upper bound here is inclusive.

4. (More Pig-latin, 15pt)

a) [10pt] Write a function first_vowel(word) which returns the index of the first vowel in word. If word does not contain a vowel, return -1. You can assume that all letters are lower case.

Hint: write an indexed loop. If you know the .index method, great, but you're not allowed to use it.

b) [5pt] Using the function first_vowel(word) you've written in 4a, implement an updated version pl(word)  of your pig-latin program, this time as a function which takes word as an argument. The new rules are as follows: if the word starst with a vowel, we still add 'yay' at the end. Otherwise, we take all the consonants up to the first vowel, and move them to the end of the word, and then append 'ay'. E.g. 'proper' becomes 'operpray'. If the word does not contain any vowels, keep the word unchanged. The pig-latin version of word should be returned, not printed.

Note: 'y' is typically counted as a vowel, but up to you. Note 2: If you are not able to solve 4a, then for 4b work with the following implementation for first_vowel(word), and test pl(word) only with words whose first vowel is 'e', say 'preen', or 'even'.

Marcus Schaefer
Last updated: April 17th, 2019.