Python代写 | CMPUT 101 Assignment 2 Winter 2020

CMPUT 101 Assignment 2 Winter 2020
Due Date: The due date is Friday, April 3 at 6pm. The assignment is to be submitted online on eclass. Late submissions will not be accepted.
Collaboration Policy: This must be your own work. Do not share or look at Python
code of other people (whether they are inside or outside the class). Do not search for or
copy code from the Internet. You can talk to others that are in the class about solution
ideas (but not so detailed that you are actually verbally sharing or hearing about or seeing
code). You must cite who you talked to in the comments of your programs.
Submission: You need to submit 2 files: and on eclass. Use the
program “stubs” of these names provided on eclass. Your submitted solutions must be in
the correct .py format so that the TAs can run the programs. Keep the file names the
same as listed above. Do not submit pdf or doc or other incorrectly formatted files.
Remember to leave your programs on your lab account before the due date (in case
something goes wrong with your submission).
Marking: The assignment is worth 9% of your final grade. Marks are allocated for each
question as follows:
− 10% Describe your program in a comment at the top of the solution
− 20% Program syntax is correct (Python reports no errors), program file can be run by
the TAs without error messages
− 50% Program works
− 20% Coding style (meaningful variable names, proper indentations, comments in code)
In this assignment, you must use the subset of Python that we have discussed in class.
In particular, you are not allowed to use the following built-in functions of Python: count,
replace, join, split, find, sort, strip, clear, remove, pop, del.
Question 1 [10 marks]:
Write a function find_valuepos(Alist, base) that returns the position of the largest value
in the list Alist and this largest value is smaller than base. The function will return -1 if
there is no value smaller than base. Note, do not sort this list. You should also be able to
solve this problem going through the list only once.
Use this function in a main program as follows. First your main program should assign
values to a list. For example,
L = [4, 1, 52, 17, 81, 2, 75, 24]
The main program should then print the list. Then the main program should call the
above function appropriately in order to print the largest numbers from the list that are
smaller than 100, 75, 50, 25 and 0 respectively using the format below. If there is no
value smaller than one or more of these numbers, then the program will print an
appropriate message as indicated below. Remember that the TAs will potentially change
the list (and its size) in order to test your program so your program must be general for
different list values and sizes.
For the example list given above, the output should be the following:
[4, 1, 52, 17, 81, 2, 75, 24]
The largest number smaller than 100 is 81
The largest number smaller than 75 is 52
The largest number smaller than 50 is 24
The largest number smaller than 25 is 24
There are no numbers smaller than 0
Another example if L is assigned values as follows
L = [1, -3, 65, 84, 39, 13, 5]
The output would be:
[1, -3, 65, 84, 39, 13, 5]
The largest number smaller than 100 is 84
The largest number smaller than 75 is 65
The largest number smaller than 50 is 39
The largest number smaller than 25 is 13
The largest number smaller than 0 is -3
Question 2 [10 marks]:
Encryption is used to keep information private. It involves transforming text into new
“unreadable” text by changing the letters in some way using a key. The goal is to do this
in such a way that someone else who has the key can decrypt the information. In this
question, you will write a program to encrypt and decrypt a string using a simple
substitution cipher.

The program will start by setting up an original alphabet string, for example
”abcdefghijklmnopqrstuvwxyz ” (note there is a space character at the end of this string
so you can create a sentence or phrase). This will be assigned in the main program (see
Next, the program will input a key string. This will be used to create a new cipher
alphabet of the same size as the alphabet string. Note the key must contain characters
that are in the alphabet and should not contain duplicate letters. (You can assume the user
of the program will provide a valid key). Your program must create the cipher alphabet
string. The cipher alphabet string will consist of the characters of the key at the start
followed by the characters of the original alphabet string where the characters of the key
have been removed.
So for example, if the original alphabet is
”abcdefghijklmnopqrstuvwxyz ” and the key is ”zebra”,
the cipher alphabet string will be ”zebracdfghijklmnopqstuvwxy ”
Note the first part of the cipher alphabet is the key ”zebra” followed by
”acdfghijklmnopqstuvwxy ” (the latter is the original alphabet with the letters of the key
At this point, you have the original alphabet string and you have a new cipher alphabet
string that are of the same size.
Next, the program will input a phrase (another string) to be encrypted. The characters in
the phrase must be characters from the original alphabet. You can assume that the user of
the program will enter a valid phrase. Your program needs to encrypt this string. This
is done by creating a new string where each character of the phrase is replaced by another
character based on a mapping of the original alphabet and the cipher alphabet. In
particular, for each character of the phrase, the program will find the position of that
character in the original alphabet string and, in the new encrypted phrase, replace it with
the character that is in the same position in the cipher alphabet.
In the example above, suppose the character in the unencrypted phrase is “b”, where “b”
is in the original alphabet at position 1 (remember positions start at 0). In the encrypted
phrase, this character will be replaced with “e” which is at position 1 in the cipher
alphabet. If the character to be encrypted is “h”, this letter is in position 7 in the original
alphabet. This character is replaced with “f” in the encrypted string because “f” is in
position 7 in the cipher alphabet. See the example below.
Finally decrypt the string back to the original to check that this works (this should be
possible by using your implementation of the encryption function called with the
encrypted phrase and the alphabets in the appropriate order).
Your program should print the original alphabet, the key, the cipher alphabet, the phrase
to be encrypted, the encrypted phrase and the decrypted phrase.
original alphabet: ”abcdefghijklmnopqrstuvwxyz ” (assigned in main program)
key: “zebra” (input)
cipher alphabet: ”zebracdfghijklmnopqstuvwxy ” (created in the program)
phrase: ”hello world” (input)
encrypted phrase: ”fajjm vmpjr” (created in the program)
decrypted phrase: ”hello world” (created in the program by reversing the process)
Suggestions for implementation:
Create the following functions (add other functions as needed)
seq_search(str1,ch) will return the position of ch in str1 if the character ch occurs in the
string str1 and -1 if the character ch is not in str1.
create_cipher_alpha(origalpha,keystring) will take in the original alphabet and key and
return the cipher alphabet. The keystring is placed at the start of the cipher alphabet.
origalpha is placed at the end of the alphabet with the characters of keystring removed.
encryptstring(ph, alpha1, alpha2) will encrypt ph by finding the position of each
character of ph in alpha1 and creating a new string with the characters that are at the
same position in alpha2. This new string is returned.
Set up your main program as the following:
#Main Program
alpha = ”abcdefghijklmnopqrstuvwxyz ”
key = input (”Enter the key: ”)
phrase = input (”Enter the phrase to encrypt: ” )
Note that there is a space character at the end of the alphabet string so that you can
include spaces in your phrases. Note also that the TAs may change the alpha string
when they test your program, so make your program general for different alphabets.
They will also test this with different keys and phrases.
Submit all your functions plus the main program in