This is a game of math in Poker. Two people each have a pile of cards. They then each place 2 cards on the table at the same time. Every card is treated as a number. The players need to find a way to calculate 24 from the numbers. For example, you are given 3 5 4 8, and you can get 24 by (3+8-5) \* 4, or 8\*4-5-3. This code tries to solve this problem.

Can anyone help me to improve it?

**`comp()`**

```
def comp( nums,i,j,exps,op):
    nums0=[];
    for x in range(0,len(nums)):
        if not (x==i or x==j):
            nums0.append(nums[x])

    if(op=='+'):
        tmp = nums[i]+nums[j];
    elif(op=='*'):
        tmp = nums[i]*nums[j];
    elif(op=='-'):
        tmp = nums[i]-nums[j];
    elif(op=='/'):
        if(nums[j]<0.0001 and nums[j]>-0.0001 ):
            tmp=100000;
        else:
            tmp = nums[i]*1.0/nums[j];
    elif(op=='^'):
        return comp(nums,j,i,exps,'-');
    elif(op=='%'):
        return comp(nums,j,i,exps,'/');

    exps.append(str((nums[i],op,nums[j])));
    nums0.append(tmp);
    return nums0

```

**`cal()`**

```
def cal(nums, exps):
    if(len(nums)==1):
        return (nums[0]>23.9999 and nums[0]<24.0001);
    pairs = {};
    for i in range(0,len(nums)):
        for j in range(i+1,len(nums)):
            tmp_key= str((min(nums[i],nums[j]),max(nums[i],nums[j])));
            #print tmp_key
            if(pairs.has_key(tmp_key)):
                continue;
            #print 'put '+tmp_key
            pairs[tmp_key]=''
            for op in ('+','-','*','/','^','%'):
                nums0 = comp(nums,i,j,exps,op)
                if(cal(nums0,exps)):
                    print str(exps);
                exps.pop();

```

---

```
cal([3,9,10,7],[]);

cal([5,5,5,1],[]);

cal([10,9,4,1],[]);

```