[go: up one dir, main page]

0% found this document useful (0 votes)
3 views8 pages

Tutorial 2 Worksheet: COMP1117A Computer Programming I 2018-2019

The document is a tutorial worksheet for COMP1117A Computer Programming I, outlining objectives and exercises focused on data manipulation, if-else statements, and built-in functions in Python. It includes exercises on sign detection, quadratic roots, leap years, absolute differences, median calculation, degree to radian conversion, triangle area calculations using trigonometric ratios and Heron's formula. Each exercise provides sample test cases and emphasizes the importance of testing code with an auto-grader.

Uploaded by

mikewaics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Tutorial 2 Worksheet: COMP1117A Computer Programming I 2018-2019

The document is a tutorial worksheet for COMP1117A Computer Programming I, outlining objectives and exercises focused on data manipulation, if-else statements, and built-in functions in Python. It includes exercises on sign detection, quadratic roots, leap years, absolute differences, median calculation, degree to radian conversion, triangle area calculations using trigonometric ratios and Heron's formula. Each exercise provides sample test cases and emphasizes the importance of testing code with an auto-grader.

Uploaded by

mikewaics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Tutorial 2 Worksheet

COMP1117A Computer Programming I 2018-2019

Tutorial objectives:
- To practice data manipulation.
- To practice the use of if-else statements.
- To be able to derive conditions to control branching of program flow.
- To practice the use of some built-in functions in the math library.

Tutorial Exercises deadline: Oct 4, 2018 14:20

1. Simple Data manipulation and if-else statements

Exercise 2.1 Sign detection


Write a program to check an input number and print the corresponding message (Positive,
Negative, or Zero).

- First let’s tackle the first exercise a little by little. First, let’s we ​read an input and print it​:

# Exercise 2.1
myNumber = int(input())
print(myNumber)

What is the expected result if your run this code?

- The code above read from the input, convert it to integer,store it in a variable, then print it.
- Here we assume that we will be handling ​integer only ​but not a floating point value.
- To continue, instead of printing the number, we ​test ​if the number is positive or not:

if myNumber > 0:
print("Positive")

How should we test this code?

- Now we have written the first case, we will want to test it. With a if-statement added, there
will be at least ​two cases​ that you need to test, and the only case that we have handled is
positive numbers.
- Save and run the code, test your code with the value 1 and -1. You should get “​Positive​” for
input 1, and ​nothing ​for -1.
- Next, try to evaluate your code to see how well it is. We know that it can only handle one
case, so we expect that the code ​will not pass ​the auto-grader fully.
- Examine the result of evaluation, you will see that you ​failed 3 test cases​, namely Negative,
Zero, and Negative One. You will see the input and expected output of these test cases if
you scroll down a bit.
- When using the auto-grader to evaluate your code, there are two things you should do:
- Run ​your code and test it with your own test cases first. This helps you to understand
your code.
- Read ​the result of evaluation carefully. The result tells you what the auto-grader is
testing and what the expected results are.
- With the code evaluated once and passed some of the tests, we know we are on the right
track. We then handle the second case. For instance, let’s handle the ​negative ​case first.
Adding ​right after​ the above if-clause:

elif myNumber < 0:


print("Negative")

- Save, run, and evaluate it, we have one more case to go.

How will you handle the last case?

- Let’s finish of by adding the else-clause right after the above elif-clause:

else:
print("Zero")

- Now, check that your code ​passes the auto-grader​.

Sample Test Cases:

Input Expected output

12 Positive

-1 Negative

0 Zero

2. More complicated conditions

Exercise 2.2 Quadratic roots


Write a program to check the type of roots from the coefficients of a quadratic equations.

Suppose the quadratic equation is ax2 + bx + c = 0 . The input to your program will be a , b ,
and c . Your program should then report the type of roots (Real, Double, Imaginary) of the
equation.
You can assume that input a , b , and c are integers and a will never be 0.

Note: Type of roots of a quadratic equation can be determined by its discriminant b2 − 4ac .

Sample Test Cases:

Input Expected output

1 Real
-3
2

1 Double
2
1

1 Imaginary
2
4

Exercise 2.3 Leap year


Write a program to determine the input year is a leap year or not. Output format as follows:

<input year>​ is a leap year.


Or
<input year>​ is not a leap year.

where ​<input year>​ is the year input by the user.

A year is a leap year if:


- The year is divisible by 4, except … (e.g., 2004 is a leap year)
- … when the year is divisible by 100, except … (e.g., 1800 is NOT a leap year)
- … when the year is divisible by 400. (e.g., 2000 is a leap year)

For example:
Year 2016 is a leap year because it is divisible by 4 and is not divisible by 100;
Year 1900 is not a leap year although it is divisible by 4, because it is divisible by 100 and is
not divisible by 400;
Year 2000 is a leap year because it is divisible by 400.

- There are two possible results for this exercise. We may consider using a simple if-statement
to hand it, but then we need to come up with the correct ​condition​:
# Exercise 2.3
year = int(input())

