Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d63344b

Browse files
committed
exercises
1 parent 6ff87ab commit d63344b

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def matrix(r,c):
2+
"""create a list of c amount of 0's for an index in range of 0 to value of rows"""
3+
"""creates a list of c amount of 0's for an amount of r rows"""
4+
data=[[0] * c for j in range(r)]
5+
print(len(data[0])) # gives length of elements in columns
6+
print(len(data)) # gives length of rows / length of the lists inside the list
7+
return data
8+
9+
print(matrix(4,3))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""Function for the sum of 2 lists with the same size"""
2+
def sum(list1, list2):
3+
total=[[0] * len(list1) for j in range(len(list1[0]))]
4+
if len(list1) == len(list2) and len(list1[0]) == len(list2[0]):
5+
for i in range(len(list2)):
6+
for j in range(len(list2[0])):
7+
total[i][j]= list1[i][j]+list2[i][j]
8+
else:
9+
raise ValueError('The lists do not have the same size')
10+
11+
return total
12+
13+
list1=[[1,1], [1,1], [1,1]]
14+
list2=[[1,1], [1,1]]
15+
16+
print(sum(list1, list2))
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class TicTacToe:
2+
def __init__(self):
3+
self._board= [[' '] *3 for i in range(3)] # creates a list of 3 lists containing 3 empty strings
4+
self._player='X' # X is what we use to mark our spot in the game
5+
6+
def mark(self,i,j):
7+
"""Function that will place the mark in the matrix based on the players position"""
8+
9+
if not (0<= i <= 2 and 0<=j<=2): #if player puts mark out of matrix bounds, raise error
10+
raise ValueError('Invalid board position')
11+
12+
if self._board[i][j] != ' ': # if player puts a mark in a position that is not empty raise error
13+
raise ValueError('Board Position taken')
14+
15+
if self.winner() is not None:
16+
raise ValueError('Game is already complete')
17+
18+
self._board[i][j]=self._player #we assign to position i j the character X onto the empty value of the board
19+
20+
if self._player== 'X':
21+
self._player=='O'
22+
else:
23+
self._player=='X'
24+
25+
def _is_win(self, mark):
26+
"""Check whether the board config is a win for a given player or a tie"""
27+
board= self._board # create empty matrix as the board
28+
29+
return(mark == board[0][0] == board[0][1] == board[0][2] or # row 0
30+
mark == board[1][0] == board[1][1] == board[1][2] or # row 1
31+
mark == board[2][0] == board[2][1] == board[2][2] or # row 2
32+
mark == board[0][0] == board[1][0] == board[2][0] or # column 0
33+
mark == board[0][1] == board[1][1] == board[2][1] or # column 1
34+
mark == board[0][2] == board[1][2] == board[2][2] or # column 2
35+
mark == board[0][0] == board[1][1] == board[2][2] or # diagonal
36+
mark == board[0][2] == board[1][1] == board[2][0]) #reverse diagonal
37+
38+
def winner(self):
39+
for mark in 'XO':
40+
if self._is_win(mark):
41+
return mark
42+
43+
return None
44+
45+
def __str__(self):
46+
"""Return string representation of current game board"""
47+
48+
rows=['|'.join(self._board[r]) for r in range(3)]
49+
return'\n---------\n'.join(rows)
50+
51+
game=TicTacToe()
52+
53+
game.mark(1,1)
54+
#need for more marks
55+
print(game)

PythonDataStructsAndAlgo/Notes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,4 @@
159159
> 2-D arrays can be also called matrices.
160160
161161
> *A common representation for a 2-D list in Python is a list of lists*
162-
>> Example: data=[[22,18,709,5,33], [45,32,830,120,750], [4,880,45,66,61]] which is a matrix of 3 x 4 3 rows, and 4 columns
162+
>> Example: data=[[22,18,709,5,33], [45,32,830,120,750], [4,880,45,66,61]] which is a matrix of 3 x 4 3 rows, and 4 columns. Meaning data[1][3] is the number

0 commit comments

Comments
 (0)