Created
January 11, 2010 06:53
-
-
Save showyou/274044 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! -*- coding:utf-8 -*- | |
# RPN | |
# 1 2 _ 3 _ 4 _ 5 _ 6 _ 7 _ 8 _ 9 _ | |
c = [i for i in range(1,3)] | |
def select(cc, n): | |
#print cc | |
c1 = cc | |
if len(c1) >= 9 + 8: | |
a = calc(c1) | |
if a == 2010: | |
print "-- 2010=",c1 | |
#import sys | |
#sys.exit() | |
return | |
if n < 10: | |
select(c1+[n], n+1) | |
if len(c1) < (n-1) * 2: | |
select(c1+['+'], n) | |
select(c1+['-'], n) | |
select(c1+['*'], n) | |
select(c1+['/'], n) | |
def calc(cc): | |
""" | |
計算 | |
>>> a = calc([1, 2, '+']) | |
>>> a | |
3.0 | |
>>> a = calc([1, 2, '+', 3, '-']) | |
>>> a | |
0.0 | |
""" | |
calcList = [] | |
for c in cc: | |
try: | |
if c == '+': | |
calcList[-2] = calcList[-2] + calcList[-1] | |
if c == '/': | |
try: | |
calcList[-2] = calcList[-2] / calcList[-1] | |
except ZeroDivisionError: | |
return -1 | |
if c == '-': | |
calcList[-2] = calcList[-2] - calcList[-1] | |
if c == '*': | |
calcList[-2] = calcList[-2] * calcList[-1] | |
except IndexError: | |
return -1 | |
if isinstance(c,int): | |
calcList.append(float(c)) | |
else: | |
calcList.pop() | |
#print calcList | |
return calcList[0] | |
if __name__ == "__main__": | |
import doctest | |
doctest.testmod() | |
select(c,3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment