During session 1
Exercises
NB! The practical session exercises are provided in such capacity that more experienced students would also find them interesting. It is not expected of you to get all of them done during one session. However, you are always welcome to complete the unfinished tasks at home!
1. Halloween
Before Halloween, children mapped the apartments they were going for trick-or-treating. One group of the children would go to the apartments with even numbers on even floors.
A text file contains apartments’ numbers delimited by a space: one row denotes one floor.
Write a program that reads apartments’ numbers from the file. The program has to output apartments’ number which the group of the children is going to visit.
If the content of the file is as follows:
1 2 3 4 5 6 7 8 9 10 11 12
then the program output is:
4 6 10 12
If the content of the file is as follows:
1 2 3 4 5 10 11 12 13 14 21 22 23 24 25 31 32 33 34 35 40 41 42 43 44 50
then the program output is:
10 12 14 32 34 50
2. Numbering
A real estate company builds several new apartment buildings - every building has a different number of floors and a different number of apartments. All apartments are numbered in the same way in every house. Every apartment’s number consists of two parts: the floor number and the apartment number on the floor. For example, apartment number 23 is on the second floor and it is the third apartment on that floor.
Write a function called numbering that takes the number of floors and the number of apartments per floor in the building as parameters and returns a matrix that contains apartments’ numbers on each floor.
Examples of the function call:
>>> numbering(3, 4) [ ['11', '12', '13', '14'], ['21', '22', '23','24], ['31', '32', '33', '34'] ] >>> numbering(5, 3) [ ['11', '12', '13'], ['21', '22', '23'], ['31', '32', '33'], ['41', '42', '43'], ['51', '52', '53'] ]
3. Bingo card
The most common Bingo cards are papers with 25 squares arranged in five vertical columns and five side to side rows. A Bingo card is assumed to be valid if:
- in the leftmost column, there are numbers from 1 to 15,
- in the next column (in the second column), there are numbers from 16 to 30, and so on;
- in the rightmost column, there are numbers from 61 to 75.
An example of the correct Bingo card is
1 30 34 55 75 10 16 40 50 67 5 20 38 48 61 4 26 43 49 70 15 17 33 51 66
An example of the incorrect Bingo card is
1 30 34 55 76 10 16 40 50 67 5 20 38 48 61 4 26 43 49 70 15 17 33 51 66
Write a program that reads numbers of a Bingo card from the file bingo.txt (in the file, there are 5 rows with 5 numbers in each row delimited by a space). The program has to check whether the Bingo card is valid or not.
4. Tic Tac Toe
In this task, create a variable matrix for a list of lists. The matrix represents the status of one turn in the Tic Tac Toe game. An example is shown below:
matrix = [['O',' ','X'], ['O','X',' '], ['X',' ',' ']]
The program has to print out:
- O if player O has won,
- X if player X has won, and
- ? if the game is not finished or it is a draw.
For instance, for the game status shown above, the result is X.
What will be printed out if the game status is as follows:
[['O',' ','X'], ['O',' ',' '], ['X',' ',' ']]
Hint.
- Declare a function same_three with a parameter for a list with three elements. The function has to return True if there are three O-s or three X-s in the list.
- Using a loop and the declared function, check each row if its three elements are the same.
- Using another loop and the above-declared function, check each column of the matrix if its three elements are the same.
- Using the declared function, check each diagonal of the matrix if its three elements are the same.
5.* Sudoku
An example of the Sudoku puzzle with 9 rows and 9 columns and its solution can be found here.
Write a program that
- prompts the user for a file name; there should be 9 lines with 9 numbers on each line delimited by a space in the file,
- reads the numbers from the file and composes a matrix (a list of the list) of the numbers,
- checks if the Sudoku puzzle is solved correctly.
In your program, you can integrate the function subgrids prepared by us (see below). The function checks if a 3×3 subgrid is solved correctly. Study how the function works and use it in your program.
def subgrids(mx): # Check every 3x3 subgrid for row_corner in range(0, 9, 3): for column_corner in range(0, 9, 3): # For every subgrid collect all its elements into list 'subgrid' subgrid= [] for i in range(3): for j in range(3): subgrid.append(int(mx[row_corner + i][column_corner + j])) # And check if all elements are correct if sorted(subgrid) != list(range(1, 10)): return False return True
In your program, you have to add functions that check the rows and the columns if they are solved correctly. The above-mentioned function can give you clues in the validation of the numbers in a list (the numbers in the list must be unique and in the range of 1-9).
Examples of the program output:
Example 1
If the content of the file correct.txt is as follows:
3 2 7 4 8 1 6 5 9 1 8 9 3 6 5 7 2 4 6 5 4 2 7 9 8 1 3 7 9 8 1 3 2 5 4 6 5 6 3 9 4 7 2 8 1 2 4 1 6 5 8 3 9 7 8 1 2 7 9 3 4 6 5 4 7 5 8 1 6 9 3 2 9 3 6 5 2 4 1 7 8
then the program output should be:
Enter file name: correct.txt OK
Example 2
If the content of the file incorrect.txt is as follows:
3 2 7 4 8 1 6 5 9 1 8 9 3 6 5 7 2 4 6 5 4 2 7 9 8 1 3 7 9 8 1 3 2 5 4 6 5 6 3 9 4 7 2 8 1 1 4 1 6 5 8 3 9 7 8 1 2 7 9 3 4 6 5 4 7 5 8 1 6 9 3 2 9 3 6 5 2 4 1 7 8
then the program output is:
Enter file name: incorrect.txt NOT OK