Worksheet 2
Worksheet 2#
Exercise (cm to m function)
Level:
Create a function cm_to_m()
that given a number n
as an argument, which represents centimeters (cm), will convert that
number to meters (m).
Exercise (Mark grading function)
Level:
Write a program that returns the student class grade. You may adapt this code.
Exercise (Even numbers function)
Level:
Write a function that given a list of numbers returns even numbers only back.
Exercise (Bubble sort)
Level:
In this exercise we are going to practice nested loops by coding the bubble sort. The bubble sort is a simple sorting
algorithm. To sort a list of numbers, the bubble sort iterates over the numbers in the list. For each number l[j]
,
it compares it with the number next to it l[j+1]
. If l[j]
is bigger than l[j+1]
, they will swap places, that is,
l[j+1]
becomes l[j]
and l[j]
becomes l[j+1]
. This process is repeated for N times, where N is the length of the list - 1.
Below is an example of the first iteration of the bubble sort algorithm. Write a bubble_sort()
function that will implement
the bubble sort algorithm as explained above. The function should take a list of numbers as an input and returns
the sorted list as an output.
Do you know?
The bubble sort is one of the simple sorting algorithms. Because it is a simple algorithm it is not an efficient one and, in fact, it is widely critisised and also a known programmers humour. Barack Obama once joked about it too!