rt3017
rt3017

Reputation: 33

Parallel processing of multiple sequences

I'm having trouble solving this problem:

Parallel processing of multiple sequences is a powerful functionality for data analysis.

Define a function mySumParallel(seqs) that applies mySum() defined in before to an arbitrary collection of sequences of numeric values in parallel. This function returns a list of sums of all sequences in the passed-in collection.

For example, calling mySumParallel(collection) where collection = [[1, 12, 89, 5], range(100), range(2, 9, 3)] returns [107, 4950, 15].

As for mySum(), I've already made it as such:

def mySum(*elems):
    if not elems: return 0
    sumOfElems = 0
    for elem in elems:
        sumOfElems += elem
    return sumOfElems

I tried solving this problem using these codes below:

collection = [[1, 12, 89, 5], range(100), range(2, 9, 3)]

def mySumParallel(*seqs):
    return list(map(mySum,seqs))

mySumParallel(collection)

But it shows an error like this:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-108-2f21ae17a1db> in <module>
      7     return list(map(mySum,seqs))
      8 
----> 9 mySumParallel(collection)

<ipython-input-108-2f21ae17a1db> in mySumParallel(*seqs)
      5 
      6 def mySumParallel(*seqs):
----> 7     return list(map(mySum,seqs))
      8 
      9 mySumParallel(collection)

<ipython-input-105-573e7bfa9b5f> in mySum(*elems)
      4     sumOfElems = 0
      5     for elem in elems:
----> 6         sumOfElems += elem
      7     return sumOfElems
      8 

TypeError: unsupported operand type(s) for +=: 'int' and 'list'

while the expected output when calling mySumParallel(collection) where collection = [[1, 12, 89, 5], range(100), range(2, 9, 3)] will return [107, 4950, 15].

Any help would be much appreciated.

Upvotes: 0

Views: 342

Answers (1)

akazuko
akazuko

Reputation: 1394

Few things to note:

  • you should return / print list(map(mySum, seqs)) in the function mySumParallel
  • elems is going to be a list and the first element of the list is going to be the sequence you want to sum over

EDIT: editing code below to accommodate the new restriction added by OP that mySum can not be changed.

Working code:

def mySum(*elems):
    if not elems: return 0
    sumOfElems = 0
    for elem in elems:
        sumOfElems += elem
    return sumOfElems

def mySumParallel(seqs):
    return [mySum(*x) for x in seqs]

collection = [[1, 12, 89, 5], range(100), range(2, 9, 3)]
print(mySumParallel(collection))

# prints [107, 4950, 15]

Upvotes: 2

Related Questions