Skip to content

Commit 6c3a54a

Browse files
Exercises 5.2 finished
1 parent 35d7e8d commit 6c3a54a

File tree

6 files changed

+263
-0
lines changed

6 files changed

+263
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Please write a function named double_items(numbers: list), which takes a list of integers as its argument.
2+
3+
# The function should return a new list, which contains all values from the original list doubled.
4+
# The function should not change the original list.
5+
6+
# An example of the function at work:
7+
8+
# if __name__ == "__main__":
9+
# numbers = [2, 4, 5, 3, 11, -4]
10+
# numbers_doubled = double_items(numbers)
11+
# print("original:", numbers)
12+
# print("doubled:", numbers_doubled)
13+
14+
# original: [2, 4, 5, 3, 11, -4]
15+
# doubled: [4, 8, 10, 6, 22, -8]
16+
17+
def double_items(numbers: list):
18+
new_list = []
19+
for i in range(len(numbers)):
20+
new_list.append(numbers[i] * 2)
21+
return new_list
22+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Please write a function named remove_smallest(numbers: list), which takes a list of integers as its argument.
2+
3+
# The functions should find and remove the smallest item in the list.
4+
# You may assume there is a single smallest item in the list.
5+
6+
# The function should not have a return value - it should directly modify the list it receives as a parameter.
7+
8+
# An example of how the function works:
9+
10+
# if __name__ == "__main__":
11+
# numbers = [2, 4, 6, 1, 3, 5]
12+
# remove_smallest(numbers)
13+
# print(numbers)
14+
15+
# [2, 4, 6, 3, 5]
16+
17+
def remove_smallest(numbers: list):
18+
small = numbers[0]
19+
for item in numbers:
20+
if item < small:
21+
small = item
22+
numbers.remove(small)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# This is the very last sudoku task.
2+
# This time we will create a slightly different version of the function for adding new numbers to the grid.
3+
4+
# The function copy_and_add(sudoku: list, row_no: int, column_no: int, number: int) takes a two-dimensional
5+
# array representing a sudoku grid, two integers referring to the row and column indexes of a single square, and a single digit between 1 and 9, as its arguments.
6+
# The function should return a copy of the original grid with the new digit added in the correct location.
7+
# The function should not change the original grid received as a parameter.
8+
9+
# The print_sudoku function from the previous exercise could be useful for testing, and it is used in the example below:
10+
11+
# sudoku = [
12+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
13+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
14+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
15+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
16+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
17+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
18+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
19+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
20+
# [0, 0, 0, 0, 0, 0, 0, 0, 0]
21+
# ]
22+
23+
# grid_copy = copy_and_add(sudoku, 0, 0, 2)
24+
# print("Original:")
25+
# print_sudoku(sudoku)
26+
# print()
27+
# print("Copy:")
28+
# print_sudoku(grid_copy)
29+
30+
# Original:
31+
# _ _ _ _ _ _ _ _ _
32+
# _ _ _ _ _ _ _ _ _
33+
# _ _ _ _ _ _ _ _ _
34+
35+
# _ _ _ _ _ _ _ _ _
36+
# _ _ _ _ _ _ _ _ _
37+
# _ _ _ _ _ _ _ _ _
38+
39+
# _ _ _ _ _ _ _ _ _
40+
# _ _ _ _ _ _ _ _ _
41+
# _ _ _ _ _ _ _ _ _
42+
43+
# Copy:
44+
# 2 _ _ _ _ _ _ _ _
45+
# _ _ _ _ _ _ _ _ _
46+
# _ _ _ _ _ _ _ _ _
47+
48+
# _ _ _ _ _ _ _ _ _
49+
# _ _ _ _ _ _ _ _ _
50+
# _ _ _ _ _ _ _ _ _
51+
52+
# _ _ _ _ _ _ _ _ _
53+
# _ _ _ _ _ _ _ _ _
54+
# _ _ _ _ _ _ _ _ _
55+
56+
# Hint When dealing with nested lists you should be extra careful when copying.
57+
# What all needs to be explicitly copied, and where do changes actually have an effect?
58+
# The visualisation tool is a great help here, too, although the size of the sudoku grid will make the view less orderly than usual.
59+
60+
def copy_and_add(sudoku: list, row_no: int, column_no: int, number: int):
61+
new_sudoku = []
62+
for i in range(len(sudoku)):
63+
row = sudoku[i][:]
64+
new_sudoku.append(row)
65+
new_sudoku[row_no][column_no] = number
66+
return new_sudoku
67+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# In this exercise we will complete two more functions for the sudoku project from the previous section: print_sudoku and add_number.
2+
3+
# The function print_sudoku(sudoku: list) takes a two-dimensional array representing a sudoku grid as its argument.
4+
# The function should print out the grid in the format specified in the examples below.
5+
6+
# The function add_number(sudoku: list, row_no: int, column_no: int, number:int) takes a two-dimensional array representing a sudoku grid,
7+
# two integers referring to the row and column indexes of a single square, and a single digit between 1 and 9, as its arguments.
8+
# The function should add the digit to the specified location in the grid.
9+
10+
# sudoku = [
11+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
12+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
13+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
14+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
15+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
16+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
17+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
18+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
19+
# [0, 0, 0, 0, 0, 0, 0, 0, 0]
20+
# ]
21+
22+
# print_sudoku(sudoku)
23+
# add_number(sudoku, 0, 0, 2)
24+
# add_number(sudoku, 1, 2, 7)
25+
# add_number(sudoku, 5, 7, 3)
26+
# print()
27+
# print("Three numbers added:")
28+
# print()
29+
# print_sudoku(sudoku)
30+
31+
# _ _ _ _ _ _ _ _ _
32+
# _ _ _ _ _ _ _ _ _
33+
# _ _ _ _ _ _ _ _ _
34+
35+
# _ _ _ _ _ _ _ _ _
36+
# _ _ _ _ _ _ _ _ _
37+
# _ _ _ _ _ _ _ _ _
38+
39+
# _ _ _ _ _ _ _ _ _
40+
# _ _ _ _ _ _ _ _ _
41+
# _ _ _ _ _ _ _ _ _
42+
43+
# Three numbers added:
44+
45+
# 2 _ _ _ _ _ _ _ _
46+
# _ _ 7 _ _ _ _ _ _
47+
# _ _ _ _ _ _ _ _ _
48+
49+
# _ _ _ _ _ _ _ _ _
50+
# _ _ _ _ _ _ _ _ _
51+
# _ _ _ _ _ _ _ 3 _
52+
53+
# _ _ _ _ _ _ _ _ _
54+
# _ _ _ _ _ _ _ _ _
55+
# _ _ _ _ _ _ _ _ _
56+
57+
# Hint
58+
59+
# Remember it is possible to call the print function without causing a line change:
60+
61+
# print("characters ", end="")
62+
# print("without carriage returns", end="")
63+
64+
# characters without carriage returns
65+
66+
# Sometimes you need just a new line, which a print statement without any argument will achieve:
67+
68+
# print()
69+
70+
def print_sudoku(sudoku: list):
71+
row_count = 1
72+
for row in sudoku:
73+
col_count = 1
74+
for item in row:
75+
if item == 0:
76+
print("_ ", end="")
77+
else:
78+
print(f"{item} ", end="")
79+
if col_count == 3 or col_count == 6:
80+
print(" ", end="")
81+
col_count += 1
82+
if row_count == 3 or row_count == 6:
83+
print()
84+
row_count += 1
85+
print()
86+
87+
def add_number(sudoku: list, row_no: int, column_no: int, number:int):
88+
sudoku[row_no][column_no] = number
89+

