[Tutor] Filling an array - with a twist

boB Stepp robertvstepp at gmail.com
Wed Nov 11 22:53:54 EST 2020


On Wed, Nov 11, 2020 at 04:30:54PM +1000, Phil wrote:
>Another call on the Brains Trust I'm afraid.
>
>I'd like to fill an 8 x 8 array (list of lists) with random numbers 
>such that no column or row repeats the same number more than twice in 
>a sequence.
>
>e.g.. [12344562] is OK but [12223456] is not wanted.
>
>Something like the following will give me a row or column that meets 
>the requirements but more often than not fails the complete array 
>test.
>
>>>> import random
>>>> r = random.sample(range(0,8),8)
>>>> r
>[6, 2, 4, 5, 3, 0, 1, 7]
>
>Searching through the array trying to repair an errant sequence is not 
>the answer. Can anyone suggest an algorithm to do what I need?
>
>To add to the complication I really need numbers between 0 and 6 which 
>random.sample() won't allow because, as the value error states, the 
>sample is larger than the population.

I found this problem interesting this evening, so I thought I would throw
out my effort at a solution:

=======================================================================================
from pprint import pprint
from random import randint

NUM_ROWS = 8
NUM_COLS = 8
MIN_INTEGER = 0
MAX_INTEGER = 6
MAX_REPETITIONS = 2

row_ck = [None, None]
col_ck = [None, None]
L = []
for i in range(NUM_ROWS):
     L.append([])
     for j in range(NUM_COLS):
         if i > 1:
             col_ck[0] = L[i - 2][j]
         if i > 0:
             col_ck[1] = L[i - 1][j]
         while True:
             test_value = randint(MIN_INTEGER, MAX_INTEGER)
             if (
                 row_ck.count(test_value) != MAX_REPETITIONS
                 and col_ck.count(test_value) != MAX_REPETITIONS
             ):
                 row_ck[0] = row_ck[1]
                 row_ck[1] = test_value
                 L[i].append(test_value)
                 break

pprint(L)
=======================================================================================

A few rounds of output:

bob at Dream-Machine1:~/Projects/Tutor_Help$ python3 mk_2d_array.py
[[6, 4, 2, 1, 5, 5, 6, 5],
  [3, 3, 1, 3, 5, 5, 6, 5],
  [3, 3, 5, 3, 0, 1, 5, 1],
  [4, 5, 4, 1, 3, 4, 6, 6],
  [1, 3, 1, 4, 1, 6, 4, 5],
  [3, 0, 5, 0, 3, 5, 1, 0],
  [5, 0, 5, 5, 1, 0, 5, 4],
  [2, 5, 1, 3, 1, 2, 2, 1]]
bob at Dream-Machine1:~/Projects/Tutor_Help$ python3 mk_2d_array.py
[[3, 1, 6, 4, 4, 5, 1, 3],
  [5, 2, 3, 4, 0, 3, 3, 1],
  [4, 0, 4, 0, 5, 3, 5, 1],
  [0, 3, 6, 5, 5, 1, 1, 5],
  [0, 2, 3, 2, 2, 6, 6, 4],
  [1, 2, 0, 5, 6, 0, 5, 1],
  [3, 5, 2, 5, 3, 3, 4, 5],
  [5, 1, 2, 4, 0, 1, 2, 2]]
bob at Dream-Machine1:~/Projects/Tutor_Help$ python3 mk_2d_array.py
[[2, 1, 2, 5, 3, 0, 5, 6],
  [1, 6, 3, 0, 0, 4, 4, 6],
  [0, 6, 4, 4, 5, 2, 3, 1],
  [3, 1, 1, 3, 0, 4, 3, 0],
  [5, 4, 3, 0, 3, 3, 1, 5],
  [4, 1, 0, 6, 1, 1, 4, 5],
  [2, 3, 6, 1, 3, 0, 3, 0],
  [4, 1, 6, 0, 1, 6, 6, 1]]

I tried this with up to 30 rows by 30 columns and I did not notice any
perceptible lag on my PC.  The third nested loop -- while loop -- most of
the time should not run through more than one iteration.  I am curious as
to your thoughts.

-- 
Wishing you only the best,

boB Stepp


More information about the Tutor mailing list