Skip to content

Instantly share code, notes, and snippets.

@swiatczak
Last active April 5, 2016 01:50
Show Gist options
  • Select an option

  • Save swiatczak/2ec146868ebfc8d82bb3f39644748f7c to your computer and use it in GitHub Desktop.

Select an option

Save swiatczak/2ec146868ebfc8d82bb3f39644748f7c to your computer and use it in GitHub Desktop.
puzzles.py
''' silly/fun puzzles and a proof of failures (actually this version finds a solution :( )
author: swiatczak
doWork -
this one solves the following p
'''
import operator
def divide(a,b):
''' this is modded division to cater for special requirements for this puzzle - or at least
how i understood it.
So in order to avoid issues when a is None or 0
We only attempt modulo or division when it would be valid.
'''
result = None
if (a is not None) and (b is not None) and (b != 0):
result = a / b
return result
_OPERATORS_4477 = {'+': operator.add , '-': operator.sub , '*': operator.mul , '/': divide }
_NUMSTART_4477 = [4.0,4.0,7.0,7.0]
_STARTWITH_4477 = 32.0
def solve4477(startWith, nums, lev, parentStmt):
''' recursively work on the data and shrinking list of numbers
try to find the solution starting at the end - so take _STARTWITH
and try all posible operations for all each possible number. Then
Take the result of the operation and use it as a starting point
repeating the same step but with a shorter list of available
numbers (removing the one already used.)
Continue until there is only one number left in the list. This
should be equal to the result of the expresion consisting of
three other numbers. If it is not then - the solution would not work.
'''
for index in range(0, len(nums)):
num = nums[index]
# in the list to be used in the next step remove the we are currently working with
newNums = [nums[i] for i in range(0, len(nums)) if i != index]
for oper in _OPERATORS_4477:
# below we apply operator to the start value and the number we picked from the list
result = _OPERATORS_4477[oper](startWith, num)
if result is not None:
if len(newNums) == 1:
if str(result) == str(newNums[0]):
victoryFlg = 'x'
else:
victoryFlg = ' '
print '[{}] ({} {} {}) = {:5} vs {} '.format(victoryFlg, parentStmt, oper, num, result, newNums[0])
solve4477(result, newNums, lev + 1, '({} {} {})'.format(parentStmt, oper, num))
solve4477(_STARTWITH_4477, _NUMSTART_4477, 1, '{}'.format(_STARTWITH_4477))
@swiatczak
Copy link
Author

just to see if the result can be obtained with a sequence of operations.

This shows that the solution - or what is thought to be one cannot be found this way. In fact, I would argue that what may be a solution is not really one as it contains hidden storage implied but not justifiable. bleah .... i sound like a pompus f..k

one solution (possibly) to represent 32 with two fours and two sevens and +,-,*, / could be:

(7/4+4)*7
which I would say is not exactly valid

another - funny in my opinion is a square:
7.. * ..4
/ ...... +
7.. * ..4

which much better uses implied operator precendence */ then +- with implied start point at top-left 7 (justifiably or not - based on standard left-to-right notation)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment