Assignment 2 COMP 208 Fall 2019
posted: Monday, Oct. 7, 2019
due: Tuesday, Oct. 22, 2019 at 09:00
Primary Learning Objectives
By the end of this assignment, students should be able to
Analyze a question and implement a solution using appropriate programming constructs, including lists, loops, functions and dictionaries.
Translate a mathematical operation into Python code. Submission Instructions
Please store all your files in a folder called Assignment2, zip the folder and then submit the file Assignment2.zip to myCourses. See the instructions on myCourses for more details on how to zip files. Inside your zipped folder, there must be the following files please use these exact file names.
mctavish.py
triples.py
binaryconv.pypolynomials.py
Any deviation from these requirements will result in deduction of points.
Dont worry if you realize you made a mistake after submitting your zip file: you can submit multiple times on myCourses. We encourage you to submit a first version a few days before the deadline computer crashes do happen and myCourses may be overloaded during rush hours.
Penalties
Late assignments will be accepted up to 2 days 48 hours after the due date and will be penalized by 10 points per day. Note that submitting one minute late is the same as submitting 23 hours late. We will deduct 10 points for any student who has to resubmit after the due date i.e. late irrespective of the reason, be it wrong file submitted, wrong file format submitted
1
or any other reason. This policy will hold regardless of whether or not the student can provide proof that the assignment was indeed done on time.
If your program does not work at all, e.g. it gives an error and does not produce any output, you will automatically get zero points for that question. If your program executes without errors but produces incorrect output, partial grades may be awarded based on the correctness of the code.
You are expected to put comments in each file, on average 1 comment for every 2 or 3 lines, explaining what the lines are doing. Failure to comment your code in this manner will result in deduction of points.
You are expected to use descriptive names for your variables whenever possible de pending on the question. Do not use variable names like x, y or z. Instead, use names like user input, sum of numbers, or average value. Failure to give your variables descriptive names will result in deduction of points.
If anything is unclear, it is up to you to clarify it by either directly asking a TA during office hours, or making a post on the myCourses discussion board.
Note: Some students may know more advanced ways in Python to do these questions. Al though it may be more efficient or optimal to use more advanced concepts, it defeats the purpose of the assignment. We would like to have a level playing field for everyone; therefore, please only use the topicsconcepts discussed in the class up to and including October 7 the day assignment2 was posted. Marks may be taken off for assignment solutions that use Python concepts taught after October 7.
You must not use any thirdparty libraries for this assignment.
2
Question 1 20 points
The McTavish weather station, located near the corner of McTavish Street and Docteur Penfield, measures the weather several times over a protracted period. For this question, you must write a program that accepts multiple lines of commaseparated numbers, where each number is a sensor reading. Your program should ask for the readings repeatedly until the user enters stop on a single line. Then, your program should print, on one line, the minimum, maximum and average value of all of the sensor readings, as shown in the examples below. You must print the three float values with only 2 decimal places. See the recent lecture slides on string formatting to learn how to do this.
You must use a loop to ask the user to enter the readings, and use a list to store the readings. When you calculate the minimum, maximum and average of the readings in the list, you are allowed to use builtin functions this time. However, you must not use invalid inputs in your calculation of the minimum, maximum and average. For example, if the user entered 30 readings in total, and 5 of them were invalid, then the minimum, maximum and average must be calculated from the 25 valid readings, ignoring the 5 invalid ones. An invalid input is defined as being either less than 70, or greater than 50 degrees.
If all sensor readings given by the user are invalid, the program must print Really broken sensor! instead of printing the three values.
Filename
You must write this program in a file called mctavish.py.
Examples as executed in Thonny
Example 1:
Run mctavish.py
Enter readings:
Enter readings:
Enter readings:
Temperatures today: maximum was 39.00 degrees, minimum was 30.00 degrees,
average was 7.25 degrees.
17,28,0,2,4,12
38,39,9,29,30,20,9,0,0,3
stop
Example 2:
Run mctavish.py
Enter readings:
Enter readings:
Enter readings:
1030,5
113377,1232
100,200,300,400,500
3
Enter readings:
Enter readings:
Temperatures today: maximum was 15.00 degrees, minimum was 5.00 degrees,
average was 10.00 degrees.
Example 3:
124455,43444,4533,15
stop
Run mctavish.py
Enter readings:
Enter readings:
Enter readings:
Enter readings:
Really broken sensor!
1030,100
42221
110,80
stop
4
Question 2 20 points
Write a program that takes as input an integer n, and print all triples a, b, c such that: 1abcn
abc
c is a prime number
You can assume that the user will enter a value of n greater or equal to 5. Each triple a, b, c should be printed on a single line.
You are allowed to use the code shown in class that checks if a number is prime or not. However, you must not import any Python libraries or their functions for this question.
Filename
You must write this program in a file called triples.py.
Examples as executed in Thonny
Example 1:
Clarification: it is okay if your program prints the lines in different order. Order of lines can be different but output should still match, i.e. all the triples a, b, c that match the three conditions mentioned above must be printed
e.g. the output in the Example 1 above, can be as follows:
Run triples.py Enter n: 10
235
257
347
Run triples.py Enter n: 10
257
235
347
5
Example 2:
Run triples.py Enter n: 14
235
257
2 9 11 2 11 13 347
3 8 11 3 10 13 4 7 11 4 9 13 5 6 11 5 8 13 6 7 13
6
Question 3 20 points
In a computer system, integers are represented in binary as a sequence of bits 0s and 1s. In this question, you will write a program that lets the user convert binary numbers to decimal numbers the base 10 number system, which uses digits from 0 through 9 and viceversa.
You must use the algorithms that we have seen in the firstsecond week of classes to do this see the lecture 2 slides.
1. dec to bindec string should take one argument, dec string, which is a string representing a decimal number, and should return a string with the binary representation of that number i.e., a string with only 0s and 1s.
2. bin to decbin string should take one argument, bin string, which is a string representing a binary number, and should return a string with the decimal representation of that number i.e., a string with digits from 0 through 9. If the bin string argument contains digits other than 0 or 1, then the function should instead return None .
3. menu should take zero arguments. It should ask the user whether they would like to convert from binary to decimal option 1, convert from decimal to binary option 2, or exit the program option 3. If the user selects option 1 or 2, it will ask them to enter a number to convert, then call one of the functions bin to dec or dec to bin depending on the users option, display the return value of the function, and repeat the menu options. If the user selects option 3, the program should print Goodbye and stop. You can assume the user will only input an integer from 1 to 3 for their choice.
You must not import any Python libraries or their functions for this question.
Filename
You must write this program in a file called binaryconv.py.
Examples as executed in Thonny
Example 1:
Run binaryconv.py
Options:
1 Binary to Decimal
2 Decimal to Binary
3 Exit program
Enter your choice: 1
Please enter the binary number: 1000110111 1000110111 in decimal is 567
7
Options:
1 Binary to Decimal
2 Decimal to Binary
3 Exit program
Enter your choice: 2
Please enter the decimal number: 5387 5387 in binary is 1010100001011 Options:
1 Binary to Decimal
2 Decimal to Binary
3 Exit program
Enter your choice: 3
Goodbye.
Example 2:
Run binaryconv.py
Options:
1 Binary to Decimal
2 Decimal to Binary
3 Exit program
Enter your choice: 1
Please enter the binary number: 11001710 11001710 in decimal is None
Options:
1 Binary to Decimal
2 Decimal to Binary
3 Exit program
Enter your choice: 3
Goodbye.
8
Question 4 40 points
In this question we will be dealing with the dictionary representation of polynomials as seen in class.
As a reminder, a polynomial can be represented as a dictionary where each key corresponds to a power, and a value corresponds to the coefficient of that power.
For example, the polynomial 12 37
can be represented by the dictionary 0 : 1, 2 : 1, 7 : 3
You will write three functions for this question, as follows:
1. derivativepolynomial, order1 takes as input a dictionary containing a polynomial rep resentation, and returns a dictionary containing the nthorder derivative of that polynomial, where the order is given by the second argument. For example, when order is 1, the firstorder derivative should be taken and returned. If order is 2, the secondorder derivative should be computed, and so on. You can assume that the order argument will always be greater than or equal to 1.
2. integralpolynomial takes as input a dictionary containing a polynomial representation, and returns a dictionary containing the indefinite integral of that polynomial. Note that the polynomial will be assumed to not have any term with power 1.
3. displaypolynomial takes as input a dictionary containing a polynomial representation, and prints the polynomial to the screen in the format C0xP0 C1xP1 where each Ci is a coefficient of the power Pi and the terms CixPi are separated by spaces. You should print the coefficients Ci with 2 decimal places.
4. create takes no inputs. It prompts the user to enter a polynomial in the format:
C0xP0 C1xP1 where each Ci is a coefficient of the power Pi and the terms CixPi are separated by spaces. It should then turn this string into a polynomial dictionary as described above, and return this dictionary. You can assume that the user will only enter strings in the above format, and that they can enter coefficients with decimal places, but powers will always be entered as integers. Note that the user could enter negative powers. Also, assume that they will enter polynomials with only one term per power i.e., they will not enter 5323, and that they will not enter any term with power 1.
You must not import any Python libraries or their functions for this question.
9
Filename
You must write this program in a file called polynomials.py. Please write your code in the provided file, as it contains code to test your functions and display the output as shown in the below examples.
Examples as executed in Thonny
Example 1:
Run polynomials.py
Enter polynomial: 34 82 21 89 3.004 8.002 2.001 8.009
Enter order of derivative: 10 Derivative:
0.000
Integral:
0.605 2.673 1.002 0.8010
Example 2:
Run polynomials.py
Enter polynomial: 310 45 32 95 3.0010 4.005 3.002 9.005
Enter order of derivative: 2 Derivative:
330.0012 120.007 6.000 180.003 Integral:
0.339 1.004 1.003 1.506
Example 3:
Run polynomials.py
Enter polynomial: 1.510 0.538610 1.5010 0.5410
Enter order of derivative: 3 Derivative:
1080.007 710.9513
Integral:
0.1411 0.069
10
Reviews
There are no reviews yet.