FIT1053 Algorithms and Programming Fundamentals in Python - Workshop 4
FIT1053 Algorithms and Programming Fundamentals in Python - Workshop 4
Workshop 4.
Objectives
After this workshop, you should be able to:
Recognise and use some of Python’s built-in functions and methods.
Solve several common algorithmic/computational problems.
MARKS: To receive full marks in Workshop 4 you must implement a minimum of eight functions, and at least
two functions must be from the list of Complex Functions. You must follow all restrictions outlined to receive
any marks. Assuming restrictions are followed, marks will be determined by the tester.
For students in FIT1053, at least four functions must be from the list of Complex Functions.
Simple Functions
RESTRICTION: You may not use the Python built-in function you are implementing within its own
implementation. In the absence of additional restrictions, you may use other Python built-in functions and
methods. If given a list or sequence as input, you must not change the original list or sequence.
is in()
Write a function is in(char, string) that models a limited version of the behavior of the in keyword (for
more details, refer to the Python docs). You may not use the in keyword in this function.
Input: a string char of a single character (you may limit to alphanumeric), and a string string comprising
both upper and lower case alphanumeric and non-alphanumeric characters.
Output: a boolean, True if char is found in string; otherwise False.
Note: the matching behaviour to be implemented here is different to the matching behaviour of Python’s in
keyword.
is sorted()
There is no built-in function that checks whether a sequence is sorted. Write a function is sorted(seq) that
checks whether a given sequence of sortable elements is sorted from smallest to largest.
Input: a sequence seq of comparable elements.
Output: a boolean, True if for all items in the list, the item at index i is less than or equal to the item at
index i + 1; otherwise False.
Note: Remember that a sequence can be a list, a string, a tuple, or a range, so this function must work for all
these types.
1
abs()
Write a function abs(x) that models a limited version of the behavior of the built-in function (it is not necessary
to deal with complex numbers).
all()
Write a function all(lst) that models a limited version of the behaviour of the built-in function. You may
use the built-in function bool().
Input: a sequence (list) of objects lst.
Output: a boolean, True if all objects in lst have a truth value of True; otherwise False.
any()
Write a function any(lst) that models a limited version of the behaviour of the built-in function. You may
use the built-in function bool().
max()
Write a function max(lst) that models a limited version of the behaviour of the built-in function. In Python,
the max() function is capable of significantly more - read about it here.
Input: a non-empty sequence (list) of comparable objects lst.
Output: the largest item in lst.
min()
Write a function min(lst) that models a limited version of the behaviour of the built-in function. In Python,
the min() function is capable of significantly more - read about it here.
reversed()
Write a function reversed(seq) that models a limited version of the behaviour of the built-in function. You
may not use list slicing (i.e. seq[::-1]) in this function, although it is a cool trick to know!
Input: a sequence seq.
Output: a new list containing all items in lst in reverse order.
Note: Remember that a sequence can be a list, a string, a tuple, or a range, so this function must work for all
these types.
sum()
Write a function sum(lst) that models a limited version of the behaviour of the built-in function.
Input: a sequence (list) of numbers lst.
Output: the sum of all numbers in lst.
2
Complex Functions
RESTRICTION: You may not use the Python built-in function you are implementing within its own
implementation. In the absence of additional restrictions, you may use other Python built-in functions and
methods. If given a list or sequence as input, you must not change the original list or sequence.
bin()
Write a function bin(x) that models the behavior of the built-in function.
Input: an integer x.
Output: a binary string representation of x.
Note: The output string should start with ”0b” to communicate it is a binary string (e.g. bin(3) returns
’0b11’). Negative integers and 0 are valid inputs.
enumerate()
Write a function enumerate(seq[, start]) that models a limited version of the behavior of the built-in
function. In Python, enumerate() returns an enumerate object - enumerate is a specific type that is different
to the list type.
Input: a sequence seq, and an optional argument, an integer start. The default parameter value for start
is 0.
Output: a list of tuples, where each tuple is of the form (n, element). If start=0, n will be the index of
the element, otherwise n will be the index of the element, offset by start.
Note: For an example of enumerate, see the Python docs. Remember that a sequence can be a list, a string, a
tuple, or a range, so this function must work for all these types. If you use yield you must be able to explain
its behaviour to your instructor.
filter()
Write a function filter(function, seq) that models the behavior of the built-in function.
Input: a function function and a sequence seq.
Output: a list of elements from seq that when used as input in function return a value that is evaluated
to True using Python’s standard truth testing procedure. The relative order between elements must be
maintained.
Note: Remember that a sequence can be a list, a string, a tuple, or a range, so this function must work for all
these types.
map()
Write a function map(function, seq) that models a limited version of the behavior of the built-in function.
Input: a function function and a sequence seq.
Output: a list comprising the output of function for each element in seq. The order of seq must be
maintained. If seq is empty, return an empty list.
Note: Remember that a sequence can be a list, a string, a tuple, or a range, so this function must work for all
these types.
lim range()
Write a function lim range(start, stop[, step]) that models a limited version of the behavior of the se-
quence type. It is important that you do not name this function range as you are likely to use built-in range
in other functions, and having only a limited version will result in weird behaviour. In Python, range() returns
a range object - range is a specific type that is different to the list type.
Input: two integers, start and stop, and an optional argument, a non-zero integer step. The default
parameter value for step is 1.
Output: a list of integers, counting from start (inclusive) to stop (exclusive), increasing by step.
3
round()
Write a function round(number[, ndigits]) that models the behavior of the built-in function.
Input: a float number, and an optional argument, a non-negative integer ndigits. The default parameter
value for ndigits is 0.
Output: if ndigits is 0, an integer, else a float, rounded to ndigits after the decimal point. If the first
dropped digit is greater than 5, round up; if the first dropped digit is less than 5, round down; else round
in such a way that the remaining digit in the smallest position is even.
set()
Write a function set(aList) that models a limited version of the behavior of the built-in function. In Python,
set() returns a set object - set is a specific type that is different to the list type.
Input: a list aList.
Output: a list that contains the set of elements in aList (i.e.aList with duplicates removed). It is not
important to preserve order.
sorted()
Write a function sorted(seq) that models a limited version of the behavior of the built-in function.
Input: a sequence seq.
Output: a list comprising the elements of seq in sorted order (i.e. for for all items in the returned list, the
item at index i is less than or equal to the item at index i + 1).
Note: Remember that a sequence can be a list, a string, a tuple, or a range, so this function must work for all
these types.
Default Parameter Values Many of the functions in the Complex function list contain default
parameter values. When a function definition contains one or more parameters of the form parameter
= expression the function is said to have ”default parameter values”. For a parameter with a default
value, the corresponding argument may be omitted from a function call, in which case the parameter’s
default value is substituted (from the Python Docs).
Simply put, if you wish to define a function my function() that takes an optional argument x, and if
no argument is given it defaults to x=0, you can define the function by doing this:
def my_function(x=0):
...