3

I need create a simple function that computes the cumulative sum (every value in the input tuple should be replaced by the sum of all values before and including the current value, thus (1, 2, 3) becomes (1, 3, 6)) of its arguments, which you can assume are passed by value. Use a variable-length parameter to access these values and return the result as a tuple.

My thought is to use a for loop, but I don't know how to reference the preceding item in the variable-length argument. Below is what I have so far.

def simple_tuple_func(*args):
#  Compute cumulative sum of each value
    print(type(args))
    for idx in args:
        **idx += {itm}**
simple_tuple_func(1,2,3)

The line I have bolded is where I am not sure how to reference the preceding item in the tuple(or list, dictionary, or any other item given as an argument for the function. I believe it would work if I had that line correct?

Thomas
  • 113
  • 3
  • 14
  • 1
    Why don't you use the accumulate() function in the itertools module??? – Mono Sep 26 '18 at 05:23
  • Because I don't know how to use that function. This is for an assignment and we haven't gone over that function yet. That was mentioned below as well. – Thomas Sep 26 '18 at 05:36

2 Answers2

1

Simply use itertools.accumulate:

def simple_tuple_func(*args):
    return tuple(itertools.accumulate(args, lambda x, y: x+y))

Or with a loop:

def simple_tuple_func(*args):
    res = []
    if not args:
      return res
    accum, *tail = args #unpacking, accum will take first element and tail de rest elements in the args tuple
    for e in tail:
        res.append(accum)
        accum += e
    res.append(accum)
    return tuple(res)

Here you have the live example

Netwave
  • 40,134
  • 6
  • 50
  • 93
  • I tried it with the loop since the use of the lambda function is in the next section to learn. It returned a list with only [1,3] though. It needs to return a tuple and it should be (1,3,6). – Thomas Sep 26 '18 at 05:32
  • I also don't fully understand what is being done with accum, *tail = args. I assume you are establishing what the args are? – Thomas Sep 26 '18 at 05:33
  • 1
    The lambda is unnecessary; `itertools.accumulate` defaults to performing a cumulative sum. – user2357112 Sep 26 '18 at 05:34
  • 1
    @Tom, `accum, *tail = args` just unpacks the elements, first version were wrong, you can check the update i did. – Netwave Sep 26 '18 at 05:48
  • @user2357112, was forcing the lambda to illustrate what accumulate use as arguments – Netwave Sep 26 '18 at 05:49
  • Ok, that makes sense now. I understand this much better now. Thanks for your help! – Thomas Sep 26 '18 at 05:51
1

You can append the cumulative sums to a separate list for output, so that you can use index -1 to access to preceding cumulative sum:

def simple_tuple_func(*args):
    cumulative_sums = []
    for i in args:
        cumulative_sums.append((cumulative_sums[-1] if cumulative_sums else 0) + i)
    return tuple(cumulative_sums)

so that:

simple_tuple_func(1,2,3)

would return:

(1, 3, 6)
blhsing
  • 91,368
  • 6
  • 71
  • 106