Why use recursion when you can use dynamic programming, save memory and 
function calls. ;-)

On Friday, 31 May 2013 13:03:26 UTC-5, Derek wrote:
>
> not a bad try, i think it may be more beautiful with recursion. i'll give 
> a stab at it. If Alan reads this, I suspect he can spit something out fast.
>
> On Friday, May 31, 2013 9:42:04 AM UTC-7, Massimo Di Pierro wrote:
>>
>> data = """0    'a'                                                       
>>         
>> 1    'b'                                                                 
>>         
>> 2    'c'                                                                 
>>         
>> 1    'd'                                                                 
>>         
>> 2    'e'                                                                 
>>         
>> 2    'f'                                                                 
>>         
>> 3    'g'                                                                 
>>         
>> 1    'h'"""
>>
>> items = [(int(x.split()[0]),x.split()[1][1:-1]) for x in data.split('\n')]
>>
>> def blobbify(items):
>>     parents = []
>>     for x,y in items:
>>         item = {'blob':y}
>>         if x<len(parents):
>>             del parents[x:]
>>         if x>len(parents):
>>             raise RuntimeError('Invalid Input')
>>         parents.append(item)
>>         if parents and x>0:
>>             parents[x-1]['under'] = parents[x-1].get('under',[])+[item]
>>     return parents[0]
>>
>>
>> print blobbify(items)
>>
>> On Thursday, 30 May 2013 15:58:20 UTC-5, Niphlod wrote:
>>>
>>> Hi @all.
>>> Given this data (ordered list of tuples):
>>> 0    'a'
>>> 1    'b'
>>> 2    'c'
>>> 1    'd'
>>> 2    'e'
>>> 2    'f'
>>> 3    'g'
>>> 1    'h'
>>>
>>>
>>> the first value is "how many level should I go down", the second is the 
>>> actual data. 
>>> Every row's first value has a following first value that is:
>>> - equal to previous
>>> - previous + 1
>>> - less than previous (up to 0)
>>>
>>> My spider senses kinda suggest me that it's enough to make a nested dict 
>>> like 
>>>
>>> Surely it's easy to print out something like
>>> a
>>> -b
>>> --c
>>> -d
>>> --e
>>> --f
>>> ---g
>>> -h
>>> but I can't force my powers to produce a nice python code out of this.
>>> If you don't like nested dicts, a nested tuple, or a nested list are 
>>> fine too. They just need to be easily iterable, top-down, e.g.
>>> {
>>> 'blob' : 'a',
>>> 'under' : [
>>>     {'blob' : 'b',
>>>      'under' : [
>>>         {'blob' : 'c'}
>>>         ]
>>>     },
>>>     {'blob' : 'd',
>>>      'under' : [
>>>         {'blob' : 'e'},
>>>         {'blob' : 'f',
>>>          'under' : [
>>>             {'blob' : 'g' }
>>>             ]
>>>         }
>>>         ]
>>>     },
>>>     {'blob' : 'f' }
>>> ]
>>> }
>>>
>>>
>>> Someone cares to enjoy a little bit ? ^_^ 
>>>
>>> Feel free to ask any questions.
>>>
>>>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to