-7

I made a program for finding all combinations for numbers from 0 to 3 and number of possible lists is 24 (4! = 24)

import random

number_for_list = 0
number_of_combinations = 0
all_lists = []
list_to_check = []




def a_function():


  number_for_list = random.randrange(4)    #generate a random number

  if len(list_to_check) == 4:      #check if 4 different numbers are in the list

    if list_to_check in all_lists:  #if that list (with 4 numbers) already exists in all_lists remove it and go back to a_function
      del list_to_check[:]
      a_function()

    else:                             #if that list isnt in all_lists
      all_lists.append(list_to_check)   #add it to the all_lists

      number_of_combinations += 1    #raise number_of_combinations 
      del list_to_check[:]        #delete the list_to_check and go back to a_function
      a_function()

  elif number_for_list not in list_to_check:  #if number_for_list isn't in the list_to_check

    list_to_check.append(number_for_list)     #add it to the list_to_check and go back to a function
    a_function()

  elif number_for_list == 24:                  #if number_of_combinations equals 24 then it should print all lists
    print('I found all the combinations')     #and stop the function
    print(all_lists)
    return



a_function()

For some reason nothing happens when I run the program and I can't find any logical errors, what's wrong then?

user3711671
  • 823
  • 1
  • 6
  • 13
  • Wild guess: your `if` clause isn't passing and neither are your two `elif` clauses, so the function simply ends quietly. – Kevin Mar 18 '15 at 18:42
  • None of the if conditions evaluate to true. – Antti Haapala -- Слава Україні Mar 18 '15 at 18:43
  • *I can't find any logical errors*. Look again. You start with an empty list and `number_for_list` is going to be `0`, `1`, `2`, or `3`. How are any of your conditions going to match in that case? What if one of those numbers *is* in the list? – Martijn Pieters Mar 18 '15 at 18:44
  • I don't really understand, what isn't it passing and what do I need to change? @Martijin Yes I create an empty list because it needs to be declared first and number_for_list can only be 0, 1, 2, 3 (generating a number generates numbers from zero to x - 1 (in my case instead going from 1 to 4 it will go from 0 to 3). – user3711671 Mar 18 '15 at 18:44
  • On your `elif number_for_list == 24:` line, you write the comment "if number_of_combinations equals 24 then it should print all lists". But you're not checking "number of combinations". You're checking "number for list". – Kevin Mar 18 '15 at 18:46
  • @user3711671: but what if you added `3`, then generated the random number `3` again? – Martijn Pieters Mar 18 '15 at 18:47
  • Looks like I made a mistake, but it still doesn't work, I get this error:local variable 'number_of_combinations' referenced before assignment as far as i can see it's referenced outside the function. – user3711671 Mar 18 '15 at 18:48
  • @user3711671: if you want to use a global for `number_of_combinations` *and* assign to it (`+= 1` is assingment) then you need to use the `global` keyword. – Martijn Pieters Mar 18 '15 at 18:53
  • 1
    Incidentally, consider discarding all of this code and just doing `import itertools; print list(itertools.permutations(range(4)))` – Kevin Mar 18 '15 at 18:54
  • Why would I need to use global when everything is declared before the function.I know that I can do this with only a line or two but I want to know what's wrong with this longer program. – user3711671 Mar 18 '15 at 18:57

1 Answers1

0

Working code:

import random

number_of_combinations = 0
all_lists = []
list_to_check = []




def a_function():
  global number_of_combinations
  global list_to_check
  global all_lists

  number_for_list = random.randrange(4)    #generate a random number

  if len(list_to_check) == 4:      #check if 4 different numbers are in the list

    if list_to_check in all_lists:  #if that list (with 4 numbers) already exists in all_lists remove it and go back to a_function
      del list_to_check[:]
      a_function()

    else:                             #if that list isnt in all_lists

      all_lists.append(list_to_check[:])   #add it to the all_lists

      number_of_combinations += 1    #raise number_of_combinations
      del list_to_check[:]        #delete the list_to_check and go back to a_function
      a_function()

  elif number_of_combinations == 24:                  #if number_of_combinations equals 24 then it should print all lists
    print('I found all the combinations')     #and stop the function
    print(all_lists)
    return
  elif number_for_list not in list_to_check:  #if number_for_list isn't in the list_to_check

    list_to_check.append(number_for_list)     #add it to the list_to_check and go back to a function
    a_function()
  else:
    a_function()

a_function()

Edited: all_lists.append(list_to_check[:]) #add it to the all_list (you need to add a copy of list if you destroy it's content afterwards. elif number_for_list not in list_to_check: a_function() else: a_function() If number is in the list (else branch) you need to generate new one.

jendas
  • 138
  • 1
  • 10