Sunday, December 18, 2011

Shifting decipher (probability based)

A little experiment using probability to find out the shifting applied to encrypt a message.


dicctionary = ['the', "and","by","to", "in", "out"]
message = "Esp qtcde nzyqpcpynp zy esp ezatn zq Lcetqtntlw\
Tyepwwtrpynp hld spwo le Olcexzfes Nzwwprp ty estd jplc."

separation_characters =[' ','.']

score = []

m = [x.lower() for x in message]
m = "".join(m)
for i in range(27):
sh = []
for x in m:
if x in separation_characters:
sh.append(x)
else:
sh.append(chr(((ord(x)+i-97)%26 + 97)))
sh = "".join(sh)
words = sh.split(" ")
print words
found = 0
for w in words:
if w in dicctionary:
found = found + 1
print found/len(words)
score.append(found/float(len(words)))
print score
maxval = 0
maxindex = 0
for i in range(len(score)):
if score[i] > maxval:
maxval = score[i]
maxindex = i
print "SHIFT%d"%(maxindex)
sh = []
for x in m:
if x in separation_characters:
sh.append(x)
else:
sh.append(chr(((ord(x)+maxindex-97)%26 + 97)))
print "".join(sh)

Hanoi Towers solver (python)

This is not the most elegant of efficient piece of code, but it was quite fun to write (http://www.ideone.com/g6Zhb):


def print_game(board):
for l in board:
print l
print "----"


def initial_state(board,count):
board[0] = range(count,0,-1)
board[1] = []
board[2] = []

def goal_state(board,count):
board[0] = []
board[1] = []
board[2] = range(count,0,-1)


def children(board):
children = list()
if board[0] != []:
val = board[0][-1]
if len(board[1]) == 0 or board[1][-1] > val:
#print_game(board)
child = list([[],[],[]])
child[0] = board[0][0:-1]
child[1] = board[1][:]
child[1].append(val)
child[2] = board[2][:]
children.append(child)
#print_game(child)
if len(board[2]) == 0 or board[2][-1] > val:
#print_game(board)
child = list([[],[],[]])
child[0] = board[0][0:-1]
child[1] = board[1][:]
child[2] = board[2][:]
child[2].append(val)
children.append(child)
#print_game(child)
if board[1] != []:
val = board[1][-1]
if len(board[0]) == 0 or board[0][-1] > val:
#print_game(board)
child = list([[],[],[]])
child[0] = board[0][:]
child[0].append(val)
child[1] = board[1][0:-1]
child[2] = board[2][:]
children.append(child)
#print_game(child)
if len(board[2]) == 0 or board[2][-1] > val:
#print_game(board)
child = list([[],[],[]])
child[0] = board[0][:]
child[1] = board[1][0:-1]
child[2] = board[2][:]
child[2].append(val)
children.append(child)
#print_game(child)
if board[2] != []:
val = board[2][-1]
if len(board[0]) == 0 or board[0][-1] > val:
#print_game(board)
child = list([[],[],[]])
child[0] = board[0][:]
child[0].append(val)
child[1] = board[1][:]
child[2] = board[2][0:-1]
children.append(child)
#print_game(child)
if len(board[1]) == 0 or board[1][-1] > val:
#print_game(board)
child = list([[],[],[]])
child[0] = board[0][:]
child[1] = board[1][:]
child[1].append(val)
child[2] = board[2][0:-1]
children.append(child)
#print_game(child)
return children

def isInExplored(explored,state):
for s in explored:
if s == state:
return True
return False

def main():
board = list([[],[],[]])
goal = list([[],[],[]])
initial_state(board,7)
goal_state(goal,7)
frontier = []
level = []
currentLevel = 0
explored = []
path = []
parent = [0]
frontier.append(board)
level.append(currentLevel)
while len(frontier) > 0:
state = frontier.pop(0)
currentLevel = level.pop(0)
father = parent.pop(0)
#print currentLevel
#print_game(state)
if state == goal:
print "GOAL FOUND:"
print currentLevel
print_game(state)
for i in range(currentLevel):
print_game(explored[father])
father = path[father]

explored.append(state)
path.append(father)
for child in children(state):
if isInExplored(explored,child) == False and isInExplored(frontier,child) == False:
frontier.append(child)
level.append(currentLevel + 1)
parent.append(len(explored)-1)
print len(explored)


main()

Saturday, December 03, 2011

Mars rover Curiosity landing tests

My last post included a video with a simulation of the Curiosity rover landing in Mars. Well, you may guess that a multi million dollar machine is not sent to Mars before a little bit of real testing. So, wait no more, here you a video with actual testing footage:



Enjoy!!