#! /usr/bin/python ## Implements the Decoder for the Fibonacci code. David MacKay Dec 2005 ## Reads binary fibo-strings from stdin and writes integers to stdout import sys, string from Numeric import * verbose=0 class node: def __init__(self, index, predecessor=1): self.index = index self.predecessor = predecessor def child(self,type) : if(type==0): n = self.index + self.predecessor predecessor = self.index else: n = 2 * self.index + self.predecessor + 1 predecessor = self.index + self.predecessor+ 1 return node(n,predecessor) ## Make first node one = node( 1 ) for line in sys.stdin.readlines(): if line[0] != '#' : words = string.split(line) for w in words : ## w is a string of 0s and 1s ch = one for s in w : ## s is a single 0 or 1 ch = ch.child(int(s)) print ch.index ## when the 0s and 1s end, we print the state