dimanche 5 juillet 2020

Having trouble locating logically error, for loop with recursion

My goal is to print numbers from 1 to 9 with no repeats using the logic: for each number in the array to be printed,

  • check all numbers in that row of the array if there is a repeated number

  • else re-roll the random number perform these tasks recursively until whole row is filled from 1-9 with no repeated number

      p = np.zeros((1,9), dtype=int)
    
      def check_row(i):
        num = random.randint(1,9)
        # check num equal any number in its row
        for nine in range(9):
          if num == p[i][nine]:
              check_row(i)  
          else:
              pass    
        return num
    
     for j in range(9):
       if p[0][j] == 0:
         p[0][j] = check_row(0)
    
     print(p)
    

Aucun commentaire:

Enregistrer un commentaire