L26.html


001| '''\
001| L26 Functions.
001| 
001| functions:
001|     L80 (str,fp):str
001|     L27 (str,fp):str
001|     L26 (str,fp):str
001| 
001| commands:
001|     main    - L80 << input
001|     demo    - L26,L27,L80 << 'JeanneMarie Julia Schulte'
001|     test    - L26,L27,L80 << various edge-case strings
001| 
001| arguments:
001|     command     - main, demo, test,...
001|     input       - string to use
001|    [filename]   - output file [default: L26.out]
001| 
001| usage:
001|     module.dispatch({command}, args={arguments})    -- calling module from code
001| 
001|     $PYTHON module_name {command} {arguments}       -- executing module externally
001| 
001| coder@sonnack.com
001| April 2010
001| '''

002| ####################################################################################################
003| from sys import stdout, argv
004| from goodies import reduce, add_commas
005| ####################################################################################################
006| BasePath = r'C:\CJS\prj\Python\app\maths'
007| 
008| 
009| ##================================================================================================##
010| def L80 (_s, fp):
011|     '''L80. Encode alphanumeric string to base 80 number (0=0).'''
012|     cs = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.,;:!?"@#$%&*()-_'
013|     s = reversed(_s)
014|     a = []
015|     for d,ch in enumerate(s):
016|         f = pow(80,d)
017|         n = cs.index(ch)
018|         t = (ch, n, f, n*f)
019|         a.append(t)
020| 
021|     for t in reversed(a):
022|         print('[%s] %2d * %d = %d' % t, file=fp)
023|     print(file=fp)
024| 
025|     v = reduce(lambda acc,x: acc + x[3], a, 0)
026|     print('%s = %d' % (_s, v), file=fp)
027|     print(file=fp)
028| 
029|     return (v, a)
030| 
031| 
032| ##================================================================================================##
033| def L27 (_s, fp):
034|     '''L27. Encode uppercase alphas plus space to base 27 number (spc=0).'''
035|     s = reversed(_s.upper())
036|     b = ord('A') - 1
037|     a = []
038|     for d,ch in enumerate(s):
039|         f = pow(27,d)
040|         n = 0 if ch == ' ' else (ord(ch) - b)
041|         if n < 0: continue
042|         t = (ch, n, f, n*f)
043|         a.append(t)
044| 
045|     for t in reversed(a):
046|         print('[%s] %2d * %d = %d' % t, file=fp)
047|     print(file=fp)
048| 
049|     v = reduce(lambda acc,x: acc + x[3], a, 0)
050|     print('%s = %d' % (_s, v), file=fp)
051|     print(file=fp)
052| 
053|     return (v, a)
054| 
055| 
056| ##================================================================================================##
057| def L26 (_s, fp):
058|     '''L26. Encode uppercase alphas to base 26 number (a=0).'''
059|     s = reversed(_s.upper())
060|     b = ord('A') - 1
061|     a = []
062|     for d,ch in enumerate(s):
063|         f = pow(26,d)
064|         n = 0 if ch == 'Z' else (ord(ch) - b)
065|         if n < 0: continue
066|         t = (ch, n, f, n*f)
067|         a.append(t)
068| 
069|     for t in reversed(a):
070|         print('[%s] %2d * %d = %d' % t, file=fp)
071|     print(file=fp)
072| 
073|     v = reduce(lambda acc,x: acc + x[3], a, 0)
074|     print('%s = %d' % (_s, v), file=fp)
075|     print(file=fp)
076| 
077|     return (v, a)
078| 
079| 
080| 
081| ##================================================================================================##
082| def do_test (*args):
083|     print('test/parameters: %d' % len(args))
084|     input_txt = args[0] if 0 < len(args) else ''
085|     file_name = args[1] if 1 < len(args) else 'L26.out'
086| 
087|     print('open: %s' % file_name)
088|     fp = open(file_name, mode='w', encoding='utf-8')
089|     try:
090|         print(100 * '~', file=fp)
091|         L26(input_txt, fp)
092|         L27(input_txt, fp)
093|         L80(input_txt, fp)
094|         print(file=fp)
095| 
096|         print(100 * '~', file=fp)
097|         L26('ZABY', fp)
098|         L26('YBAZ', fp)
099|         print(file=fp)
100| 
101|         L27(' ABZ', fp)
102|         L27('ZBA ', fp)
103|         print(file=fp)
104| 
105|         L80('0AZaz_', fp)
106|         L80('_zaZA0', fp)
107| 
108|         print(100 * '~', file=fp)
109|         print(file=fp)
110| 
111|         print('wrote: %s' % file_name)
112|     except Exception as e:
113|         print(e, file=fp)
114|         raise
115|     finally:
116|         fp.close()
117|     return 'Done'
118| ##================================================================================================##
119| def do_demo (*args):
120|     print('demo/parameters: %d' % len(args))
121|     input_txt = args[0] if 0 < len(args) else 'JeanneMarie Julia Schulte'
122|     file_name = args[1] if 1 < len(args) else 'L26.out'
123| 
124|     print('open: %s' % file_name)
125|     fp = open(file_name, mode='w', encoding='utf-8')
126|     try:
127|         print(100 * '~', file=fp)
128|         print('L26', file=fp)
129|         L26(input_txt, fp)
130| 
131|         print(100 * '~', file=fp)
132|         print('L27', file=fp)
133|         L27(input_txt, fp)
134| 
135|         print(100 * '~', file=fp)
136|         print('L80', file=fp)
137|         L80(input_txt, fp)
138| 
139|         print(100 * '~', file=fp)
140| 
141|         print(file=fp)
142|         print('wrote: %s' % file_name)
143|     except Exception as e:
144|         print(e, file=fp)
145|         raise
146|     finally:
147|         fp.close()
148|     return 'Done'
149| ##================================================================================================##
150| def do_main (*args):
151|     print('main/parameters: %d' % len(args))
152|     input_txt = args[0] if 0 < len(args) else ''
153|     file_name = args[1] if 1 < len(args) else 'L26.out'
154| 
155|     print('open: %s' % file_name)
156|     fp = open(file_name, mode='w', encoding='utf-8')
157|     try:
158|         L80(input_txt, fp)
159|         L80(input_txt, stdout)
160| 
161|         print('wrote: %s' % file_name)
162|     except Exception as e:
163|         print(e, file=fp)
164|         raise
165|     finally:
166|         fp.close()
167|     return 'Done'
168| ####################################################################################################
169| def dispatch (cmd, *args):
170|     print('command: %s' % cmd)
171|     print('arguments: %d' % len(args))
172|     if cmd == 'n!': return do_factorial(*args)
173|     if cmd == 'test': return do_test(*args)
174|     if cmd == 'demo': return do_demo(*args)
175|     if cmd == 'main': return do_main(*args)
176|     return 'Nothing to do!'
177| ####################################################################################################
178| if __name__ == '__main__':
179|     print('autorun: %s' % argv[0])
180|     cmd = argv[1] if 1 < len(argv) else 'n!'
181|     etc = argv[2:] if 2 < len(argv) else []
182|     obj = dispatch(cmd, *etc)
183|     print(obj)
184| ####################################################################################################
185| '''eof'''
186|