[go: up one dir, main page]

0% found this document useful (0 votes)
146 views3 pages

FIT1053 Algorithms and Programming Fundamentals in Python - Workshop 2

This document provides instructions for Workshop 2 on algorithms and programming fundamentals in Python. It outlines 5 tasks to complete: 1) implementing a basic calculator function, 2) functions to determine if a year is a leap year, 3) a function to calculate the next triangular number, 4) functions to manipulate lists and strings, and 5) a function to return the first n even Fibonacci numbers. It also includes an optional challenge to estimate the mathematical constant e within a given error.

Uploaded by

Alireza Kafaei
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)
146 views3 pages

FIT1053 Algorithms and Programming Fundamentals in Python - Workshop 2

This document provides instructions for Workshop 2 on algorithms and programming fundamentals in Python. It outlines 5 tasks to complete: 1) implementing a basic calculator function, 2) functions to determine if a year is a leap year, 3) a function to calculate the next triangular number, 4) functions to manipulate lists and strings, and 5) a function to return the first n even Fibonacci numbers. It also includes an optional challenge to estimate the mathematical constant e within a given error.

Uploaded by

Alireza Kafaei
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/ 3

FIT1053 Algorithms and Programming Fundamentals in Python –

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.

• Use while-loops to solve problems.


• Manipulate lists and strings using basic sequence operators.
MARKS: Marks for this workshop will be determined by the tester.

Task 0: Setup file and tester


Download the Workshop 2 folder. In it you will find:
• a Python file workshop02.py

• a Python file workshop02 test.py


It is important that you do not change the name of either of these files. The file workshop02.py is the file
you need to modify. You will see you have already been provided with function definitions. You may open and
look in the test file, but ensure that you do not modify it. The exception is FIT1053 students who must set
FIT1045 = False. This week we will be teaching you how to run automated tests.
When you submit your workshop on Moodle, you only submit the file workshop02.py. It is important that
you fill out the function details() with your person information.

Task 1: Basic Calculator


<§2.1 Conditionals; §4 Functions>

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.

Example: Calling calculate(10, 4, ‘/’) returns 2.5.

Task 2: Leap Years


<§2.1Conditionals; §3.1 Strings>

Part A - Is this a leap year?


Write a function is leap year(year) that calculates whether a given (CE) year is a leap year. The function
must:
• take one argument: a positive integer representing a year (assumed to be CE)
• return True if that year was a leap year; return False if not

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).

Task 3: Triangular Numbers


<§2.2.1 While-loops>

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.

Task 4: Little and Long Lists


<§2.2.1 While-loops; §3.1 Strings; §2.1 Conditionals>

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’.)

Task 5: Even Fibonacci Numbers


Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 0
and 1, the first 10 terms will be:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Notice that some terms are even, and some are odd.
In the file workshop02.py, implement a function n even fibonacci(n) that takes an integer n as input,
and returns a list of the first n even fibonacci terms, in ascending order. You will need to write the function
definition yourself.
1 For a more intuitive visual representation, see here: https://en.wikipedia.org/wiki/Triangular_number.

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.

You might also like