part5/2. References/tic_tac_toe.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Tic-Tac-Toe is played on a 3 by 3 grid, by two players who take turns inputting noughts and crosses.
2+
# If either player succeeds in placing three of their own symbols on any row, column or diagonal, they win.
3+
# If neither player manages this, it is a draw.
4+
5+
# Please write a function named play_turn(game_board: list, x: int, y: int, piece: str), which places the given symbol at the given coordinates on the board.
6+
# The values of the coordinates on the board are between 0 and 2.
7+
8+
# NB: when compared to the sudoku exercises, the arguments the function takes are the other way around here.
9+
# The column x comes first, and the row y second.
10+
11+
# The board consists of the following strings:
12+
13+
# "": empty square
14+
# "X": player 1 symbol
15+
# "O": player 2 symbol
16+
# The function should return True if the square was empty and the symbol was successfully placed on the game board.
17+
# The function should return False if the square was occupied, or if the coordinates weren't valid.
18+
19+
# An example execution of the function:
20+
21+
# game_board = [["", "", ""], ["", "", ""], ["", "", ""]]
22+
# print(play_turn(game_board, 2, 0, "X"))
23+
# print(game_board)
24+
25+
# True
26+
# [['', '', 'X'], ['', '', ''], ['', '', '']]
27+
28+
def play_turn(game_board: list, x: int, y: int, piece: str):
29+
for row in range(len(game_board)):
30+
for col in range(3):
31+
if x == col and y == row and game_board[y][x] == "":
32+
game_board[y][x] = piece
33+
return True
34+
return False
35+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Please write a function named transpose(matrix: list), which takes a two-dimensional integer array, i.e., a matrix, as its argument.
2+
# The function should transpose the matrix. Transposing means essentially flipping the matrix over its diagonal: columns become rows, and rows become columns.
3+
4+
# You may assume the matrix is a square matrix, so it will have an equal number of rows and columns.
5+
6+
# The following matrix
7+
8+
# 1 2 3
9+
# 4 5 6
10+
# 7 8 9
11+
12+
# transposed looks like this:
13+
14+
# 1 4 7
15+
# 2 5 8
16+
# 3 6 9
17+
18+
# The function should not have a return value.
19+
# The matrix should be modified directly through the reference.
20+
21+
def transpose(matrix: list):
22+
copy_matrix = []
23+
for row in matrix:
24+
copy_matrix.append(row[:])
25+
print(copy_matrix)
26+
for row in range(len(copy_matrix)):
27+
for col in range(len(copy_matrix[0])):
28+
matrix[col][row] = copy_matrix[row][col]

0 commit comments

Comments
 (0)