Grasshopper

algorithmic modeling for Rhino

I have a long list of numbers....

say a series from 1 to 100. 

I want to take the first number in the list, and add the second number,

then add that number to a list to store, 

I want to take that result and add the third number, then store the result,

then I want to take the result and add the third number... etc until the list is all done... 

I think I need to make a for loop with a function that adds 1 to the index of the selected number to use in an addition function and appends a list... and does this until the index is = to the length of the list... but darn it if my pythons gotten a little rusty... any help PLEASE super appreciated, thanks e

Views: 995

Replies to This Discussion

I think the native gh component "Mass Addition" will do what you want. It also has "Partial Results" output that will give you the separate steps of adding each new value.
Or you insist on it being in python?

well i would never insist on python if I can do it with GH !

I never saw the partial results option in mass add before good call!

...I would like to know how to do it in python still just out of curiosity and in a general effort to start to sharpen my looping skills again... BUT this def works for now! THANKS !!!!

Here it is in python! Just for reference.

pResults = []

def sumation(cSum,iter):
    if iter<len(x):
        r = cSum + x[iter]
        pResults.append(r)
        sumation(r,iter + 1)
    else: return None


sumation(0,0)
a = pResults[len(x)-1]
b = pResults

Attachments:

Could also do it with a simple for loop.

Attachments:

Actually, this problem is a good example of why recursion in Python can be problematic. Try inputting a large list (see attached def). At some point this will likely crash Rhino (on my system around 2000 items). I believe this has to do with tail recursion, which can be avoided by either using memoization or by implementing the algorithm using an iterative for/while loop instead. Hope that helps..

Attachments:

i will check this out thank u!!!

I don't know what I was thinking last night, that tail recursion was a massive brain-fart...

thanks d ! super helpful

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service