Ejercicio: 3Eva2010TI_T2 Funciones ocupados y ubica libre en matriz
| ventana | pasillo | pasillo | ventana | ||
| 1 | 2 | ... | 3 | m=4 | |
|---|---|---|---|---|---|
| Fila 1 | 0 | 0 | 0 | 0 | |
| 2 | 0 | 0 | 0 | 0 | |
| 3 | 0 | 0 | 0 | 1 | |
| 4 | 0 | 1 | 0 | 0 | |
| ... | |||||
| 19 | 0 | 0 | 0 | 0 | |
| n=20 | 0 | 0 | 0 | 0 |
resultado obtenido:
matriz = [[1,1,1,1],
[1,0,0,0],
[0,1,0,1]]
ocupados: 7
primer encontrado: [1, 1]
>>>
Algoritmo en Python
# 3Eva_IT2010_T2 Funciones ocupados y ubica libre en matriz
# resolver sin usar numpy
import numpy as np
def ocupados(matriz):
suma = np.sum(matriz)
return(suma)
def ubicalibre(matriz):
matriz = np.array(matriz)
tamano = np.shape(matriz)
n = tamano[0]
m = tamano[1]
encontre = 0
a = np.NaN
b = np.NaN
i = 0
while i<n and encontre==0:
j = 0
while j<m and encontre ==0:
if matriz[i,j] == 0:
encontre = 1
a = i
b = j
j = j+1
i = i + 1
return([a,b])
# PROGRAMA
matriz = [[1,1,1,1],
[1,0,0,0],
[0,1,0,1]]
# PROCEDIMIENTO
aforo = ocupados(matriz)
libre = ubicalibre(matriz)
# SALIDA
print("ocupados: ", aforo)
print('primer encontrado: ',libre)