how to implement "fold" function in python?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kc2ine
    New Member
    • Feb 2010
    • 5

    how to implement "fold" function in python?

    Hi,
    I need to write a python function to fold the grid in one of these
    four ways, depending on the argument.

    L -> Left to Right
    R -> Right to Left
    T -> Top to Bottom
    B -> Bottom to Top

    When folding, half the elements will be laid on top of
    the others.
    After folding until only 1 element remains, I want to print the list
    of elements from top to bottom.

    Here's a 2x2 example.

    Input:
    1 2
    3 4

    fold("R").fold( "B") -> 3, 4, 2, 1
    fold("T").fold( "L") -> 3, 1, 2, 4

    Is there any "easy" way to do this using reduce function?
    thanks
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Could you explain what you are trying to do? I have no idea what "folding" is, and I don't see the relationship between your example and the output.

    Comment

    • Glenton
      Recognized Expert Contributor
      • Nov 2008
      • 391

      #3
      Tricky.

      You might consider writing a class structure to hold the whole thing in.

      But in brief, you want to be thinking 3D. Or a 2D list of lists if that makes sense. Then you can add the lists together in an appropriate order as a fold operation. (I'm assuming that the rows and columns will have to be powers of 2?)

      Here's a bit of playing around to get your started. I start with grid a (except that I make it a grid of lists to make it generalisable), and then b does a R fold.
      Code:
      In [64]: a=[[[1],[2]],[[3],[4]]]
      
      In [65]: b=[]
      
      In [66]: for row in range(len(a)):
         ....:     b.append([])
         ....:     for col in range(len(a[row])/2):
         ....:         b[row].append([])
         ....:         b[row][col]+=a[row][col]
         ....:         temp=a[row][len(a[row])-col-1]
         ....:         temp.reverse()
         ....:         b[row][col]+=temp
      
      In [67]: b
      Out[67]: [[[1, 2]], [[3, 4]]]
      (Of course in this case the reverse is unnecessary, but it makes the code a bit more general)

      Next we want to fold up:
      Code:
      In [75]: c=[]
      
      In [76]: for row in range(len(b)/2):
         ....:     c.append([])
         ....:     for col in range(len(b[row])):
         ....:         c[row].append([])
         ....:         c[row][col]+=b[row][col]
         ....:         temp=b[len(b)-row-1][col]
         ....:         temp.reverse()
         ....:         c[row][col]+=temp
         ....:         
         ....:         
      
      In [77]: c
      Out[77]: [[[1, 2, 4, 3]]]
      Okay, I hope that gets you started.

      Comment

      • kc2ine
        New Member
        • Feb 2010
        • 5

        #4
        Thanks Glenton,
        Yes, rows and columns will have to be powers of 2.
        I was hoping there was some ready made function in python to do this like reduce because I've seen in wiki fold funcion in python is just a reduce but maybe not exactly.
        here: http://en.wikipedia.org/wiki/Fold_(h...order_function)

        Anyway that got me started, thanks a lot for help.

        Comment

        • kc2ine
          New Member
          • Feb 2010
          • 5

          #5
          Bvdet,
          imagine piece of paper with numbers on it like matrix.
          When you fold this paper e.g. from left to right then from bottom to top
          until nothing else is left to fold you'll get the output. Number from top to bottom.

          Comment

          Working...