Python useful solution


  • 1

    This is not a very efficace solution. Because it use evalfunction.
    In fact I wrote a function to help me find all solutions for 24 game.
    Efficacy was not that important. Then I met this problem I just modified my original solution to return just True or False

        def judgePoint24(self, nums):
            def f(s1, s2):
                res = []
                for a in s1:
                    for b in s2:
                        res.append('(' + a + '+' + b + ')')
                        res.append('(' + a + '-' + b + ')')
                        res.append('(' + b + '-' + a + ')')
                        res.append(a + '*' + b)
                        res.append(a + '/' + b)
                        res.append(b + '/' + a)
                return res
    
            nums = [str(float(i)) for i in nums]
            for c in itertools.permutations(nums):
                eq1 = f(f(f([c[0]], [c[1]]), [c[2]]), [c[3]])
                eq2 = f(f([c[0]], [c[1]]), f([c[2]], [c[3]]))
                for eq in eq1 + eq2:
                    try:
                        if 23.9 <= eval(eq) <= 24.1:
                            print eq.replace(".0", "")
                            return True
                    except:
                        pass
            return False

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.