Wednesday, March 1, 2017

Damn you, permutations

I had such a hard time figuring out how to write a python function to generate permutations two weeks ago.

Then yesterday, I tried doing it again using a different method and I got stuck for half an hour. It finally worked when I used list.extend instead of list.append.

And then when I looked at the code I wrote two weeks ago, they were the exact same. Recursion within a for-loop. Damn. This site very helpfully explained the thinking: http://typeocaml.com/2015/05/05/permutation/


def permutate(input):
if len(input)==0:
return [[]]
output=[]
for index,num in enumerate(input):
remain=input[:index]+input[index+1:]
to_add=[[num]+ result for result in permutate(remain)]
output.extend(to_add)
print(output)
return output

print(permutate([1,2,3]))

1 comment:

  1. As we have came upon, blackjack has significantly developed. Based on the information given above, in a position to} say that it’s already not the King of desk video games. Still, it doesn’t make it much less engaging and well-liked when comparability with} different casino video games. And we believe that ultimately blackjack will see a boom again, as it nonetheless has plenty of potential level out|to indicate} sooner or later. You might need already heard about Virtual Reality and its implementation in casino 코인카지노 video games.

    ReplyDelete