[Tutor] Recursive list checking

joe_schmoe geek_show at dsl.pipex.com
Sat Apr 9 00:52:05 CEST 2005


Rick Muller wrote:
> 
> On Apr 8, 2005, at 11:37 AM, tutor-request at python.org wrote:
> 
>> From: joe_schmoe <geek_show at dsl.pipex.com>
>> For example, this is what I am currently doing:
>>
>> =============code block ========================
>>
>>     # generate unique numbers and append to list
>>     nmbr01 = random.randrange( 1, 20 )
>>     nmbr_list.append( nmbr01 )
>>
>>     nmbr02 = random.randrange( 1, 20 )
>>     # check for duplicates and re-generate a number if needed
>>     while nmbr02 in nmbr_list:
>>         nmbr02 = random.randrange( 1, 20 )
>>     nmbr_list.append( nmbr02 )
>>
>>     nmbr03 = random.randrange( 1, 20 )
>>     while nmbr03 in nmbr_list:
>>         nmbr03 = random.randrange( 1, 20 )
>>     nmbr.append( nmbr03 )
>>
>> ================================================
>>
> 
> Since you want unique entries, couldn't you just do something like
> 
> def unique_entries(n,start=1,stop=20):
>     "Generate n unique entries in range(1,20)"
>     from random import shuffle
>     l = range(start,stop)
>     shuffle(l)
>     return l[:n]
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
Rick, Kent, Bernard, and Jeff

Thanks for your comments: that is so cool - ask a question and 5 
different ways to do the same thing!! Thanks guys :)

As it so happens, I settled for Jeff's suggestion for 2 reasons:
1. because it was the first response and it worked when tested
2. it is straight forward enough for me to mentally follow what's happening

I thought some of the suggestions were really cool, but admittedly a 
little over my head at this point in time.

The solution was worked into a small "Master-Mind" like program I was 
fooling around with. I don't know what the protocol is on this list, but 
if you want to see it I'm happy to post here or off-list.

Anyway, just to give feedback on your helpful comments. Thanks.

/j


More information about the Tutor mailing list