if ????????????????????:
print(year, "is a leap year.”)
else:
print(year, "is not a leap year.”)

- There are three conditions to be considered for a year to be a leap year:


1. Year is divisible by 4 → ​year % 4 == 0
2. Year is not divisible by a 100 → ​year % 100 != 0
3. Year is divisible by 400 → ​year % 400 == 0​.

- We can combine these conditions together with ​and​, ​or​, and parentheses:

if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):


print(year, "is a leap year.”)
else:
print(year, "is not a leap year.”)

Is there any other way to solve the problem?

- Instead of using a single condition, we can use multiple conditions. For example, we can
handle each conditions one by one and exhaust all possibilities.
1. Year is divisible by 4?
Yes ​→ not sure, consider condition 2; ​No ​→ must not be a leap year.
2. Year is divisible by 100?
Yes ​→ not sure, consider condition 3; ​No ​→ must be a leap year.
3. Year is divisible by 400?
Yes ​→ must be a leap year; ​No ​→ must not be a leap year.

- With all possibilities exhausted, we can write our code accordingly:

if year % 4 != 0:
print(year, "is not a leap year.”)
elif year % 100 != 0:
print(year, "is a leap year.”)
elif year % 400 != 0:
print(year, "is not a leap year.”)
else:
print(year, "is a leap year.”)

- You should now see that there are many ways to solve a problem. Sometimes it will be
easier to break a condition into multiple ones to handle.
Sample Test Cases:
Input Expected output

1992 1992 is a leap year.

2018 2018 is not a leap year.

3. Tutorial exercises

Exercise 2.4 Absolute difference


Write a program to calculate the absolute difference of two input numbers.

Sample Test Cases:

Input Expected output

73 36
37

37 0
37

Exercise 2.5 Finding median


Write a program to output the median of three input numbers.

Sample Test Cases:

Input Expected output

33 25
25
17

23 23
35
17

Exercise 2.6 Degree to radian


Write a program to convert a user input degree (​in float​) and convert to radian according to
the following formula:
radian = degree × π ÷ 180 .
Then print the result rounded to 4 digits from the decimal point.

- In the formula, we need the value of π and actually we can use the built-in π value by
importing the math library.
- Add the following line on the top of your program. This tells Python that we are going to use
its math library.
import math

- Then you can use the built-in π value by the code ​math.pi​. For example, you can just print
the value π with the following code.

print(math.pi)

- You will see 3.141592653589793 if you execute the above line of code.

- Next, when you finished the calculation. You can use another built-in function called
round(x,n)​ to round a number ​x​ to a specific number of digits ​n​ from the decimal point. For
example, you can just print the π value to 6 digits from the decimal point with the following
code.

print(round(math.pi, 6))

- You will see 3.141593 if you execute the above line of code.

- Now, please implement the formula and see whether you could pass the autograder. (Note:
we know that there is a built-in function in Python to convert degree to radian, please do not
use that function in this exercise.)

Sample Test Cases:

Input Expected output

10 0.1745

10.5 0.1833

-123 -2.1468

Exercise 2.7 Area of triangle using trigonometric ratio


There are many ways to calculate the area of a triangle. As shown in the above figure, if we
know the sides A, B and the angle between A, B. By using the trigonometric ratio sine, we
could calculate the area of the triangle by the following formula:
area = 21 × A × B × sin(θAB ) .
Write a program to ask the user to input the (​float​) values of A, B and θAB (where θAB is in
radian) and print the area rounded to 4 digits from the decimal point.

- In the formula, we need the value of sin(x) and actually we can use the built-in sin(x) value
by importing the math library.
- Add the following line on the top of your program. This tells Python that we are going to
use its math library.
import math

- Then you can use the sin(x) value by the code ​math.sin(x)​. For example:

print(math.sin(math.pi/2))

π
- You will see 1.0 if you execute the above line of code. As in radian 2 is equal to 90
degree, and sine value of it is 1.

Sample Test Cases:

Input Expected output

5 25.0
10
1.5708

5.8 19.241
6.7
1.4312

Exercise 2.8 Area of triangle using heron’s formula

If we know the three sides (A, B, C) of the triangle, we can also calculate the area of triangle
using the Heron’s formula as follows:


A+B+C A+B+C
area = 2 ( 2 − A)( A+B+C
2 − B)( A+B+C
2 − C)
Write a program to ask the user to input the (​float​) values of A, B and C and print the area
rounded to 4 digits from the decimal point.

- In the formula, we need the value of √x and actually we can use the built-in sqrt(x) value by
importing the math library.
- Add the following line on the top of your program. This tells Python that we are going to
use its math library.
import math

- Then you can use the sqrt(x) value by the code ​math.sqrt(x)​. For example:

print(math.sqrt(4))

- You will see 2.0 if you execute the above line of code.

A+B+C
- Remarks: In the formula you can see that 2 is repeated many times, you may use a
A+B+C
temporary variable to store the computed value, e.g. temp = 2 . Then you can easily
manage and simplify the formula in your program.

Sample Test Cases:

Input Expected output

10 43.3013
10
10

10.5 67.7351
15.75
12.985

- Please complete Exercise 2.1 to 2.8 on Moodle, make sure you have evaluated your code
and your code passed the auto-grader.
- Starting from this tutorial, you must evaluate your code on or before next Thursday
14:20 to get marks for the tutorial.

You might also like