Skip to content

Instantly share code, notes, and snippets.

@antonio-catalano
Created April 8, 2018 00:27
Show Gist options
  • Save antonio-catalano/270095579aa43722c7135591ad66a787 to your computer and use it in GitHub Desktop.
Save antonio-catalano/270095579aa43722c7135591ad66a787 to your computer and use it in GitHub Desktop.
""" Given the first 10 numbers, we want to know how many snake permutations exist, where a snake permutations is:
x1 < x2 > x3 < x4 > x5 < x6 > x7 < x8 > x9 < x10
The result is very counterintuitive....more than 50000 snake permutation exist.
It takes a lot of time to have the output, so take only the idea of the code..."""
import random
union = []
for i in range (25000000):
A = [s for s in range (1,11)]
new = []
for i in range(10):
k = random.choice(A)
new.append(k)
A.remove(k)
count = 0
for i in range(0,8,2):
if new[i] < new[i+1] and new[i+1] > new[i+2] and new[8] < new[9]: # the algorithm to search the snake permutaton
count += 1
if count == 4 and new not in union:
union.append(new)
print(union)
print()
print("For n = 10 there are: {} snakes permutations".format(len(union)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment