Last active
August 29, 2015 14:01
-
-
Save lidio601/3473843dac004e8b39de to your computer and use it in GitHub Desktop.
Test to solve the Hanoi Tower http://www.python-course.eu/towers_of_hanoi.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from random import random | |
dadi = 4 | |
# array >>> [1, 2, 3, 4] | |
ncol = 3 | |
sol = [] | |
# esistente | |
def e(col): | |
return col in sol | |
def validv(vect): | |
for i in range(0,len(vect)): | |
if not v(vect[i]): | |
return False | |
return True | |
def v(col): | |
for i in range(0,len(col)): | |
for j in range(i+1,len(col)): | |
if col[j] > col[i]: | |
#print col, j, col[j], col[i] | |
return False | |
return True | |
def pc(col): | |
for i in range(0,len(col)): | |
print col[i] | |
def pr(col,r): | |
#print len(col), r, (len(col) >= r) | |
if r >= len(col): | |
print "|", | |
else: | |
print col[r], | |
def pv(vect): | |
max0 = 3 | |
#for i in range(0,len(vect)): | |
# if max0 < len(vect[i]): | |
# max0 = len(vect[i]) | |
for i in range(0,max0+1): | |
for c in range(0,len(vect)): | |
#print vect[c], (max0-i), len(vect[c]) | |
pr( vect[c], max0-i ) | |
print "" | |
#for c in range(0,max0-1): | |
# print "^", | |
#print "" | |
col = [] | |
for i in range(0,5000): | |
col = [],[],[] | |
array = range(1,dadi+1) | |
while len(array) > 0: | |
dado = int(random()*dadi+1) | |
while not dado in array: | |
dado = int(random()*dadi+1) | |
index = array.index(dado) | |
array.pop(index) | |
pos = int(random()*ncol) | |
col[pos].append(dado) | |
if validv(col) and not e(col): | |
sol.append(col) | |
#pv(col) | |
#print v(col), e(col) | |
#print len(sol) | |
for i in range(0,len(sol)): | |
pv(sol[i]) | |
print "" | |
print len(sol) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment