Exercises

Exercise 3.1: Reading and writing a text file

a) Use the built-in Python function open to write a file called “paajordenetsted.txt”, containing the poem På jorden et sted by André Bjerke. So you will create a string with the poem and write it into the file. The poem is given below.

På jorden et sted

Tro ikke frosten som senker en fred
av sne i ditt hår.
Alltid er det på jorden et sted
tidlig vår.

Tro ikke mørket når lyset går ned
i skumringens fang.
Alltid er det på jorden et sted
soloppgang.

Since the poem (hence the string containing it) has multiple lines, you can choose between making a multi-line string and using the newline character \n. We recommend the second option. This is because the actual string will include the newline character \n either way, so it is worth getting used to working with it.

poem = """På jorden et sted
              
Tro ikke frosten som senker en fred
av sne i ditt hår.
Alltid er det på jorden et sted
tidlig vår.

Tro ikke mørket når lyset går ned
i skumringens fang.
Alltid er det på jorden et sted
soloppgang."""

poem = "På jorden et sted \n\nTro ikke frosten som senker en fred \nav sne i ditt hår. \nAlltid er det på jorden et sted \ntidlig vår.\n\nTro ikke mørket når lyset går ned \ni skumringens fang. \nAlltid er det på jorden et sted \nsoloppgang."

b) Now, in a new Python program, read the file “paajordenetsted.txt” that you made in the previous exercise. Print the content of the text.

c) Split the text into words, remembering to remove all punctuations and make all letters lowercase. Count the number of times that the words “sted”, “tro” and “soloppgang” occurs in the text. Print these numbers using string formatting.

Hint: To avoid writing the same code many times, you should loop over the words you want to count and reuse the code in the loop for each word.

for word in ["sted", "tro", "soloppgang"]:
    # do the counting and printing for each word

d) Now count the number of times that the string “på jorden et sted” occurs in the text using the .count() method.

If you didn’t know about the .count() method, you might have found it by searching “count occurrences of substring python” and finding a stackoverflow answer like this: https://stackoverflow.com/questions/8899905/count-number-of-occurrences-of-a-substring-in-a-string.

There are functions and methods in python which perform many useful tasks, you should learn to search the internet to find them before wasting all day writing code to solve a solved problem. Unless we tell you to solve it yourself of course, then its good practice.

Example use of the .count() method:

mytext = "Methods are functions that are called on a specific object, like a string, by using a . and the method name right after the object!"
print(mytext.count("are"))
2

*Exercise 3.2: In-Place 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.

In the previous exercise set you implemented a sorting algorithm that uses two lists to sort numbers. This uses a lot of space on the computer, and requires a many operations which change the size of lists. A better approach is using an algorithm that sorts a list without changing its size or using any other lists - an in-place sorting algorithm.

The sorting algorithm outlined here is an in-place sorting algorithm. (When testing this algorithm, it used 44.2 seconds to sort 30000 numbers. The algorithm from last week used 55.6 seconds. Both of these times are considered extremely slow.)

Here is a list of numbers we want to sort in ascending order:

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]

The idea is to go through the elements of the list swapping each element with the smallest element found from that point onward.

a) Find the smallest number in this list.

b) Find the index of the smallest number in this list.

c) Swap the smallest number in this list with the first element.

d) Now, find the smallest number in this list, beginning from the second element.

e) Swap the number you found in d) with the second number in the list.

f) Sort the numbers in numbers in ascending order without creating another list. Hint: You will need two nested for-loops. One iterating i from 0 to len(numbers), and one inside that one iterating j from i to len(numbers). You can of course use other variable names instead of i and j.