[Tutor] combinations

Andreas Zwinkau andi@buxach.de
Sat May 10 06:44:02 2003


This is a multi-part message in MIME format.

--Multipart_Sat__10_May_2003_12:42:42_+0200_08267408
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit

I wrote a program to print out all kominbations of a given string
123:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

I had to write a function to check wether a number is in the current
possibility yet
def partof(needle, heystack):
        """ is needle in heystack? """
        for x in heystack:
                if needle == x:
                        return 1
        return 0

First i tried to do it this way
	if neddle in heystack:
1. This would be short but is not possible, is it??

Another problem was the recursion
        for current in range(len(full)): # try all combinations
                if not partof(current, yet):
                        yet.append(current) # add ne possible element
                        yet = kombies(yet, full)[0:-1]
        return yet
First i tried this 
                        yet.append(current)
                        kombies(yet, full) #kombies returns nothing
			yet = yet[0:-1] #kick the last element to try another
But this did not work.
2. Python never got back to the first stage of the recursion. why?

attached is the full program as it works
-- 
Andreas Zwinkau
 | web: andi.dasstellenwirinsinternet.de
 | mail: andi@buxach.de
 | jabber: beza1e1@amessage.de

--Multipart_Sat__10_May_2003_12:42:42_+0200_08267408
Content-Type: application/octet-stream;
 name="kombis.py"
Content-Disposition: attachment;
 filename="kombis.py"
Content-Transfer-Encoding: base64

IyEvdXNyL2Jpbi9weXRob24KaW1wb3J0IHN5cwoKZGVmIHBhcnRvZihuZWVkbGUsIGhleXN0YWNr
KToKCSIiIiBpcyBuZWVkbGUgaW4gaGV5c3RhY2s/ICIiIgoJZm9yIHggaW4gaGV5c3RhY2s6CgkJ
aWYgbmVlZGxlID09IHg6CgkJCXJldHVybiAxCglyZXR1cm4gMAoKZGVmIHBvc19wcmludCggcG9z
aXRpb25zLCBhbGwgKToKCSIiIiBwcmludCB0aGUgbGV0dGVycyBvZiBhbGwgYnkgdGhlIHBvc2l0
aW9ucyAiIiIKCWZvciBwb3MgaW4gcG9zaXRpb25zOgoJCXByaW50IGFsbFtwb3NdLAoJcHJpbnQK
CmRlZiBrb21iaWVzKHlldCwgZnVsbCk6CgkiIiIgZmluZCBhbGwga29tYmluYXRpb25zIHJlY3Vy
c2l2ZSAiIiIKCWlmIGxlbih5ZXQpID09IGxlbihmdWxsKTogI3dlIGFyZSBmdWxsLCBwcmludCBp
dCBvdXQKCQlwb3NfcHJpbnQoIHlldCwgZnVsbCApCgkJcmV0dXJuIHlldAoJCglmb3IgY3VycmVu
dCBpbiByYW5nZShsZW4oZnVsbCkpOiAjIHRyeSBhbGwgY29tYmluYXRpb25zCgkJaWYgbm90IHBh
cnRvZihjdXJyZW50LCB5ZXQpOgoJCQl5ZXQuYXBwZW5kKGN1cnJlbnQpCgkJCXlldCA9IGtvbWJp
ZXMoeWV0LCBmdWxsKVswOi0xXSAKCXJldHVybiB5ZXQKCmlmIF9fbmFtZV9fID09ICJfX21haW5f
XyI6Cglrb21iaWVzKFtdLHN5cy5hcmd2WzFdKQoK

--Multipart_Sat__10_May_2003_12:42:42_+0200_08267408--