CPSC150A
Scientific Computing

Activity 10

Traversal

Substring

Write the function is_substring(word_1, word_2). The function should use the in operator to determine if the string word_1 is a substring of word_2. The string word_1 is a substring of word_2 if all of the characters of word_1 exist sequentially inside of word_2.

Example

Test Code Output
print(is_substring(“lend me your ears”, “me”)) True
print(is_substring(“lend me your ears”, “you”)) True
print(is_substring(“lend me your ears”, “you’re”)) False

Reverse

Write the function reverse(text). The function should use the for character in word style of loop to reverse the characters in text.

Example

Test Code Output
print(reverse(‘hello’)) olleh
print(reverse(‘radar’)) radar

Caesar

Most of us like to keep secrets. For example, I don’t want you to know my credit card number. I’d rather you not be able to buy things with my money. However, communication over the Internet is like trying to shout in a crowded room. Everyone can hear what you are saying. So, to protect our valuable secrets, we rely on encryption schemes. Today, you are going to implement a simplistic encryption scheme known since Roman times: Caesar cipher.

Encryption in the Caesar schema relies on shifting letters in the alphabet. By default, this shift is 3 letters. So, a becomes d, b becomes e, etc. The only issue with this is at the end of the alphabet. The end of the alphabet wraps around to the front. So, x becomes a, y becomes b, and z becomes c.

Details

Write two functions encrypt(text) and decrypt(text) that take a single string as a parameter. The encrypt function should return the encryption of the text parameter using Ceasar cipher. The decrypt function should return the decription of the text parameter using the Ceasar cipher.

Example

Test Code Output
print(encrypt(‘hello’)) khoor
print(encrypt(‘goodbye’)) jrrgebh
print(decrypt(‘khoor’)) hello
print(decrypt(‘jrrgebh’)) goodbye

Hint

  • The ord function gives you the ASCII representation of a character.
  • Test if shifted characters are no longer in the range a-z, by comparing their ordinal value to the ordinal value of z.
  • Shift characters that are outside of the range a-z by substracting enough to shift them back into the correct range.
  • The chr function takes an integer, and returns to you the character that represents the ASCII value.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only programs that have perfect functionality will be accepted as complete.