[Tutor] Bit Strings [a recursive approach]

Gregor Lingl glingl at aon.at
Thu Oct 23 17:13:12 EDT 2003


....

>>nevertheless very compact:
>>
>> >>> def bitstrings(n):
>>        if n==0: return ["0","1"]
>>        return [ digit+bitstring for digit in bitstrings(1)
>>                                 for bitstring in bitstrings(n-1)]
>>    
>>
>
>Hi Gregor,
>
>Correction: the basis here needs to be repaired for n == 1;  otherwise,
>this won't terminate.  With a recursive approach, we have to especially
>careful about the boundary cases of our problem.
>
>
>One way to fix the bug might be:
>
>###
>def bitstrings(n):
>    if n == 0: return []
>    if n == 1: return ["0","1"]
>    return [ digit+bitstring for digit in bitstrings(1)
>                             for bitstring in bitstrings(n-1) ]
>###
>  
>
Hi Danny!
Back from a marvelous theatre performance I discovered, that I once more 
caused
some confusion by posting an incorrect statement.  That  again  came 
because I was in
a hurry. I tried out

 >>> def bitstrings(n):
        if n == 1: return ["0","1"]
        return [digit+bitstring for digit in bitstrings(1)
                                for bitstring in bitstrings(n-1)]

and this still satisfies me, although it doesn't work for n = 0. I 
transferred it into my
posting and a moment before pressing the send-button I thougt towards a 
solution
like Blakes, (which I really like; also his arguments concerning [""]).
Began to change my first idea, saw that I had no time left to try it out 
before the theatre
and quickly decided to go the "secure" way, my first idea. Changed it back,
except this bad zero:  :-(   Then, in a sudden emotion of impatience I 
pressed the send button
and I know I do this all to often.

What do I have to learn from this? To be more careful, and not to post  
if  in a hurry.

Anyway, thanks for your correction. I'm really glad that there are so 
many  people
on this list which can be considered as critical friends. (Hope this 
expression makes
sense in English).
 
Gregor

>
>Good luck!
>
>
>
>  
>




More information about the Tutor mailing list