#!/usr/bin/env python # Copyright 2007 Ken Friis Larsen codes = [('A',".-"), ('B',"-..."), ('C',"-.-."), ('D',"-.."), ('E',"."), ('F',"..-."), ('G',"--."), ('H',"...."), ('I',".."), ('J',".---"), ('K',"-.-"), ('L',".-.."), ('M',"--"), ('N',"-."), ('O',"---"), ('P',".--."), ('Q',"--.-"), ('R',".-."), ('S',"..."), ('T',"-"), ('U',"..-"), ('V',"...-"), ('W',".--"), ('X',"-..-"),('Y',"-.--"), ('Z',"--..")] def decode(input): if input == "" : return [input] else: return [ c + rest for c, code in codes if input.startswith(code) for rest in decode(input[len(code):]) ] def test( s, code): if s in decode(code): print code + " can be decoded as " + s else: print code + " can NOT be decoded as " + s import time start = time.clock() res = len(decode("...---..-....-...----")) print "%f" % (time.clock()-start) #test("SOFIASOT", "...---..-....-...---..-.") #test("SOPHIA", "...---..-....-") #test("EUGENIA", "...---..-....-")