1 8 15 22 29 36 43
2 9 16 23 30 37 44
3 10 17 24 31 38 45
4 11 18 25 32 39 46
5 12 19 26 33 40 47
6 13 20 27 34 41 48
7 14 21 28 35 42 49
In the 7x7 array given above, select six numbers from six cells simultaneously satisfying the following conditions:
- No two numbers chosen should belong to the same row;
- No two numbers chosen should belong to the same column;
- In the set of numbers chosen, each of the ten digits appears once and only once.
For example, if the numbers selected are 7,9,18,26,30 and 45 then this violates the given conditions as 9 and 30 belong to the same row.
The only way to get 10 digits is with 2 1-digit and 4 2-digit numbers. Due to the column restriction, there must be one of 1,2,..,7 and one of either 8 or 9. The numbers 10 to 14 cannot appear.
The program below finds only one solution:
(6, 8, 17, 25, 30, 49)
---------------------
singles = [i for i in range(1,10)]
doubles = [i for i in range(15,50)]
from itertools import combinations
for sing in combinations(singles,2):
if 8 not in sing and 9 not in sing:
continue
if 8 in sing and 9 in sing:
continue
alpha = str(sing[0]) + str(sing[1])
for doub in combinations(doubles,4):
numbers = sing + doub
beta = str(doub[0]) + str(doub[1]) + str(doub[2]) + str(doub[3])
string = alpha + beta
if len(set(string)) != 10:
continue
rows = [num%7 for num in numbers]
if len(set(rows)) != 6:
continue
cols = [(num-1)//7 for num in numbers]
if len(set(cols)) != 6:
continue
print(numbers,rows,cols)
|
Posted by Larry
on 2024-02-01 10:24:19 |