~/home_Python/apple_/Polish/polish.py
|INDEX| Python.use(better)
》作業中です《
#! /usr/bin/env python # coding: utf-8 ## ---------------------------------------- ## ## (C) Copyright 2000-2010, 小粒ちゃん《監修》小泉ひよ子とタマゴ倶楽部 ## ## ---------------------------------------- # ..+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 """ >>> None >>> ## ---------------------------------------- >>> None version: #1.0.07 """ ## ---------------------------------------- """ >>> a,b = "AZ" >>> s = [chr(e) for e in range(ord(a),ord(b)+1)] >>> "".join(s) >>> for e in range(len(s)): a,*b = s; s = b+[a]; "".join(s) """ ## ---------------------------------------- class Stack(list): def push(self, item): if item is None: return self.append(item) def polish(s): """ >>> polish("") >>> polish("3") 3 >>> polish("3 4 +") 7 >>> polish("3 4 + 2 *") 14 >>> polish("3 4 + 2 * 5 -") 9 >>> polish("3 4 2 + *") 18 >>> polish("3 4 * 2 /") 6.0 >>> testcase = [ ... "", ... "3", ... "3 4 +", ... "3 4 + 2 *", ... "3 4 + 2 * 5 -", ... "3 4 2 + *", ... "3 4 * 2 /", ... ] >>> for e in testcase: polish(e) 3 7 14 9 18 6.0 >>> None version: #1.0.05 """ s = Stack(s.split()) acc = None for e in s: if e in "+-*/": op = s.pop() acc = eval("%s %s %s"%(op, e, acc)) else: s.push(acc) acc = eval("%s"%e) else: return acc ## ---------------------------------------- def itok_pre(f): """ >>> # 異常ケースのある風景 >>> itok(-1) Traceback (most recent call last): ... AssertionError: itok(>> -1 <<) # 0..9999 >>> itok(10000) Traceback (most recent call last): ... AssertionError: itok(>> 10000 <<) # 0..9999 >>> None version: #1.0.11 """ def pre(n): """ >>> # 正常ケースのある風景 >>> s = 0,1,9,10,12,30,34,99,100,105,110,116,170,178,900,901,1000,1001,9999 >>> for e in s: "%4d: %s"%(e, itok(e)) ' 0: 零' ' 1: 壱' ' 9: 玖' ' 10: 拾' ' 12: 拾弐' ' 30: 参拾' ' 34: 参拾肆' ' 99: 玖拾玖' ' 100: 佰' ' 105: 佰伍' ' 110: 佰拾' ' 116: 佰拾陸' ' 170: 佰漆拾' ' 178: 佰漆拾捌' ' 900: 玖佰' ' 901: 玖佰壱' '1000: 阡' '1001: 阡壱' '9999: 玖阡玖佰玖拾玖' >>> None version: #1.0.13 """ lower,upper = 0,9999 assert lower <= n <= upper, ( "itok(>> %s <<) # %s..%s"%(n, lower, upper)) return f(n) return pre @itok_pre def itok(n): """ itok(n) # 0..9999 """ dig = "零壱弐参肆伍陸漆捌玖" pos = "拾佰阡〇" if not n: return dig[n] car,*cdr = str(n)[::-1] n = int(car) acc = [dig[n]] if n else [] acc = ([dig[n]], [])[not n] ## acc = ([], [dig[n]])[n] for i,e in enumerate(cdr): n = int(e) if not n: continue acc.append(pos[i]) if n < 2: continue acc.append(dig[n]) return "".join(acc[::-1]) ## ---------------------------------------- ## ---------------------------------------- class Rtoi: """ >>> # ローマ数字 Roman numerals >>> # ローマ数字 -> 整数 >>> rtoi = Rtoi() >>> s = ",I,IV,V,VI,IX,X,XI,XL,L,LX,XC,C,CX,CD,D,DC,CM,M,MC,MMMCMXCIX" >>> s = s.split(",") >>> for e in s: "%4d: %s"%(rtoi(e), e) ' 0: ' ' 1: I' ' 4: IV' ' 5: V' ' 6: VI' ' 9: IX' ' 10: X' ' 11: XI' ' 40: XL' ' 50: L' ' 60: LX' ' 90: XC' ' 100: C' ' 110: CX' ' 400: CD' ' 500: D' ' 600: DC' ' 900: CM' '1000: M' '1100: MC' '3999: MMMCMXCIX' >>> None version: #1.0.33 """ mapping = {} def __init__(self): class_ = self.__class__ if not class_.mapping: m = {"@": 0} m.update(self.romanNumerals()) class_.mapping = m def __call__(self, s): return self._value(s) def romanNumerals(self): s1,s2 = "IXCM","VLD" m1 = {e: 10**s1.index(e) for e in s1} m2 = {e:5*10**s2.index(e) for e in s2} m1.update(m2) return m1 def _value(self, s): acc = 0 c = "@" for e in s: acc += self._value1(c, e) c = e return acc def _value1(self, c1, c2): n1 = self.mapping[c1] n2 = self.mapping[c2] return (n2, n2-n1*2)[n1 < n2] ## ---------------------------------------- class Itor: """ >>> # ローマ数字 Roman numerals >>> # 整数 -> ローマ数字 >>> itor = Itor() >>> s = 0,1,3,4,5,6,8,9,10,14,19,40,50,90,100,400,900,1000,1444,1666,3999 >>> for e in s: "%4d: %s"%(e, itor(e)) ' 0: ' ' 1: I' ' 3: III' ' 4: IV' ' 5: V' ' 6: VI' ' 8: VIII' ' 9: VIV' ' 10: X' ' 14: XIV' ' 19: XVIV' ' 40: XL' ' 50: L' ' 90: LXL' ' 100: C' ' 400: CD' ' 900: DCD' '1000: M' '1444: MCDXLIV' '1666: MDCLXVI' '3999: MMMDCDLXLVIV' >>> None version: #1.0.41 """ mapping = {} rating = [] def __init__(self): class_ = self.__class__ if not class_.mapping: m = {0: "@"} m.update(self.romanNumerals()) class_.mapping = m if not class_.rating: s = [] for e in range(len("IXCM")): s.append(10**e//2) s.append(10**e) class_.rating = s[::-1][:-1] def __call__(self, s): return self._value(s) def romanNumerals(self): s1,s2 = "IXCM","VLD" m1 = {e: 10**s1.index(e) for e in s1} m2 = {e:5*10**s2.index(e) for e in s2} m1.update(m2) return {v:k for k,v in m1.items()} def _value(self, n): s = [] for e in self.rating: m = n//e; n %= e s.append(self.mapping[e]*m) return self._value1(s) def _value1(self, s): for e,f in zip("IXC","VLD"): t = e*4 if t in s: s[s.index(t)] = e+f return "".join(s) ## ---------------------------------------- def happy(arg): print("Are you happy():", arg) happy("?") print("-"*40) print(happy) print("="*40) def piyo(arg): print("I am piyo():", arg) return "Yes, as happy as happy can be." @piyo def happy(arg): print("I am happy():", arg) print("-"*40) print(happy) ## happy = piyo(happy) ## ---------------------------------------- ## ---------------------------------------- ##def step00(): ## ---------------------------------------- ## ---------------------------------------- def print_(source, local, mode="single"): print(">>>",source) eval(compile(source,"",mode),globals(),local) def ex_stack(local): #@: source = ''' s = Stack(); s s.push("A"), s s.push("B"), s s.pop(), s s.pop(), s s.pop(), s '''.split("\n") for e in source[1:-1]: try: print_(e, local) except IndexError as error: print("IndexError:", error) ## ======================================== s = "step" DIR = [e for e in dir() if e.startswith(s)] DIR = dict((i,e) for i,e in enumerate(DIR)) DIR = list(DIR.items()); DIR.sort() def ex(): n = (len(DIR)-1)//10+1 for k,v in DIR: source = ''' x = eval("%%s.__doc__"%%v) print("%%%dd: %%s -- %%s"%%(k,v,x)) '''%n eval(compile(source,"","exec")) def do_all(): for k,v in DIR: do(k) def do(k=None): if k is None: ex(); return try: k,v = DIR[k] print(">>> #","-"*50,v) eval("%s()"%v) except IndexError: print("(x_x) too bad ... %s"%k) ## ---------------------------------------- from time import ctime def time_stamp(): print("="*24) print(ctime()) print("="*24) ## ---------------------------------------- from doctest import testmod if __name__=='__main__': time_stamp() testmod() ## ========================================
Last updated♪2010/03/15