~/home_Python/apple_/Scale/scale.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.31 """ ## ---------------------------------------- """ >>> ---------------------------------------- histogram 0[12] ....+....1.. 1[10] ....+....1 2[11] ....+....1. 3[13] ....+....1... 4[12] ....+....1.. 5[ 8] ....+... 6[11] ....+....1. 7[ 5] ....+ 8[11] ....+....1. 9[ 7] ....+.. 100 """ ## ---------------------------------------- ## ---------------------------------------- def step00(): """return "."*n""" def scale(n): return "."*n ## ---------------------------------------- local = locals() ex_scale0(local, 5) ## ---------------------------------------- def step01(): """for e in range(n):""" def scale(n): s = "" for e in range(n): s += "." return s ## ---------------------------------------- local = locals() ex_scale1(local, 5) ## ---------------------------------------- def step02(): """s.append(".")""" def scale(n): s = [] for e in range(n): s.append(".") return "".join(s) ## ---------------------------------------- local = locals() ex_scale2(local, 5) ## ---------------------------------------- def step03x(): """if e%5 == 0:""" def scale(n): s = [] for e in range(n): if e%5 == 0: s.append("+") s.append(".") return "".join(s) ## ---------------------------------------- local = locals() ex_scale3(local, 7) ## ---------------------------------------- def step04x(): """else:""" def scale(n): s = [] for e in range(n): if e%5 == 0: s.append("+") else: s.append(".") return "".join(s) ## ---------------------------------------- local = locals() ex_scale3(local, 11) ## ---------------------------------------- def step05(): """if not e%5:""" def scale(n): s = [] for e in range(1,n+1): if not e%5: s.append("+") else: s.append(".") return "".join(s) ## ---------------------------------------- local = locals() ex_scale4(local, 11) ## ---------------------------------------- def step06x(): """if not e%10:""" def scale(n): s = [] for e in range(1,n+1): if not e%10: s.append(str(e//10)) if not e%5: s.append("+") else: s.append(".") return "".join(s) ## ---------------------------------------- local = locals() ex_scale4(local, 12) ## ---------------------------------------- def step07(): """elif not e%5:""" def scale(n): s = [] for e in range(1,n+1): if not e%10: s.append(str(e//10)) elif not e%5: s.append("+") else: s.append(".") return "".join(s) ## ---------------------------------------- local = locals() ex_scale4(local, 12) ## ---------------------------------------- def step08(): """s.append(c)""" def scale(n): s = [] for e in range(1,n+1): c = "." if not e%10: c = str(e//10) elif not e%5: c = "+" s.append(c) return "".join(s) ## ---------------------------------------- local = locals() ex_scale5(local, 41) source = ''' s = "STRING" s = "Return a nice string representation of the object." '''.split("\n") for e in source[1:-1]: print_(e, local) X = 'print(scale(len(s))); print(s)' print_(X, local) ## ---------------------------------------- def step09(): """def scale(n, offset=1):""" def scale(n, offset=1): s = [] for e in range(offset,n+offset): c = "." if not e%10: c = str(e//10) elif not e%5: c = "+" s.append(c) return "".join(s) ## ---------------------------------------- local = locals() ex_scale5(local, 41) X = 's = "STRING"' print_(X, local) X = 'print(scale(len(s),0)); print(s)' print_(X, local) X = 's = "Return a nice string representation of the object."' print_(X, local) X = 'print(scale(len(s),offset=0)); print(s)' print_(X, local) ## ---------------------------------------- from random import * def step_histo(): """Histogram""" def scale(n, offset=1): s = [] for e in range(offset,n+offset): c = "." if not e%10: c = str(e//10) elif not e%5: c = "+" s.append(c) return "".join(s) ## ---------------------------------------- histo = {} #1: for _ in range(100): n = randint(0, 9) histo[n] = histo.get(n, 0) + 1 for k, v in histo.items(): #2: print("{0:d}[{1:2d}]{2}".format(k, v, scale(v))) print("sum:",sum(histo.values())) for e in "min","max": #3: value = eval("%s(histo.values())"%e) s = [k for k,v in histo.items() if v==value] print("%s: %d %s"%(e,value,s)) ## ---------------------------------------- ''' >>> # -------------------------------------------------- step_body >>> path '/Applications/Python 3.1/Extras/Demo/scripts/queens.py' >>> ex_body(path, end=5) 1: #! /usr/bin/env python 2: 3: """N queens problem. 4: 5: The (well-known) problem is due to Niklaus Wirth. >>> ex_body(path, start=81) 81: q.solve() 82: print("Found", q.nfound, "solutions.") 83: 84: if __name__ == "__main__": 85: main() >>> ex_body(path, 14, 18) 14: class Queens: 15: 16: def __init__(self, n=N): 17: self.n = n 18: self.reset() >>> ''' ## ---------------------------------------- def step_body(): """def body(name, start=1, end=None, number=True):""" #### def body(name, start=1, end=None, number=True): #### s = open(name).readlines(); N = len(s) #### if not end: end = N+1 #### if number: #### for n, e in enumerate(s): #### s[n] = "{{0:{0}d: {1}}}".format(len(str(N))%(n+1, e)) #### return s[start-1:end] def body(name, start=1, end=None): seq, n = _body(name, start, end) s = "{{0:{0:d}d}}: {{1}}".format(len(str(n))) return [s.format(start+i, e) for i, e in enumerate(seq)] def _body(name, start, end): s = open(name).readlines(); N = len(s) if not end: end = N+1 return s[start-1:end], N ## ---------------------------------------- def ex_body(name, start=1, end=None): for e in body(name, start, end): print(e,end="") print() path = "/Applications/Python 3.1/Extras/Demo/scripts/" path += "queens.py" def ex(local): #@: source = ''' path ex_body(path, end=5) ex_body(path, start=81) ex_body(path, 14, 18) '''.split("\n") for e in source[1:-1]: print_(e, local) local = locals(); ex(local) ## ---------------------------------------- ## ---------------------------------------- def print_(source, local, mode="single"): print(">>>",source) eval(compile(source,"",mode),globals(),local) def ex_scale0(local, n): for e in range(n): X = 'scale({0})'.format(e) print_(X, local) def ex_scale1(local, n): X = 'for e in range({0}): print(scale(e))'.format(n) print_(X, local, "exec") def ex_scale2(local, n): X = 'for e in range({0}): print(e,"|",scale(e))'.format(n) print_(X, local, "exec") def ex_scale3(local, n): X = 'for e in range({0}): print(e,"|",scale(e),sep="")'.format(n) print_(X, local, "exec") def ex_scale4(local, n): X = 'for e in range({0}): print("%2d"%e,"|",scale(e),sep="")'.format(n) print_(X, local, "exec") def ex_scale5(local, n): X = 'for e in range(0,{0},4): print("%2d"%e,"|",scale(e),sep="")'.format(n) print_(X, local, "exec") ## ======================================== 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 from sys import argv, version def inform(): n = 60 print("="*n) print("Version: %s"%version.split("\n")[0]) print("Module : %s"%argv[0].split("/")[-1]) print("Date : %s"%ctime()) print("="*n) ## ---------------------------------------- from doctest import testmod if __name__=='__main__': inform() testmod() ## ========================================
Last updated♪2010/03/23