Introduction to Computer Science I
Winter 2022 Midterm Exam Practice
Implement in midtermPractice.py
the functions described below.
1. [25pts] Write function season()
that takes as input the number of a month (1-12) and returns
the season corresponding to the month, as a string. You may assume
that numbers 1 to 3 correspond to Winter, 4 to 6 correspond to
Spring, 7 to 9 correspond to Summer, and 10 to 12 correspond to
Fall.
>>> season(3)
'Winter'
>>> season(11)
'Fall'
2. [25pts] You can turn a word into pig-Latin
using the following two rules (simplified):
- If the word starts with a consonant, move that letter to the
end and append 'ay'. For example, 'happy'
becomes 'appyhay' and 'pencil' becomes 'encilpay'.
- If the word starts with a vowel, simply append 'way'
to the end of the word. For example, 'enter' becomes 'enterway'
and 'other' becomes 'otherway'. For our
purposes, there are 5 vowels: a, e, i, o, u (so we count y as a
consonant).
Write a function pig() that takes a word (i.e., a string)
as input and returns its pig-Latin form. Your function
should still work if the input word contains upper case characters.
Your output should always be lower case however.
Usage:
>>> pig('happy')
'appyhay'
>>> pig('Enter')
'enterway'
3. [20 pts] Write function quote() that takes as input
the name of a file (as a string) and an index i (as an integer). The
file will contain quotes, one per line. The function should open the
file, read the file, close the file, and return the i-th quote (i.e. the i-th line in the
file), assuming that the quote numbering starts at 0. Test your
solution on file Wilde_Quotes.txt.
Usage:
>>>
quote("Wilde_Quotes.txt", 5)
'At twilight, nature is not
without loveliness, though perhaps its chief use is to
illustrate quotations from the poets.\n'
>>>
quote("Wilde_Quotes.txt", 0)
'A little sincerity is a
dangerous thing, and a great deal of it is absolutely fatal.\n'
>>>
quote("Wilde_Quotes.txt", 23)
'Patriotism is the virtue of
the vicious. \n'
4. [20pts] In this problem you will
implement a function that will find the hidden message in a file.
The hidden message can be reconstructed from every 10th character in
the file. Implement function decrypt() that takes a
filename as input and returns a string that is the
decryption of its content. You should test your implementation on
file ciphertext.txt.
>>> decrypt('ciphertext.txt')
'You got it.'
5. [10pts] Write a function crypto()
that takes as an input a string s and returns a copy of s
modified as follows: split the text up into blocks of two letters
each and swap each pair of letters (where spaces/punctuation, etc.
is treated like letters). If the string has odd length then leave
the last letter as is.
>>> crypto('Secret Message')
'eSrcteM seaseg'
>>> crypto('Secret Messages')
'eSrcteM seasegs'