Help with pure functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Xentrik
    New Member
    • Mar 2010
    • 8

    Help with pure functions

    I am a beginner working my way through How To Think Like A Computer Scientist. I am having a problem with an exercise that multiplies a matrix by a scalar. def scalar_mult(n, m), n being the scalar and m being the matrix.
    My code works but I guess its not a pure function because if I put in

    >>> m = [[1, 2], [3, 4]]
    >>> scalar_mult(3, m)

    my return is [[3, 6], [9, 12]] which is correct, but when i check m after I run my function I get [[3, 6], [9, 12]] instead of [[1, 2], [3, 4]]. I don't understand this because I cloned m and used mx throughout the function. This is the first time I have posted on a forum so please excuse me if my format is incorrect.

    [code=python]
    def scalar_mult(n, m):
    mx = m[:]
    (my code is here)
    [/code]
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    Hi

    Welcome to the forum. Please go ahead and post the rest of your code.

    Thanks

    Glenton

    Comment

    • Xentrik
      New Member
      • Mar 2010
      • 8

      #3
      Scalar code

      Originally posted by Glenton
      Hi

      Welcome to the forum. Please go ahead and post the rest of your code.

      Thanks

      Glenton
      Glenton, here is my code.

      [code=python]
      def scalar_mult(n, m):
      mx = m[:]
      x = 0
      while x < len(mx):
      for index, value in enumerate(mx[x]):
      mx[x][index] = mx[x][index] * n
      x += 1
      return mx
      [/code]

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        The problem is that you have a list of lists. You made a copy of the original list, but you did not make copies of the internal lists. Do this:
        Code:
        def scalar_mult(n, m):
            mx = [item[:] for item in m]
            x = 0
            while x < len(mx):
                for index, value in enumerate(mx[x]):
                    mx[x][index] = mx[x][index] * n
                x += 1
            return mx
        Following is an example that makes a new list. It uses recursion to get at nested lists also.
        Code:
        def scalar_mult(seq, n):
            results = []
            for elem in seq:
                if isinstance(elem, (list, tuple)):
                    results.append(scalar_mult(elem, n))
                else:
                    results.append(elem * n)
            return results
        
        seq = [[1, 2], [3, 4]]
        seq1 = scalar_mult(seq, 3)
        print seq
        print seq1
        
        seq2 = ((1, 2), (3, (4, 5, 6), ((6, 7, 8), (9, 20))))
        seq3 = scalar_mult(seq2, 3)
        print seq2
        print seq3
        
        ''' The output:
        >>> [[1, 2], [3, 4]]
        [[3, 6], [9, 12]]
        ((1, 2), (3, (4, 5, 6), ((6, 7, 8), (9, 20))))
        [[3, 6], [9, [12, 15, 18], [[18, 21, 24], [27, 60]]]]
        '''

        Comment

        Working...