10 Jun Python3 codes to complete
CMPS 5P Assignment 3 Spring 19
Instructions
1. The aim of this assignment is to practice Selection, Control Flows(Functions, Recursion, break), Strings and use APIs in Python.
2. The deadline for the assignment is 05/06/2019 (Monday) 11:59 PM.
3. The grade of the assignment is out of 100 pts with 5 questions.
4. Students must use Python3 (NOT Python2) to write the code.
5. Don’t share code or get code from the internet, but working together is encouraged. If you choose to work with someone you must mention them in one of the first comments in the file.
6. Students are expected to write the code that is easily readable. To this end, they must employ meaningful variable names and comment their code properly.
7. The output of the code requires to be precisely as shown in the sample runs.
8. Throughout the assignment ”” indicate strings, [] indicate lists and True/False indicates bool. It is the student’s responsibility to resolve for themselves any ambiguity in the assignment or instructions. Please use Piazza to get questions clarified.
9. Students must put the code in a single file named FirstName LastName StudentID HW3.py and submit it through Canvas. DO NOT zip your submission. Zip is more likely used when there is more than one file submission. Please notice that failing to com- ply with this particular requirement will result in 0 points for the entire assignment.
10. Each question has a function name already given. For the sake of ease in grading please use the exact same name for your function. Inconsistent function names will be treated as question unanswered which will fetch 0 points for that question.
11. It is important that you practice commenting your code, so each file submitted without comments will have 5 points deducted from its score (even if the program works perfectly)!
12. Each solution will be run against multiple test cases. The percentage of test cases passed by your solution will factor into your score.
13. Use the skeleton code provided at Question 5 for your submission. You can start bottom up by having this skeleton in place and then solving each of the individual questions or top down by solving the individual questions first and then combining them to the overall submission.
1
CMPS 5P Assignment 3 Spring 19
1 Position queries – 15 points
Write a function that is named positionQueries that takes 2 lists as input such that the first list named data contains a list of unsorted integers and the second list named order contains position queries. Order denotes the position of an element in the data list if it were sorted. Thus, though the order elements can contain any value(as described below) valid elements in order list cannot be less than 1 or greater than the length of the data list. The output will be the list of integers in the data list corresponding to the order of elements. If the input is bad print the message ”bad input” and return to solve the next question.
Examples:
Test Case 1: input: data = [-1, 2, 5, 0, 3], order = [1, 5] output: [-1,5] Explanation: The 1st or the smallest element in the list is -1. The 5th or the largest element in the list is 5. So we return a list of [-1, 5] in place of the queries [1,5].
Test Case 2: input: data = [1, 1, 1, 1, 1, 1], order = [1,3,5] output: [1,1,1] Explanation: Though all the elements in the list are the same, if these numbers were or- dered in ascending order then the 1st, 3rd, 5th positions would still be occupied by 1s.
Test Case 3: input: data = [], order = [1, 2, 3] output: ”bad input” Explanation: There are no elements in data. Therefore, it is not possible to find the 3rd
largest element. Hence, we return bad input.
Test Case 4: input: data = [-3, 1, 1, 3.8, 1, 9999], order = [1,2,2,5] output: [-3, 1, 1, 3.8]
Test Case 5: input: data = [1,2], order = [1, 2, 3] output: ”bad input”
Test Case 6: input: data = [1, 2, 8], order = []
2
CMPS 5P Assignment 3 Spring 19
output: []
2 The Round Trip – 15 points
A certain robot can perform only 4 types of movement. It can move either up or down or left or right. These movements are represented by ’U’, ’D’, ’L’, ’R’. Assume all these movements to be of unit distance. Write a function named theRoundTrip that takes all the movements the robot made in a day as input and output True of bool type if the robot returned to its starting position after its journey. Otherwise return False. If the input is bad print the message ”bad input” and return to solve the next question.
Examples: Test Case 1: input: movement = [’U’, ’L’, ’D’, ’R’] output: True Explanation: In a 2D plane a unit movement up by 1, left by 1, down by 1 and right by 1 would result in arriving at the starting posistion. So we return True.
Test Case 2: input: movement = [’U’, ’U’, ’R’, ’L’, ’D’, ’R’] output: False Explanation: After traversing this movement we see that we have not arrived at the start- ing position. So we return False.
Test Case 3: input: movement = [’U’, ’U’, ’I’, ’L’, ’D’, ’R’] output: bad input
Note: There are at least 2 ways to solve this. Can you think of both?
3 The Secret Key – 20 points
You are in pursuit of a treasure of great value. You just found the treasure chest but it is locked with 2 keyholes. Luckily, you had guessed this in the beginning of your quest and brought with you a bunch of keys. Each of the keys in your bunch seem to have an integer value associated with it. Now, the chest is designed in such a way that any 2 keys that can combine (arithmetic sum) to generate the secret key value can open the chest. You start plugging in all sorts of
3
CMPS 5P Assignment 3 Spring 19
combination of your keys in the 2 keyholes of the chest to check if you are lucky before you walk away in despair. Write a function called isSecretKey that tells you if you will be able to retrieve the treasure given your key bunch and the secret key value. True of type bool indicates you can retrieve the treasure. False means otherwise. Examples: Test Case 1: input: sub keys = [1, 2, 3, 4], secret key = 5 output: True Explanation: There is at least one pair (1,4) in the list of sub keys that can combine to generate the secret key 5. Hence we can open the chest.
Test Case 2: input: sub keys = [7, 8, 1, 2, 4], secret key = 16 output: False Explanation: No 2 sub keys can combine to generate the secret key. Hence, we cannot open the chest.
4 The Alien Language – 30 points
You are given a rare document that we suspect is written in an alien language. The alien language has the following grammar.
1. All its words read the same backward as well as forward. E.g. aba. A sequence of these words are written as sentences.
2. It is composed of only English alphanumeric([a-z, A-Z, 0-9]) characters in it.
3. It has exactly one space between each word.
4. It is case insensitive. i.e. aba is the same word as abA.
Write a function isAlienWord that takes an input word and returns if it is alien or not. Then, write another function called isAlienLanguage that calls the isAlienWord function for each word of its sentence and returns if the entire document is alien or not. For the document to be alien each and every single word in the document has to alien. True of type bool indicates the document is alien False means otherwise.
Examples: Test Case 1: input: document = ”a ma” output: False
4
CMPS 5P Assignment 3 Spring 19
Explanation: Though the string ”ama” is a valid alien language ”a ma” is not.
Test Case 2: input: document = ”aba mom roor tet jasttsaj pillip” output: True Explanation: All the words in the document are alien. Hence, the language is alien. Return true.
Note: The function isAlienWord must be implemented recursively. Failure to do so will fetch 0 points for this question.
5 Submission Logistics – 20 points
Combine all your answers into a single program. Have a main function from which each of the answer to questions are retrieved and run the program as a whole. Name this file in the FirstName LastName StudentID HW3.py format and submit your code on Canvas.
The skeleton code is given below. This is how your file should look like on submission. The angle brackets 〈comment〉 with a short description of its contents indicate that appropriate code snippets need to get replaced at its position based on the comments.
5
CMPS 5P Assignment 3 Spring 19
1 Skeleton code
A good idea to get started on your assignment is to reproduce the code below in your favourite code editor and start filling up the functions.
# Question 1 and comments about your function go here
def positionQueries(<Enumerate your parameters here>):
# Your answer goes here
return <your answer>
# Question 2 and comments about your function go here
def theRoundTrip(<Enumerate your parameters here>):
# Your answer goes here
return <your answer>
# Question 3 and comments about your function go here
def isSecretKey(<Enumerate your parameters here>):
# Your answer goes here
return <your answer>
# Question 4 and comments about your function go here
def isAlienLanguage(<Enumerate your parameters here>):
# Your answer goes here
# recursive function to test if a word is a Alien
def isAlienWord(<list your parameter here>):
return <your sub answer>
return <your final answer>
# Question 5 Combining your answers
def main():
results = list()
results.append(positionQueries([1, 2, 5, 0, 3], [1, 2]))
results.append(theRoundTrip([‘U’, ‘R’, ‘L’, ‘D’]))
results.append(isSecretKey([1, 2, 6, 7, 9], 3))
results.append(isAlienLanguage(“aabbbaa mom”))
for i in results:
print(i)
# Clean entry point to your code
if (__name__ == ‘__main__’):
main()
Our website has a team of professional writers who can help you write any of your homework. They will write your papers from scratch. We also have a team of editors just to make sure all papers are of HIGH QUALITY & PLAGIARISM FREE. To make an Order you only need to click Ask A Question and we will direct you to our Order Page at WriteDemy. Then fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Fill in all the assignment paper details that are required in the order form with the standard information being the page count, deadline, academic level and type of paper. It is advisable to have this information at hand so that you can quickly fill in the necessary information needed in the form for the essay writer to be immediately assigned to your writing project. Make payment for the custom essay order to enable us to assign a suitable writer to your order. Payments are made through Paypal on a secured billing page. Finally, sit back and relax.
About Writedemy
We are a professional paper writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework. We offer HIGH QUALITY & PLAGIARISM FREE Papers.
How It Works
To make an Order you only need to click on “Order Now” and we will direct you to our Order Page. Fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Are there Discounts?
All new clients are eligible for 20% off in their first Order. Our payment method is safe and secure.
