Odd list behaviour

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Moritz B
    New Member
    • Aug 2006
    • 6

    Odd list behaviour

    Hi!
    I'm coding a program that's supposed to solve Sudoku, but came accross a really odd behaviour of lists.

    comparelist is a 2D list (9 x 9) filled with integers and unsorted (and I want it to stay that way)

    >for i in range(9):
    >>> bufferlist=comp arelist[i]
    >>> bufferlist=sort er.sortiere(buf ferlist,0,len(b ufferlist)-1) # sorts a list
    >>> if bufferlist==che cklist:
    >>>>> count2=count2+1
    >print comparelist

    now, the bufferlists are all being sorted properly, but the comparelist is now sorted as well !! Why is that so? I really don't understand that behaviour, since I don't say to put the sorted bufferlist back into the comparelist.

    Someone help me please. And thanks in advance :)

    ~Mo
  • kudos
    Recognized Expert New Member
    • Jul 2006
    • 127

    #2
    Hi!
    you buffer points to the list inside your 2d list, what you need to do is to make your own version of the list.

    Something like this perhaps?
    import copy
    bufferlist=copy .copy(compareli st[i])

    -kudos


    Originally posted by Moritz B
    Hi!
    I'm coding a program that's supposed to solve Sudoku, but came accross a really odd behaviour of lists.

    comparelist is a 2D list (9 x 9) filled with integers and unsorted (and I want it to stay that way)

    >for i in range(9):
    >>> bufferlist=comp arelist[i]
    >>> bufferlist=sort er.sortiere(buf ferlist,0,len(b ufferlist)-1) # sorts a list
    >>> if bufferlist==che cklist:
    >>>>> count2=count2+1
    >print comparelist

    now, the bufferlists are all being sorted properly, but the comparelist is now sorted as well !! Why is that so? I really don't understand that behaviour, since I don't say to put the sorted bufferlist back into the comparelist.

    Someone help me please. And thanks in advance :)

    ~Mo

    Comment

    • Moritz B
      New Member
      • Aug 2006
      • 6

      #3
      I'll try that. Thanks alot.

      ~Mo

      Comment

      • Moritz B
        New Member
        • Aug 2006
        • 6

        #4
        Ok it's all working now, thanks again. Just need to do sth about my algorithm now... going through 10 ^ 81 possibilities, of which a bazillion are totally irrelevant, by brute force just isn't quite the right thing :P
        Anyway it works if u put in like de first 76 numbers... u'd only have to wait about a minute then, till de program pops up with an answer... hehe (^ - ^')

        But back to the topic. Wouldn't one expect, just by intuition or whatever, that if u write
        >list1=list2
        both lists would be totally handled individually afterwards? I don't get why there have to be these 'pointers'. Well, I guess it's probably needed for sum advanced programming or more complex coding. But if anyone could dumb it down and explain to me as to why and for what purpose these links between the lists exist I'd be very grateful.

        ~Mo

        Comment

        • kudos
          Recognized Expert New Member
          • Jul 2006
          • 127

          #5
          Hi!
          the trick to make sudoku solving faster, is to perform some heuristic before you do
          the exhaustive search. This page present some 'heuristics' :
          http://www.javaonthebr ain.com/java/sudokuspoiler/technical.html in a nice and
          graphical way.

          I don't really know the 'computer science' reason why the list doesn't get copied
          when you do
          list2 = list1
          but i would guess it might have something to do with saving memory. An example, one of
          the cool features with the C programming language is the following;

          assume I have the list

          unsigned int a[] = {0xff00cc00,0x0 0ffccaa};

          Now, lets assume that I want the first byte in the second entry in the array, then
          I could add the following line;

          unsigned char *b;
          b = (unsigned char *)a;
          printf("%d\n",b[5]);

          Now isn't it a bit irritating, if this automatical should 'steal' 8 bytes of memory, by
          allocating space for b?`

          Another example is the following, lets assume that you have a list, that you want to
          sort:

          list = 5,3,2,6,1,4
          sort(list)

          wouldn't it be kind of irritating, if this command didn't sort 'list', but instead
          returned a new list?

          A last note, in my last post I mentioned the following;
          list = [1,2,3,4,5,6]
          if, you could use copy to do the following;
          list2 = copy.copy(list)
          However, I noticed that you said that you where working with 2d list, maybe
          something like this:

          list1 = [[1,2],[3,4],[5,6]]

          what if we copy this list too?

          list2 = copy.copy(list1 )

          Now try to change

          list2[0][1] = 7

          then list2 would be [[1,7],[3,4],[5,6]], but this would also be content of list1!


          Originally posted by Moritz B
          Ok it's all working now, thanks again. Just need to do sth about my algorithm now... going through 10 ^ 81 possibilities, of which a bazillion are totally irrelevant, by brute force just isn't quite the right thing :P
          Anyway it works if u put in like de first 76 numbers... u'd only have to wait about a minute then, till de program pops up with an answer... hehe (^ - ^')

          But back to the topic. Wouldn't one expect, just by intuition or whatever, that if u write
          >list1=list2
          both lists would be totally handled individually afterwards? I don't get why there have to be these 'pointers'. Well, I guess it's probably needed for sum advanced programming or more complex coding. But if anyone could dumb it down and explain to me as to why and for what purpose these links between the lists exist I'd be very grateful.

          ~Mo

          Comment

          Working...