FIT1053 Algorithms and Programming Fundamentals in Python - Workshop 2
FIT1053 Algorithms and Programming Fundamentals in Python - Workshop 2
Workshop 2.
Objectives
After this workshop, you should be able to:
• Define and call functions in Python.
• Use boolean expressions and conditionals to solve problems.
Implement a function calculate(x, y, operator) that takes three arguments: two integers, and one of ‘+’,
‘-’, ‘x’, ‘/’. The function will return the result of the created equation.
NOTE: all years that are divisible by four are leap years, unless they are also divisible by 100, in which case
they are only leap years if they are also divisible by 400.
1
Part B - 2000 was a leap year.
Write a function leap year answer(year) that returns the string ‘Year was a leap year’, ‘Year was
not a leap year’, ‘Year will be a leap year’, ‘Year will not be a leap year’. For the purpose
of tense, you may assume 2020 and any early year are past tense; the year 2021 and onwards are future tense.
You may wish to call your function from Part A as part of this function.
Extension (Optional): If you wish to make your function’s output dynamic (i.e. tense is updated when the
year changes) look into the datetime or time modules (non-examinable).
A triangular number is any number from the series of numbers (1, 3, 6, 10, 15, etc.) obtained by continued
summation of the natural numbers 1, 2, 3, 4, 5, etc.1 Implement a function next triangular number(num)
that takes an integer and determines the smallest triangular number that is equal to or larger than the given
number. The function must:
• take one argument: a positive integer, num
• return trig num, where trig num is the smallest triangular number that is greater than or equal to num
• use a while-loop
Example: calling next triangular number(25) returns 28 as the triangular number 21 (1+2+3+4+5+6 =
21) is smaller than 25, and so the next triangular number 28 (1 + 2 + 3 + 4 + 5 + 6 + 7 = 28), which is bigger
than 25, is the next triangular number in the series.
Warm Up
Let’s start by learning about indexing. Indexing allows us to access the ith element in a sequence. For example,
if we have the list lst = [‘a’, ‘b’, ‘c’, ‘d’] and we want to access the ‘b’ we can ask for lst[1]. Notice
that Python indexes from 0. This means if we want the first element in a sequence we have to ask for seq[0].
If the first element is at 0, in our list lst what index is the last element? How could we write a loop that
would look at every element?
Part A - Adding
Write a function add(numbers) that returns the sum of every second number in a given list. (E.g. calling
add([2,-3,5,8,10,1]) returns 6 because −3 + 8 + 1 = 6.)
Part B - Flipping
Write a function flip(binary string) that takes a binary string (a string containing only 1s and 0s) and
returns a new string where every bit has been flipped. (E.g. calling flip(’001011’) returns ’110100’.)
2
Example: calling n even fibonacci(10) should return [0, 2, 8, 34, 144, 610, 2584, 10946, 46368,
196418].
Challenge: Estimate e
The Challenge is not compulsory to receive full marks for this workshop. Challenges are worth 3% of your final
unit mark. You may attempt as many challege questions as you like, with your highest scored challenge being
your final challenge mark. 1 mark is from correctly solving the challenge, 2 marks is from the interview. In the
interview you will be asked how you could improve your implementation. To prepare for this, try to make your
code as clean and efficient as possible.
Euler’s number, represented as e, is one of the most important mathematical constants in computer science.
In this challenge we will estimate e. There are many ways of calculating the value of e, and in this exercise we
are going to use the fact that:
1 1 1 1 1 1
e= + + + + + ...
0! 1! 2! 3! 4! 5!
Note that ! means factorial. That is, 0! = 1, 1! = 1, 2! = (1 × 2), 3! = (1 × 2 × 3), and so on. You must use this
method for calculating e to receive marks for this challenge.
Implement a function, estimate e(error), that takes a positive number error as input, and returns
estimate, where |estimate - e| ≤ error. The returned value estimate must be the first partial sum
of the above infinite series that meets the condition. Note: you are expected to use the constant e from the
math module. Importing e is the only import you may make.
Examples
• calling estimate e(1) should return 2.0. 2.0 = 1
0! + 1
1! , and | 2.0 − e |≈ 0.7182 which is less than 1.
• calling estimate e(0.1) should return 2.6666666666666665. 2.666 ≈ 1
0!
1
+ 1! 1
+ 2! 1
+ 3! , and | 2.6666−e |≈
0.0516 which is less than 0.1.