@@ -0,0 +1,1612 @@
+# fivethirtyeight.com Riddler https://fivethirtyeight.com/features/can-you-outsmart-our-elementary-school-math-problems/%0A%0A# A situation is a tuple (A,B,C,D), representing AB*CD, where each element%0A# is a digit or -1 to represent an unfilled position.%0A%0A# Strategy is a %22disctionary%22 that contains optimal moves for pairs of%0A# a situation and a number (the number on the new card):%0A# E.g., Strategy%5B((A,B,C,D),N)%5D = 2 means that N should go in place of C.%0AStrategy = %7B%7D%0A%0A# Expectation is a dictionary from situations to the expectation of them%0A# given optimal strategy.%0AExpectation = %7B%7D%0A%0Adef Process(situation):%0A%09# Determine the optimal strategies for playing with this situation%0A%09# and any next card, and generate the expectation of playing %0A%09# optimally in this situation.%0A%0A%09global Strategy, Expectation%0A%09accum =  0%0A%09num_newcards = 0%0A%09for NewCard in range(10):%0A%09%09if NewCard in situation:%0A%09%09%09continue%0A%09%09least_expected_product = %5B-1,10000%5D%0A%09%09for i in range(4):%0A%09%09%09if situation%5Bi%5D == -1:%0A%09%09%09%09sitlist = list(situation)%0A%09%09%09%09sitlist%5Bi%5D = NewCard%0A%09%09%09%09newsituation = tuple(sitlist)%0A%09%09%09%09if not -1 in newsituation:%0A%09%09%09%09%09Expectation%5Bnewsituation%5D = (10*newsituation%5B0%5D+newsituation%5B1%5D)*(10*newsituation%5B2%5D+newsituation%5B3%5D)%0A%09%09%09%09elif not newsituation in Expectation:%0A%09%09%09%09%09Process(newsituation)%0A%09%09%09%09E = Expectation%5Bnewsituation%5D%0A%09%09%09%09if E %3C least_expected_product%5B1%5D:%0A%09%09%09%09%09least_expected_product = %5Bi,E%5D%0A%09%09Strategy%5B(situation,NewCard)%5D = least_expected_product%5B0%5D%0A%09%09accum += least_expected_product%5B1%5D%0A%09%09num_newcards += 1%0A%09Expectation%5Bsituation%5D = accum/num_newcards%0A%0AProcess((-1,-1,-1,-1)) %0A%0Aprint(Expectation%5B(-1,-1,-1,-1)%5D)%0A%0A

Create OptimalProduct.py