Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

COMPUTER SCIENCE

WEEK 7 – SEMINAR
tanser.karakash@navitas.com

Information Classification: Confidential


IN THIS SEMINAR:

 Lecture recap
 Practice exercises
 Sample exam exercises

Information Classification: Confidential


EXAMPLE 1

Write a function to accept 3 numbers from the user, calculate their sum and print it.

Information Classification: Confidential


EXAMPLE 2

Write a program to accept three numbers from the user. Pass these variables into a function to calculate their sum and
print it.​

Information Classification: Confidential


EXAMPLE 3

Write a program to accept three numbers from the user. Pass these variables into a function to calculate their sum and
print it. If only two inputs are passed into the function, then use 3.14 as the third number.

Information Classification: Confidential


EXAMPLE 4

Write a program to accept three numbers from the user. Pass these variables into a function to calculate their sum and
return it. Write another function to calculate and print the square of any number. Calculate the square of the return
value from the first function.

Information Classification: Confidential


EXAMPLE 5

Write a function that takes a list of user defined numbers as an input. The function should sort the numbers into even
and odd and return two lists: one containing even numbers and one containing odd numbers.

Information Classification: Confidential


EXAMPLE 6

In a new file, import and call the method written in an earlier exercise to sort values into even and odd lists.​

Information Classification: Confidential


EXAMPLE 7

Write a program to import the variable pi from the module called math. Use this to calculate the area of a circle of
radius 10 units.

Information Classification: Confidential


EXAMPLE 8

Write a program to import the module called math with the alias ‘m’. Use the methods and values from the
math module to calculate the sin of pi/2

10

Information Classification: Confidential


EXERCISE PROJECTILE

 In a file called max_height_calc.py write a function that calculates the maximum height of a projectile by taking
the launch velocity, launch angle, initial height and acceleration due to gravity.
 In another file called user_input.py ask the user to enter the parameters and store them in a list. Import the
function from max_height_calc.py and print the maximum height of a projectile.

11

Information Classification: Confidential


EXERCISE TEMPERATURE CONVERTER

Write a Python module called ‘temperature_converter’ that contains a function named ‘convert_temp’. The
function should take a temperature value in Fahrenheit as input and return the equivalent temperature in Celsius or the
vice versa , depending on the wishes of the user. Then, in your main file, write a code which imports the
‘temperature_converter’ module and allows the user to convert temperatures. After displaying the converted
temperature, ask the user if they want to continue converting or exit. Make sure to handle error cases.

12

Information Classification: Confidential


EXERCISE BINARY SEARCH

Implement a function called ‘binary_search’ that takes a sorted list and a target value as input. The function should
perform a binary search algorithm to find the index of the target value in the list. If the value is found, return its
index; else, return -1.

13

Information Classification: Confidential


EXERCISE DUTCH BSN VALIDATOR

The Dutch BSN (Burgerservicenummer) is a unique personal identification number used in the Netherlands. It
consists of 9 digits and follows a specific algorithm for validation. Write a Python program that implements a
function called ‘validate_bsn’ which takes a BSN as input and returns True if the BSN is valid, and False otherwise.
How to validate a Dutch BSN:
1. Check if the BSN is exactly 9 digits long.
2. Multiply each digit by its corresponding weight factor (9, 8, 7, 6, 5, 4, 3, 2, 1).
3. Sum up the products.
4. If the sum is divisible by 11, the BSN is valid.
Make sure you return a boolean value indicating whether the BSN is valid or nor and a message describing the reason
if the BSN is valid. Provide more context about why the validation failed.

14

Information Classification: Confidential


SAMPLE EXAM EXERCISE

Predict the output of the code on this slide. Show your


work in the form of a table of variables. Show how the
values of the variables change at each iteration.

15
Information Classification: Confidential

You might also like