# A simple hangman using superposition # OH - aug 2018 """ $ python guess2word.py Guess my secret word? A no match Guess my secret word? H a partial match of my secret; score: 7 Guess my secret word? HO a partial match of my secret; score: 10 Guess my secret word? O a partial match of my secret; score: 7 Guess my secret word? HOI a partial match of my secret; score: 11 Guess my secret word? HOME you found my secret in 6 tries """ import itertools,os,re,collections word = "HOME" def spectrum(str): return [ord(x) for x in list(str)] def INTERMODULATION(freq): # Snap intermodulation and compare with previous SNAP snap = [0] m = re.search( "^([A-Za-z]+)2([A-Za-z]+)\.",os.path.basename(__file__)) if m: # variables for intermodulation in the scriptname list1 = eval("spectrum(%s)" % (m.groups()[1]) ) list2 = eval("spectrum(%s)" % (m.groups()[0]) ) # 2nd order combinations mix=list( itertools.product(list1+list2, repeat=2) ) # sum and differences of 2nd order combinations mixd = [i[0]-i[1] for i in mix] mixs = [i[0]+i[1] for i in mix] snap = collections.Counter(list1+list2+mixs+mixd) return snap[freq] for i in range(1,10): guess = raw_input("Guess my secret word? ").upper() j=INTERMODULATION(0) if j==len(word)**2: print " you found my secret in " + str(i) + " tries" i=99 break if j>(len(word)+len(guess)): print( " a partial match of my secret; score: " + str(j)) else print " sorry, no match" if i==9: print "\nSorry, not found after too many attemps.\nThe secret word was '"+word+"'"