Exercises

If you ever get really stuck on an exercise, skip it and ask for help!

Exercise 2.1: Lists

a) Create a list with five names. Use a loop to print greetings including each name.

b) Create a list with ten integers.

  • Change the first element to be equal to the fifth

  • Print the third element

  • Add 5 to all elements (for loop)

  • Swap the first and last elements

  • Remove the second element of the list using removedElement = myList.pop(index).

Exercise 2.2: What is a word?

a) Split the sentence “I’d rather go to the cinema tonight” into words using the method split. Print the resulting list of words and its length. Do you encounter any problems when using the split-functionality to extract words?

b) Start with the same sentence (“I’d rather go to the cinema tonight”). Before splitting it into words, replace “‘d” with ” would” using the method replace. Now split the result into words. Print the list of words and its length. Apply the same process to the sentence “I’d rather read Maria’s book”, and discuss the validity of using this method to distinguish conjoined words.

Exercise 2.3: Loops

a) Write a program that prints all even numbers from 0 to 20.

b) Print the 7 times table.

c) Create a list with the numbers \(\frac{1}{1}\), \(\frac{1}{2^2}\), \(\frac{1}{3^2}\), \(\frac{1}{4^2}\) and so on, with 1000000 elements.

d) Find the sum of every element in your list from c), multiply it by 6, and take its square root. Is there anything special about the result?

e) Write a program that finds the sum of all odd numbers from 1 to 1001. How can you test that your program works?

*f) Create a list with 10 numbers. Reverse it using a for loop.

*Exercise 2.4: Sorting Algorithm

Important note! Refrain from using functions such as sorted() and min() to solve this exercise, as the point is implementing your own algorithms using loops, if-statements and lists. Feel free to use len(), pop() (removes element) and append(). In all other cases you really should use functions like sorted() and min() though!

Sorting algorithms are some of the most used algorithms in the world. When you make an online search, the search engine sorts the results after relevance. Shopping sites let you choose between different criteria for sorting their products. And of course, Netflix sorts movies and shows after what is most likely to keep you subscribed.

The simplest case however, is sorting a list of numbers. There are many different ways of sorting numbers, each one with its pros and cons. The sorting algorithm outlined here is slow and uses a lot of space compared to more advanced sorting algorithms. It is however, a good exercise in algorithmic thinking.

Here is a list of numbers:

numbers = [5,1,6,3,2,6,1,1,4,78,4,12,12,6,23,12,4,3,34,2,1,43,7,7,2,3,4,214,5,1,2,7684,325,135,12353145,2,0,0,123,3,5,1,2,5,7,43,23,1]

a) Find the smallest number in this list using a for loop.

b) Find the index corresponding to an occurrence of the smallest number in the list.

c) Remove that occurrence of the smallest number using the method pop,taking care to store the popped number in a variable.

d) Create a new list sortedNumbers and add the smallest element popped from numbers to sortedNumbers using append.

e) Fill sortedNumbers with all of the numbers from numbers in ascending order. Hint: Make the code from b), c) and d) run len(numbers) times.