Assignment 3

Due Mon, July 29

Please create a single zip file named <your_name>-hw3.zip (i.e. paul-hw3.zip) that contains these files:
hw3-1.py (the solution to question 1)
hw3-2.py (the solution to question 2)
hw3-3.py (the solution to question 3)

Email your solutions to zschneider <at> berkeley.edu. Include comments in each script stating your name, the name of any collaborators, and approximate time it took you to complete the exercise.


1. Matrix Rotation

Tuples can be used to represent a 2x2 matrix. First, place each row in a tuple, then create a third tuple that contains the top row and then the bottom row. For example, the matrix,

1 2
3 4

should be expressed as the tuple, ((1,2), (3,4)). Write a function that takes a 2x2 matrix in this format and rotates it 90 degrees counter-clockwise. Please name your function exactly as below:

def rotate_90(matrix):
"""Takes a 2x2 matrix in tuple format and returns the matrix rotated 90 degrees counter-clockwise"""

For example, rotating the matrix above should give

2 4
1 3

Your function should return the rotated matrix in the same tuple format, so this example should be returned as the tuple, ((2,4), (1,3)).


2. String Rearrangement

a) Write a function that takes two strings and returns True if they are anagrams – that is, if rearranging the letters of one string can transform it into the other. Name your function as follows:

def are_anagrams(str_1, str_2):
"""returns True or False (boolean value) whether two strings are anagrams"""

For this problem, use s_str.lower() to compare lowercase letters. Hint: sorted(s_str) returns a list of the characters in s_str in alphabetical order.

b) Write a second function that takes two strings as arguments, changes them to lowercase, turns them into lists, and returns true if they are the same string, but backwards. Name your function as follows:

def is_backwards(str_1, str_2):
"""returns True or False (boolean value) whether two strings are the same but backwards (ignores case)"""


3. Message Scramble

In this exercise, you will implement a simple encryption algorithm, one that rearranges the letters in a message. While this algorithm will make the message difficult to read, it provides little actual security and should never be used for sensitive information.

a. Given two messages, A and B, which have the same length, we can create a new message by taking the first character from A, then the first character from B, then the second character from A, then the second character from B, and so on. We’ll call this the interleave of A and B.

For example, if A is the text “abcde” and B is the text “12345” the interleave of A and B is “a1b2c3d4e5”.

Write a function, interleave(), that takes two strings of the same length and returns the interleave of the two.

b. To make a message even harder to read, we can perform several interleaves in a row. Assume that the length of a message is a power of 2. We can define the scramble of the message recursively as follows:

  1. The scramble of a single character is just that character.
  2. The scramble of a longer message is found by taking the scramble of the first half of the message and the scramble of the second half of the message, and interleaving them.
For example, the scramble of “12” is the scramble of “1”, which is “1”, interleaved with the scramble of “2”, which is “2”. The result is simply “12”.

The scramble of “1234” is the interleave of the scramble of “12”, which is “12”, and the scramble of “34”, which is similarly “34”. The result is “1324”.

The scramble of “12345678” can be similarly computed as “15372648".

Write a program that prompts the user for a message. Add spaces to the end of the message until its length is a power of 2. Then compute the scramble of the message and display it.

Once your scrambling program works, amend it so that it scrambles a message, and then computes the scramble of the result and displays that as well. How much more security does this create?