Programming 1
Week 7: Exercises

1. Split

Write a functon split(s) that splits a string s into words separated by one or more spaces, and returns a list of these words. Do not use the built-in method of the same name.

2. Rotate a Matrix

Write a program that reads a matrix of integers and rotates it to the right.

Input:

2 4 6 8
8 6 4 2
1 3 7 9
9 7 3 1

Output:

9 1 8 2
7 3 6 4
3 7 4 6
1 9 2 8

3. Cycle

Write a function cycle that takes a list of length N representing a permutation of the integers 0, …, N – 1. The function should return True if the permutation is a single cycle. For example:

4. Most Common Word

Write a program that reads text until the end of input is reached, and prints out the most common word in the input text.

5. Most Common Characters

Write a program that reads a string and print out the five most common characters in the string along with their occurrence counts, e.g.

e 23

t 20

o 18

n 15

i 14

Write the characters in decreasing order of frequency. The string may contain any Unicode characters.

6. Flip a Dictionary

Write a function that takes a dictionary such as { 'green' : 'zelený', 'red' : 'červený' } and returns a corresponding dictionary in which the keys and values have been flipped, e.g. { 'zelený' : 'green', 'červený' : 'red'} .