During session 10
Exercises
NB! The practical session exercises are always provided enough and to spare with an eye to fast / experienced students have exercises to solve. To this end, do not get upset if you do not get on all the task in class. Solve as many problems as you can and submit your work in Moodle under "Practical 10". The rest can be solved at home.
1. Direct product
Write a program that for any two arrays x and y prints out a table that lists all pairs that can be formed by "pairing" the elements of these arrays:
x[0]y[0] x[0]y[1] x[0]y[2] ... x[0]y[l] x[1]y[0] x[1]y[1] x[1]y[2] ... x[1]y[l] . . . . . . . . . . . . . . . . . . . x[k]y[0] x[k]y[1] x[k]y[2] ... x[k]y[l]
Test your program by printing out all combinations of the arrays of card strengths and suits:
x = ['A', 'K', 'Q', 'J', '10', '9', '8', '7', '6', '5', '4', '3', '2'] y = ['♥', '♦', '♠', '♣']
In mathematics, this operation is called direct product (https://en.wikipedia.org/wiki/Direct_product).
2. Tic-tac-toe
In this task, create a variable matrix for a list of lists. The matrix represents the status in the Tic-tac-toe game. An example is shown below:
matrix = [['O',' ','X'], ['O','X',' '], ['X',' ',' ']]
Write a function has_won, which takes two parameters: a matrix that represents game field as described above, and a symbol that is either 'X' or 'O'. The function returns the value True, if the given symbol has won the game, and False if the symbol has not won the game.
For example, with the matrix as above we get
>>> has_won(matrix, 'X') True >>> has_won(matrix, 'O') False
- Declare a function same_three with two parameters: a list with three elements and a symbol. The function has to return True if there are three symbols 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.
Next write a function find_win which also takes two parameters: a matrix and a symbol. If the given symbol can win the game in one move, then the function prints out the game field after winning move. If winning in one move is not possible, then the function prints out the corresponding message.
For example,
>>> find_win([['O',' ','X'], [' ','O','O'], ['X',' ','X']], 'X') Player X can win: O X O O X X X >>> find_win([[' ',' ','X'], ['X','O','O'], ['X',' ',' ']], 'O') Player O cannot win.
Look through all positions in the matrix. If the position is empty, make a copy of the matrix, add the given symbol to this position and check using the function has_won whether the symbol has won.
3. Queue in a food store
A number of people stand in a food store queue. An array contains the numbers of products in their baskets. Write a program that for each person in the queue finds the number of people in front of him/her that have more than three products in their baskets.
Enter the numbers of products in baskets: 5 2 1 8 21 7 2 4 There are 0 persons with more than three products in front of person 1. There is 1 person with more than three products in front of person 2. There is 1 person with more than three products in front of person 3. There is 1 person with more than three products in front of person 4. There are 2 persons with more than three products in front of person 5. There are 3 persons with more than three products in front of person 6. There are 4 persons with more than three products in front of person 7. There are 4 persons with more than three products in front of person 8.
4. 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 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 which 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).
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