You are on page 1of 2

Mid-Term Examination -1-

MID-TERM EXAMINATION
1. Requirements
Write a program that performs complex calculations like a pocket calculator named calc.py, but the program runs
as a command line. The program will read the command lines from standard input (keyboard) and output the
corresponding results to standard output (screen).
The program needs to check the type of operation to be performed and the corresponding parameters (ignores
case-sensitive, for example entering Sqrt or SQRT is the same). The program will perform the math and output the
calculated results.
Each time the program executes, it will run all the math operations in the standard input (keyboard) and then
output the calculated results to the standard output (screen). Please check sample for more information. The
operations are described in the following table:
No Operator Description Example
+ Perform operations: addition, subtraction, multiplication, Input: + 5 8 Output: 13
- division, power and remainder. Outputs error if dividing by Input: - 8 5 Output: 3
* 0. These operators accept exactly 2 parameters Input: * 6 2 Output: 12
1 / Input: / 8 0 Output: error
** Input: ** 2 3 Output: 8
% Input: % 10 3 Output: 1
Calculate the average of the list of Numbers. These Input: avg 1 5 7
operators accept from 1 to a maximum of 10 parameters Output: 4.33 ((1+5+7)/3)
Input: avg 3 5 4 7 8 9
2 avg
Output: 6.00
Input: avg 0
Output: 0
Find the smallest / largest number in the list of numbers. Input: min 1 5 7 -2
min
These operators accept from 1 to a maximum of 10 Output: -2
3 max
parameters. Input: max 5 4 3 7 8 9
Output: 9
Sum odd/even numbers from 1 to n. If n <=0, output error. Input: sum even 8
These operators accept exactly 1 Parameters Output: 20
(Because: 2 + 4 + 6 + 8 = 20)
Input: sum odd 5
sum odd Output: 9
4
sum even
(Because: 1 + 3 + 5 = 9)
Input: sum odd -2
Output: error
5 Counts the number of primes in the list. These operators Input: count 1 2 7
accept from 1 to a maximum of 10 parameters. Output: 2
Input: count 5 4 7 8 9 11
count
Output: 3
Input: count 0
Output: 0
6 Convert from decimal to binary. If n <=0, output error. Input: dec2bin 2
These operators accept exactly 1 parameters Output: 10
Input: dec2bin 10
dec2bin
Output: 1010
Input: dec2bin 0
Output: error
Mid-Term Examination -2-

7 Converts an integer sequence to a new integer sequence with Input: map double 1 2 3
map each element having twice the value of the original element. Output: 2 4 6
double These operators accept from 1 to a maximum of 10 Input: map double -1 -2 -3
parameters Output: -2 -4 -6
8 Convert an integer sequence to a new integer sequence with Input: map square 1 2 3
map each element having the value equal to the square of the Output: 1 4 9
square original elements. These operators accept from 1 to a Input: map square -1 -2 -3
maximum of 10 parameters Output: 1 4 9
9 Converts an integer sequence to a new integer sequence Input: map absolute 1 2 3
map with each element having a value equal to the absolute Output: 1 2 3
absolute value of the original elements. These operators accept from Input: map absolute -1 -2 -3
1 to a maximum of 10 parameters. Output: 1 2 3
The input data can contain one or more lines of data. Each line is an independent mathematical expression.
A valid mathematical expression is one that contains only the operations listed in the table above and valid integer
values. The tokens are separated by only one space, with no extra spaces on either side.
• If input data contains more than 1 line, the output data must also contain the corresponding number of result lines.
In addition to the cases listed in detail in the table above, in all other cases of invalid expressions, the program
must output "error".
• If the result of the calculation is a number with a decimal part, take only the first 2 decimal places, do not
round up or down.
Parameters can be integers or floating point numbers, as long as they can be converted from strings to numbers.
Value of the parameters cannot be outside the range [-100, 100].
Examples of invalid expressions:
• + 5 8: because there is more than 1 space between 5 and 8
• * 120 8: Because parameter 120 is outside the allowable range [-100, 100]
• map 1 2 3: because one of the sub operators is missing: double, square or absolute.
• add 4 2 1: the add operator only accepts exactly 2 parameters, 3 were given
• count 1 2 3 4 5 6 7 8 9 1 2: There are more than the maximum number of parameters this operator supports.
• avg 1 5 7 0: because there is an extra space at the beginning
• dec 2bin 2: dec 2bin is not a valid operator
• count: because there is missing data after the operator name
Below are some examples of the contents of input.txt and output.txt respectively:
Input Data Output Data
sum even 8 20
map Double 1 2 3 246
sum odd -5 error
*62 12
AVG 1 5 7 4.33
+58 error
+ 29 -9 20
* -3 +70 -210
* 2.5 3. 7.5
2. Deadline and submission method
Follow the deadline on classroom. How to submit your work? Students log in to LMS and submit midterm by the
time specified in the LMS. Students only submit a single file named calc.py uncompressed.
--- END ---

You might also like