-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.py
More file actions
60 lines (50 loc) · 2.01 KB
/
grid.py
File metadata and controls
60 lines (50 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Grid es un array de dos dimensiones o Matriz tal como lo aprendí
# en UADE.
from my_array import Array
import random
class Grid():
# Metodo constructor del Grid
def __init__(self, rows, columns, fill_value=None):
self.data = Array(rows)
for row in range(rows):
self.data[row] = Array(columns, fill_value)
# Metodo para obtener la altura del Grid = cant de filas:
def __getheight__(self):
return len(self.data)
# Metodo para obtener el ancho del Grid = cant de columnas:
def __getwidth__(self):
return len(self.data[0])
# Metodo para obtener un valor en particular:
def __getitem__(self, r_index, c_index):
return self.data[r_index][c_index]
# Metodo para cambiar un valor en particular:
def __setitem__(self, r_index, c_index, value):
self.data[r_index][c_index] = value
# Metodo para devolver los valores en string:
def __str__(self):
result = ""
for row in range(self.__getheight__()):
if result != "":
result += "\n"
for column in range(self.__getwidth__()):
if self.data[row][column] == None:
result += str(self.data[row][column]) + " | "
else:
if self.data[row][column] < 10:
result += " " + str(self.data[row][column]) + " | "
else:
result += str(self.data[row][column]) + " | "
return result
# Primer reto:
# Metodo para rellenar con numeros random:
def __randomdata__(self):
for row in range(self.__getheight__()):
for column in range(self.__getwidth__()):
self.data[row][column] = random.randint(0,100)
# Metodo para sumar todos los valores:
def __sumdata__(self):
sum = 0
for row in range(self.__getheight__()):
for column in range(self.__getwidth__()):
sum += self.data[row][column]
return sum