Table of Contents
Ejercicio final¶
Importamos librerias
import numpy as np
Fuerza bruta¶
Apartado A¶
balls = np.array([ [1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 0]])
balls
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 0]])
def find_solution(balls):
"Fuerza bruta Apartado A (intentando pensar como un novato)"
# Paso el array a 1D para poder tratarlo como una lista
balls = balls.ravel()
sol = []
for a in balls:
for b in balls:
for c in balls:
if a!=b and a!=c and b!=c:
if a!=0 and b!=0 and c!=0:
if a+b+c==28:
sol.append([a,b,c])
return sol
solutions = find_solution(balls)
solutions
[[1, 12, 15],
[1, 13, 14],
[1, 14, 13],
[1, 15, 12],
[2, 11, 15],
[2, 12, 14],
[2, 14, 12],
[2, 15, 11],
[3, 10, 15],
[3, 11, 14],
[3, 12, 13],
[3, 13, 12],
[3, 14, 11],
[3, 15, 10],
[4, 9, 15],
[4, 10, 14],
[4, 11, 13],
[4, 13, 11],
[4, 14, 10],
[4, 15, 9],
[5, 8, 15],
[5, 9, 14],
[5, 10, 13],
[5, 11, 12],
[5, 12, 11],
[5, 13, 10],
[5, 14, 9],
[5, 15, 8],
[6, 7, 15],
[6, 8, 14],
[6, 9, 13],
[6, 10, 12],
[6, 12, 10],
[6, 13, 9],
[6, 14, 8],
[6, 15, 7],
[7, 6, 15],
[7, 8, 13],
[7, 9, 12],
[7, 10, 11],
[7, 11, 10],
[7, 12, 9],
[7, 13, 8],
[7, 15, 6],
[8, 5, 15],
[8, 6, 14],
[8, 7, 13],
[8, 9, 11],
[8, 11, 9],
[8, 13, 7],
[8, 14, 6],
[8, 15, 5],
[9, 4, 15],
[9, 5, 14],
[9, 6, 13],
[9, 7, 12],
[9, 8, 11],
[9, 11, 8],
[9, 12, 7],
[9, 13, 6],
[9, 14, 5],
[9, 15, 4],
[10, 3, 15],
[10, 4, 14],
[10, 5, 13],
[10, 6, 12],
[10, 7, 11],
[10, 11, 7],
[10, 12, 6],
[10, 13, 5],
[10, 14, 4],
[10, 15, 3],
[11, 2, 15],
[11, 3, 14],
[11, 4, 13],
[11, 5, 12],
[11, 7, 10],
[11, 8, 9],
[11, 9, 8],
[11, 10, 7],
[11, 12, 5],
[11, 13, 4],
[11, 14, 3],
[11, 15, 2],
[12, 1, 15],
[12, 2, 14],
[12, 3, 13],
[12, 5, 11],
[12, 6, 10],
[12, 7, 9],
[12, 9, 7],
[12, 10, 6],
[12, 11, 5],
[12, 13, 3],
[12, 14, 2],
[12, 15, 1],
[13, 1, 14],
[13, 3, 12],
[13, 4, 11],
[13, 5, 10],
[13, 6, 9],
[13, 7, 8],
[13, 8, 7],
[13, 9, 6],
[13, 10, 5],
[13, 11, 4],
[13, 12, 3],
[13, 14, 1],
[14, 1, 13],
[14, 2, 12],
[14, 3, 11],
[14, 4, 10],
[14, 5, 9],
[14, 6, 8],
[14, 8, 6],
[14, 9, 5],
[14, 10, 4],
[14, 11, 3],
[14, 12, 2],
[14, 13, 1],
[15, 1, 12],
[15, 2, 11],
[15, 3, 10],
[15, 4, 9],
[15, 5, 8],
[15, 6, 7],
[15, 7, 6],
[15, 8, 5],
[15, 9, 4],
[15, 10, 3],
[15, 11, 2],
[15, 12, 1]]
len(solutions)
132
print(f'Hay {len(solutions)} combinaciones de valores para a,b y c')
Hay 132 combinaciones de valores para a,b y c
Nota: Esto creo que ya es complicado para los alumnos, y tecnicamente no se si se lo hemos pedido, pero puede que alguien se lie por tenerlo en cuenta,
¿Pero cuantas soluciones son únicas? Porque la solucion [1,12,15]
es la misma que [15,12,1]
¿Cuantas combinaciones de bolas podemos formar?
unique_sols = set([frozenset(x) for x in solutions])
unique_sols
{frozenset({3, 11, 14}),
frozenset({4, 10, 14}),
frozenset({3, 12, 13}),
frozenset({7, 10, 11}),
frozenset({1, 13, 14}),
frozenset({5, 11, 12}),
frozenset({6, 8, 14}),
frozenset({5, 8, 15}),
frozenset({6, 7, 15}),
frozenset({7, 9, 12}),
frozenset({3, 10, 15}),
frozenset({1, 12, 15}),
frozenset({7, 8, 13}),
frozenset({4, 9, 15}),
frozenset({5, 10, 13}),
frozenset({6, 10, 12}),
frozenset({2, 11, 15}),
frozenset({4, 11, 13}),
frozenset({5, 9, 14}),
frozenset({2, 12, 14}),
frozenset({8, 9, 11}),
frozenset({6, 9, 13})}
len(unique_sols)
22
Apartado B¶
balls = np.array( [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 0]])
balls
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 0]])
def sol_apartado_b(balls):
solutions = find_solution(balls)
sol_b = []
for sol in solutions:
a,b,c = sol
i= np.where(balls==a)
j= np.where(balls==b)
k= np.where(balls==c)
if i[0]!=j[0] and i[0]!=k[0] and j[0]!=k[0]:
print
sol_b.append([a,b,c])
return sol_b
sol_b = sol_apartado_b(balls)
sol_b
[[1, 12, 15],
[1, 15, 12],
[2, 11, 15],
[2, 12, 14],
[2, 14, 12],
[2, 15, 11],
[3, 10, 15],
[3, 11, 14],
[3, 12, 13],
[3, 13, 12],
[3, 14, 11],
[3, 15, 10],
[4, 9, 15],
[4, 10, 14],
[4, 11, 13],
[4, 13, 11],
[4, 14, 10],
[4, 15, 9],
[5, 9, 14],
[5, 10, 13],
[5, 13, 10],
[5, 14, 9],
[6, 9, 13],
[6, 13, 9],
[9, 4, 15],
[9, 5, 14],
[9, 6, 13],
[9, 13, 6],
[9, 14, 5],
[9, 15, 4],
[10, 3, 15],
[10, 4, 14],
[10, 5, 13],
[10, 13, 5],
[10, 14, 4],
[10, 15, 3],
[11, 2, 15],
[11, 3, 14],
[11, 4, 13],
[11, 13, 4],
[11, 14, 3],
[11, 15, 2],
[12, 1, 15],
[12, 2, 14],
[12, 3, 13],
[12, 13, 3],
[12, 14, 2],
[12, 15, 1],
[13, 3, 12],
[13, 4, 11],
[13, 5, 10],
[13, 6, 9],
[13, 9, 6],
[13, 10, 5],
[13, 11, 4],
[13, 12, 3],
[14, 2, 12],
[14, 3, 11],
[14, 4, 10],
[14, 5, 9],
[14, 9, 5],
[14, 10, 4],
[14, 11, 3],
[14, 12, 2],
[15, 1, 12],
[15, 2, 11],
[15, 3, 10],
[15, 4, 9],
[15, 9, 4],
[15, 10, 3],
[15, 11, 2],
[15, 12, 1]]
len(sol_b)
72
unique_sols_b = set([frozenset(x) for x in sol_b])
unique_sols_b
{frozenset({3, 11, 14}),
frozenset({5, 9, 14}),
frozenset({2, 12, 14}),
frozenset({4, 10, 14}),
frozenset({5, 10, 13}),
frozenset({3, 12, 13}),
frozenset({3, 10, 15}),
frozenset({2, 11, 15}),
frozenset({1, 12, 15}),
frozenset({4, 11, 13}),
frozenset({6, 9, 13}),
frozenset({4, 9, 15})}
len(unique_sols_b)
12
Creo que esta forma de pensar es muy complicada para el primer ejercicio :S
Utilizando Librerias adecuadas¶
Apartado A¶
from itertools import combinations
arr = np.arange(1,15+1)
arr
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
comb=list(combinations(arr, r=3))
comb
[(1, 2, 3),
(1, 2, 4),
(1, 2, 5),
(1, 2, 6),
(1, 2, 7),
(1, 2, 8),
(1, 2, 9),
(1, 2, 10),
(1, 2, 11),
(1, 2, 12),
(1, 2, 13),
(1, 2, 14),
(1, 2, 15),
(1, 3, 4),
(1, 3, 5),
(1, 3, 6),
(1, 3, 7),
(1, 3, 8),
(1, 3, 9),
(1, 3, 10),
(1, 3, 11),
(1, 3, 12),
(1, 3, 13),
(1, 3, 14),
(1, 3, 15),
(1, 4, 5),
(1, 4, 6),
(1, 4, 7),
(1, 4, 8),
(1, 4, 9),
(1, 4, 10),
(1, 4, 11),
(1, 4, 12),
(1, 4, 13),
(1, 4, 14),
(1, 4, 15),
(1, 5, 6),
(1, 5, 7),
(1, 5, 8),
(1, 5, 9),
(1, 5, 10),
(1, 5, 11),
(1, 5, 12),
(1, 5, 13),
(1, 5, 14),
(1, 5, 15),
(1, 6, 7),
(1, 6, 8),
(1, 6, 9),
(1, 6, 10),
(1, 6, 11),
(1, 6, 12),
(1, 6, 13),
(1, 6, 14),
(1, 6, 15),
(1, 7, 8),
(1, 7, 9),
(1, 7, 10),
(1, 7, 11),
(1, 7, 12),
(1, 7, 13),
(1, 7, 14),
(1, 7, 15),
(1, 8, 9),
(1, 8, 10),
(1, 8, 11),
(1, 8, 12),
(1, 8, 13),
(1, 8, 14),
(1, 8, 15),
(1, 9, 10),
(1, 9, 11),
(1, 9, 12),
(1, 9, 13),
(1, 9, 14),
(1, 9, 15),
(1, 10, 11),
(1, 10, 12),
(1, 10, 13),
(1, 10, 14),
(1, 10, 15),
(1, 11, 12),
(1, 11, 13),
(1, 11, 14),
(1, 11, 15),
(1, 12, 13),
(1, 12, 14),
(1, 12, 15),
(1, 13, 14),
(1, 13, 15),
(1, 14, 15),
(2, 3, 4),
(2, 3, 5),
(2, 3, 6),
(2, 3, 7),
(2, 3, 8),
(2, 3, 9),
(2, 3, 10),
(2, 3, 11),
(2, 3, 12),
(2, 3, 13),
(2, 3, 14),
(2, 3, 15),
(2, 4, 5),
(2, 4, 6),
(2, 4, 7),
(2, 4, 8),
(2, 4, 9),
(2, 4, 10),
(2, 4, 11),
(2, 4, 12),
(2, 4, 13),
(2, 4, 14),
(2, 4, 15),
(2, 5, 6),
(2, 5, 7),
(2, 5, 8),
(2, 5, 9),
(2, 5, 10),
(2, 5, 11),
(2, 5, 12),
(2, 5, 13),
(2, 5, 14),
(2, 5, 15),
(2, 6, 7),
(2, 6, 8),
(2, 6, 9),
(2, 6, 10),
(2, 6, 11),
(2, 6, 12),
(2, 6, 13),
(2, 6, 14),
(2, 6, 15),
(2, 7, 8),
(2, 7, 9),
(2, 7, 10),
(2, 7, 11),
(2, 7, 12),
(2, 7, 13),
(2, 7, 14),
(2, 7, 15),
(2, 8, 9),
(2, 8, 10),
(2, 8, 11),
(2, 8, 12),
(2, 8, 13),
(2, 8, 14),
(2, 8, 15),
(2, 9, 10),
(2, 9, 11),
(2, 9, 12),
(2, 9, 13),
(2, 9, 14),
(2, 9, 15),
(2, 10, 11),
(2, 10, 12),
(2, 10, 13),
(2, 10, 14),
(2, 10, 15),
(2, 11, 12),
(2, 11, 13),
(2, 11, 14),
(2, 11, 15),
(2, 12, 13),
(2, 12, 14),
(2, 12, 15),
(2, 13, 14),
(2, 13, 15),
(2, 14, 15),
(3, 4, 5),
(3, 4, 6),
(3, 4, 7),
(3, 4, 8),
(3, 4, 9),
(3, 4, 10),
(3, 4, 11),
(3, 4, 12),
(3, 4, 13),
(3, 4, 14),
(3, 4, 15),
(3, 5, 6),
(3, 5, 7),
(3, 5, 8),
(3, 5, 9),
(3, 5, 10),
(3, 5, 11),
(3, 5, 12),
(3, 5, 13),
(3, 5, 14),
(3, 5, 15),
(3, 6, 7),
(3, 6, 8),
(3, 6, 9),
(3, 6, 10),
(3, 6, 11),
(3, 6, 12),
(3, 6, 13),
(3, 6, 14),
(3, 6, 15),
(3, 7, 8),
(3, 7, 9),
(3, 7, 10),
(3, 7, 11),
(3, 7, 12),
(3, 7, 13),
(3, 7, 14),
(3, 7, 15),
(3, 8, 9),
(3, 8, 10),
(3, 8, 11),
(3, 8, 12),
(3, 8, 13),
(3, 8, 14),
(3, 8, 15),
(3, 9, 10),
(3, 9, 11),
(3, 9, 12),
(3, 9, 13),
(3, 9, 14),
(3, 9, 15),
(3, 10, 11),
(3, 10, 12),
(3, 10, 13),
(3, 10, 14),
(3, 10, 15),
(3, 11, 12),
(3, 11, 13),
(3, 11, 14),
(3, 11, 15),
(3, 12, 13),
(3, 12, 14),
(3, 12, 15),
(3, 13, 14),
(3, 13, 15),
(3, 14, 15),
(4, 5, 6),
(4, 5, 7),
(4, 5, 8),
(4, 5, 9),
(4, 5, 10),
(4, 5, 11),
(4, 5, 12),
(4, 5, 13),
(4, 5, 14),
(4, 5, 15),
(4, 6, 7),
(4, 6, 8),
(4, 6, 9),
(4, 6, 10),
(4, 6, 11),
(4, 6, 12),
(4, 6, 13),
(4, 6, 14),
(4, 6, 15),
(4, 7, 8),
(4, 7, 9),
(4, 7, 10),
(4, 7, 11),
(4, 7, 12),
(4, 7, 13),
(4, 7, 14),
(4, 7, 15),
(4, 8, 9),
(4, 8, 10),
(4, 8, 11),
(4, 8, 12),
(4, 8, 13),
(4, 8, 14),
(4, 8, 15),
(4, 9, 10),
(4, 9, 11),
(4, 9, 12),
(4, 9, 13),
(4, 9, 14),
(4, 9, 15),
(4, 10, 11),
(4, 10, 12),
(4, 10, 13),
(4, 10, 14),
(4, 10, 15),
(4, 11, 12),
(4, 11, 13),
(4, 11, 14),
(4, 11, 15),
(4, 12, 13),
(4, 12, 14),
(4, 12, 15),
(4, 13, 14),
(4, 13, 15),
(4, 14, 15),
(5, 6, 7),
(5, 6, 8),
(5, 6, 9),
(5, 6, 10),
(5, 6, 11),
(5, 6, 12),
(5, 6, 13),
(5, 6, 14),
(5, 6, 15),
(5, 7, 8),
(5, 7, 9),
(5, 7, 10),
(5, 7, 11),
(5, 7, 12),
(5, 7, 13),
(5, 7, 14),
(5, 7, 15),
(5, 8, 9),
(5, 8, 10),
(5, 8, 11),
(5, 8, 12),
(5, 8, 13),
(5, 8, 14),
(5, 8, 15),
(5, 9, 10),
(5, 9, 11),
(5, 9, 12),
(5, 9, 13),
(5, 9, 14),
(5, 9, 15),
(5, 10, 11),
(5, 10, 12),
(5, 10, 13),
(5, 10, 14),
(5, 10, 15),
(5, 11, 12),
(5, 11, 13),
(5, 11, 14),
(5, 11, 15),
(5, 12, 13),
(5, 12, 14),
(5, 12, 15),
(5, 13, 14),
(5, 13, 15),
(5, 14, 15),
(6, 7, 8),
(6, 7, 9),
(6, 7, 10),
(6, 7, 11),
(6, 7, 12),
(6, 7, 13),
(6, 7, 14),
(6, 7, 15),
(6, 8, 9),
(6, 8, 10),
(6, 8, 11),
(6, 8, 12),
(6, 8, 13),
(6, 8, 14),
(6, 8, 15),
(6, 9, 10),
(6, 9, 11),
(6, 9, 12),
(6, 9, 13),
(6, 9, 14),
(6, 9, 15),
(6, 10, 11),
(6, 10, 12),
(6, 10, 13),
(6, 10, 14),
(6, 10, 15),
(6, 11, 12),
(6, 11, 13),
(6, 11, 14),
(6, 11, 15),
(6, 12, 13),
(6, 12, 14),
(6, 12, 15),
(6, 13, 14),
(6, 13, 15),
(6, 14, 15),
(7, 8, 9),
(7, 8, 10),
(7, 8, 11),
(7, 8, 12),
(7, 8, 13),
(7, 8, 14),
(7, 8, 15),
(7, 9, 10),
(7, 9, 11),
(7, 9, 12),
(7, 9, 13),
(7, 9, 14),
(7, 9, 15),
(7, 10, 11),
(7, 10, 12),
(7, 10, 13),
(7, 10, 14),
(7, 10, 15),
(7, 11, 12),
(7, 11, 13),
(7, 11, 14),
(7, 11, 15),
(7, 12, 13),
(7, 12, 14),
(7, 12, 15),
(7, 13, 14),
(7, 13, 15),
(7, 14, 15),
(8, 9, 10),
(8, 9, 11),
(8, 9, 12),
(8, 9, 13),
(8, 9, 14),
(8, 9, 15),
(8, 10, 11),
(8, 10, 12),
(8, 10, 13),
(8, 10, 14),
(8, 10, 15),
(8, 11, 12),
(8, 11, 13),
(8, 11, 14),
(8, 11, 15),
(8, 12, 13),
(8, 12, 14),
(8, 12, 15),
(8, 13, 14),
(8, 13, 15),
(8, 14, 15),
(9, 10, 11),
(9, 10, 12),
(9, 10, 13),
(9, 10, 14),
(9, 10, 15),
(9, 11, 12),
(9, 11, 13),
(9, 11, 14),
(9, 11, 15),
(9, 12, 13),
(9, 12, 14),
(9, 12, 15),
(9, 13, 14),
(9, 13, 15),
(9, 14, 15),
(10, 11, 12),
(10, 11, 13),
(10, 11, 14),
(10, 11, 15),
(10, 12, 13),
(10, 12, 14),
(10, 12, 15),
(10, 13, 14),
(10, 13, 15),
(10, 14, 15),
(11, 12, 13),
(11, 12, 14),
(11, 12, 15),
(11, 13, 14),
(11, 13, 15),
(11, 14, 15),
(12, 13, 14),
(12, 13, 15),
(12, 14, 15),
(13, 14, 15)]
sol_A = []
for posible_sol in comb:
a,b,c = posible_sol
if a!=0 and b!=0 and c!=0:
if a+b+c==28:
sol_A.append([a,b,c])
sol_A
[[1, 12, 15],
[1, 13, 14],
[2, 11, 15],
[2, 12, 14],
[3, 10, 15],
[3, 11, 14],
[3, 12, 13],
[4, 9, 15],
[4, 10, 14],
[4, 11, 13],
[5, 8, 15],
[5, 9, 14],
[5, 10, 13],
[5, 11, 12],
[6, 7, 15],
[6, 8, 14],
[6, 9, 13],
[6, 10, 12],
[7, 8, 13],
[7, 9, 12],
[7, 10, 11],
[8, 9, 11]]
len(sol_A)
22
Apartado B¶
balls = np.array([ [1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 0]])
balls
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 0]])
# Es la misma que está arriba
def sol_apartado_b(balls):
solutions = find_solution(balls)
sol_b = []
for sol in solutions:
a,b,c = sol
i= np.where(balls==a)
j= np.where(balls==b)
k= np.where(balls==c)
if i[0]!=j[0] and i[0]!=k[0] and j[0]!=k[0]:
print
sol_b.append([a,b,c])
return sol_b