CMU 15-110 Fall 2018: Principles of Computing
Homework 4 (Due Tuesday 2-Oct at 8pm)
- Team vs Solo:
As usual, this hw has a collaborate team portion, and a solo portion. Please see the syllabus for details. - Starter code:
To start the solo hw, download this file: hw4.py Then, edit that file you downloaded!
Team Hw4
- This part of the hw is team-based and collaborative.
- Here are the topics and activities we covered:
- Collaborative Code Tracing
- Collaborative Coding
- Word Guessing Game
- Google Play Store (Data Analysis)
- Team-Based Distributed Sorting (with playing cards)
Solo Hw4
- This part of the hw is solo and not collaborative. See the syllabus for details.
- vowelCount(s) [15 pts]
Write the function vowelCount(s), that takes a string s, and returns the number of vowels in s, ignoring case, so "A" and "a" are both vowels. The vowels are "a", "e", "i", "o", and "u". So, for example, ("Abc def!!! a? yzyzyz!") returns 3 (two a's and one e). - interleave(s1, s2) [15 pts]
Write the function interleave(s1, s2) that takes two strings, s1 and s2, and interleaves their characters starting with the first character in s1. For example, interleave('pto', 'yhn') would return the string "python". If one string is longer than the other, concatenate the rest of the remaining string onto the end of the new string. For example ('a#', 'cD!f2') would return the string "ac#D!f2". Assume that both s1 and s2 will always be strings. - largestNumber(text) [20 pts]
largestNumber: Write the function largestNumber(text) that takes a string of text and returns the largest int value that occurs within that text, or None if no such value occurs. You may assume that the only numbers in the text are non-negative integers and that numbers are always composed of consecutive digits (without commas, for example). For example:largestNumber("I saw 3 dogs, 17 cats, and 14 cows!")
returns 17 (the int value 17, not the string "17"). AndlargestNumber("One person ate two hot dogs!")
returns None (the value None, not the string "None"). - Bonus/Optional: Simple Encryption [3 pts]
Background: Here we will implement a simple password-based encryption scheme. First, we will start with a plaintext (not encrypted) message, say "Go team!". We will ignore all the non-letters and convert the letters to uppercase, giving us "GOTEAM". That is the message we will encrypt. Next, we need a password, which we assume is all lowercase letters, say "azby". We will think of each letter in the password as a shift, where a is 0 and z is 25, so "azby" specifies the shifts 0, 25, 1, 24 in order. We apply these shifts to the letters of the plaintext message, with wraparound (so after "Z" we go back to "A"). So we will shift "G" by 0 (resulting in "G"), "O" by 25 (resulting in "N"), "T" by 1 (resulting in "U"), and "E" by 24 (resulting in "C"). Then we run out of password characters, so we start over from the beginning of the password. So we shift "A" by 0 (resulting in "A") and "M" by 25 (resulting in "L"). Hence, encrypting "Go team!" with the password "azby" produces the ciphertext (that is, the encrypted text) "GNUCAL".
- encrypt [2 pts]
With the explanation above in mind, write the function encrypt(plaintext, password) that takes two strings, a plaintext message and a password, and which returns the encrypted string that results from the process described above. If the password is not all-lowercase, just return the string "password must be all lowercase".
Big Hint: use ord(c) to convert a character to its ASCII/Unicode value. Use chr(v) to go the other way, converting ASCII/Unicode back into a character. So: ord("A") is 65, and chr(65) is "A". And chr(ord("A")+1) is "B".
- decrypt [1 pt]
Now write the other necessary part of any encryption scheme: decryption! Specifically, write the function decrypt(ciphertext, password) that takes two strings, a ciphertext message that was encrypted as described above, and a password. This function returns the original all-caps message that was encrypted. So, for example, decrypt("GNUCAL", "azby") should return "GOTEAM". Hint: decryption is very similar to encryption (which is why it is not worth so many points).
- encrypt [2 pts]