Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ReckonPrimer.activity/functions.py
blob: 0c8f6465e5666974113a6b3a28e6899f206acf03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# -*- coding: utf-8 -*-
"""the module for functions of reckonprimer.py.
placed in reconprimer.py caused recursive import."""
import copy
def reverse_(list):
    """list.reverse() returns None; reverse_ returns the reversed list"""
    li = copy.deepcopy(list[3:5])  # make a copy
    li.reverse()
    del list[3:5]
    list = (li + list)
    return list
    
def get_until(char, chars):
    _i, _chars = 0, []
    while chars[_i] != char:
        #print('in get_until, chars[', _i, ']=', chars[_i])
        _chars.append(chars[_i])
        _i = _i + 1
    return _chars
    
def get_after(char, chars):
    _i, _chars, _max = 0, [], len(chars)
    while (chars[_i] != char) & (_i < _max):
        print('in get_after, chars[', _i, ']=', chars[_i])
        _i = _i + 1
    return chars[_i+1:]

def extend_(chars1, chars2):
    _chars = copy.deepcopy(chars1)  # make a copy
    _chars.extend(chars2)
    return _chars 

def swap_eq(chars):
    _i, _chars = 0, []
    while chars[_i] != '=':
        #print('in swap_eq chars[', _i, ']=', chars[_i])
        _chars.append(chars[_i])
        _i = _i + 1
    return extend_(extend_(chars[_i+1:], ['=']), _chars)
    
#_c = ['2','+','3','=','5']
#print('_c      =', _c)
#_c1 = get_until('=', _c)
#print('get_until', _c1)
#_c2 = get_after('=', _c)
#print('get_after', _c2)
#_c3 = extend_(_c1, _c2)
#print('extend_  ', _c3)
#_c4 = swap_eq(_c)
#print('swap_eq  ', _c4)

def contain(chars, char):
    """do chars (list of characters) contain char ?"""
    if len(chars) > 0:
        for _c in chars:
            if _c == char:
                return True
        else:
            return False
    else:
        return False
#chrs = ['2','+','3',\
#        '=','5']
#print('contain([\'2\',\'+\',\'3\',\'=\',\'5\'], \'+\')', contain(chrs, '+'))
#print('contain([\'2\',\'+\',\'3\',\'=\',\'5\'], \'-\')', contain(chrs, '-'))

def make_blanks(n):
    """create a list of n blanks"""
    _l = []
    for i in range(0, n):
        _l.append(' ')
    return _l
#xxx = make_blanks(3)
#print(xxx)
#xxx[1]='+'
#print(xxx)

#ls = [11,22,33]
#[a,b,c] = ls
#print('in functions: a=',a,', b=',b,', c=',c)
def collect_digits(calc):
    """collect the digits of each number in a calc into a list"""
    _coll_calc, _i = [], 0
    while _i < len(calc):
        _coll_no = []
        while not(calc[_i] in ['=','+','-','*',':','in','|']):
            _coll_no.append(calc[_i])
            #print('in collect_digits: _coll_calc=',_coll_calc,', _i=',_i,\
            #      ', _coll_no=',_coll_no)
            _i = _i+1
            if _i >= len(calc):
                _coll_calc.append(_coll_no)
                return _coll_calc
        _coll_calc.append(_coll_no)
        _coll_calc.append(calc[_i])
        _i = _i+1
#cs = collect_digits(['1','2','+','3','4','=','4','6'])
#print('in functions: cs=', cs)
##('in functions: cs=', [['1', '2'], '+', ['3', '4'], '=', ['4', '6']])
#cs = collect_digits(['1','+','2','=','3'])
#print('in functions: cs=', cs)
##('in functions: cs=', [['1'], '+', ['2'], '=', ['3']])

def make_line(coll_calc, linepos):
    """make a calc (returned by collect_digits) to a line to display;
    linepos is 1 or 3 or 5."""
    _i, _line = 0, [' ']
    for _c in coll_calc:
        #print('in make_line: _c=', _c)
        if _i == linepos -1:
            for _d in _c:
                _line.append('_')
        else:
            for _d in _c:
                #print('in make_line: _d=', _d)
                if _d in ['=','+','-','*',':','in']:
                    _line.extend([' ',_d,' '])
                else:
                    _line.append(_d)
        _i = _i + 1
    _line.append(' ')
    return _line
#coll_calc = [['1', '2'], '+', ['3', '4'], '=', ['4', '6']]
#print('in functions: make_line(coll_calc, 1)=', make_line(coll_calc, 1))
##[' ', '_', '_', ' ', '+', ' ', '3', '4', ' ', '=', ' ', '4', '6', ' ']
#print('in functions: make_line(coll_calc, 3)=', make_line(coll_calc, 3))
##[' ', '1', '2', ' ', '+', ' ', '_', '_', ' ', '=', ' ', '4', '6', ' ']
#print('in functions: make_line(coll_calc, 5)=', make_line(coll_calc, 5))
##[' ', '1', '2', ' ', '+', ' ', '3', '4', ' ', '=', ' ', '_', '_', ' ']
#coll_calc = [['1'], '+', ['2'], '=', ['3']]
#print('in functions: make_line(coll_calc, 1)=', make_line(coll_calc, 1))
##[' ', '_', ' ', '+', ' ', '2', ' ', '=', ' ', '3', ' ']
#print('in functions: make_line(coll_calc, 3)=', make_line(coll_calc, 3))
##[' ', '1', ' ', '+', ' ', '_', ' ', '=', ' ', '3', ' ']
#print('in functions: make_line(coll_calc, 5)=', make_line(coll_calc, 5))
##[' ', '1', ' ', '+', ' ', '2', ' ', '=', ' ', '_', ' ']


def make_line_remainder(coll_calc):
    """make a calc (returned by collect_digits) to a line to display"""
    _i, _line = 0, [' ']
    for _c in coll_calc:
        if _i == 4 or _i == 6:
            for _d in _c:
                #print('in make_line_remainder: _c, _d=', _c, _d)
                _line.append('_')
        else:
            if _c == 'in':
                _line.extend([' ',_c,' '])
            else:
                for _d in _c:
                    #print('in make_line: _c, _d=', _c, _d)
                    if _d in ['=','+','-','*',':']: #not '|'
                        _line.extend([' ',_d,' '])
                    else:
                        _line.append(_d)
        _i = _i + 1
    _line.append(' ')
    return _line
#coll_calc = [['2'], 'in', ['2'], '=', ['1'], '|', ['0']]
#print(make_line_remainder(coll_calc))


def make_input(coll_calc, linepos):
    """make a list of inputs formatted for Display in one line.
    take car of 'in' from times_div"""
    _indig_cnt, _ins = 0, []
    for _indig in coll_calc[linepos-1]:
        _c_cnt, _inline, _in_idx, _in_ahead = 0, [' '], 1, True
        for _c in coll_calc: #build 1 line (see make_line)
            #print('in make_input: _c_cnt=', _c_cnt,', _c=',_c)
            if _c_cnt == linepos-1:
                _d_cnt = 0
                for _d in _c:
                    #print('in make_input:     _d_cnt=',_d_cnt,', _d=',_d)
                    if _indig_cnt >= _d_cnt:
                        _inline.append(_d)
                        _in_ahead = False
                        #_in_idx = _in_idx + 1
                    else:
                        _inline.append('_')
                    _d_cnt = _d_cnt + 1
            else:
                if _c == 'in':
                    _inline.extend([' ',_c,' '])
                    if _in_ahead:
                            _in_idx = _in_idx + 3
                else:
                    for _d in _c:
                        #print('in make_input: _d=', _d)
                        if _d in ['=','+','-','*',':','in']:
                            _inline.extend([' ',_d,' '])
                            if _in_ahead:
                                _in_idx = _in_idx + 3
                        else:
                            _inline.append(_d)
                            if _in_ahead:
                                _in_idx = _in_idx + 1
            _c_cnt = _c_cnt + 1
        _inline.append(' ')
        _idx = _in_idx + _indig_cnt
        _indig = coll_calc[linepos-1][_indig_cnt]
        _prot_err = copy.deepcopy(_inline)
        _prot_err[_idx] = '_'
        _in = (0, _idx, _indig, \
               ''.join(_prot_err), ''.join(_inline), \
               _inline)
        _ins.append(_in)
        _indig_cnt = _indig_cnt + 1
    _ins.reverse() #for pop
    return _ins
####################################################################
#ins = make_input([['1','2','3'], '+', ['4'], '=', ['1','2','7']], 5)
#print(ins)
#[(0, 13, '7', ' 123 + 4 = 12_ ', ' 123 + 4 = 127 ',
#  [' ', '1', '2', '3', ' ', '+', ' ', '4', ' ', '=', ' ', '1', '2', '7',' ']),
# (0, 12, '2', ' 123 + 4 = 1__ ', ' 123 + 4 = 12_ ',
#  [' ', '1', '2', '3', ' ', '+', ' ', '4', ' ', '=', ' ', '1', '2', '_',' ']),
# (0, 11, '1', ' 123 + 4 = ___ ', ' 123 + 4 = 1__ ',
#  [' ', '1', '2', '3', ' ', '+', ' ', '4', ' ', '=', ' ', '1', '_', '_',' '])]
#ins = make_input([['1'], '*', ['2'], '=', ['2']], 5)
#print(ins)
#[(0, 9, '2', ' 1 * 2 = _ ', ' 1 * 2 = 2 ',
#  [' ', '1', ' ', '*', ' ', '2', ' ', '=', ' ', '2', ' '])]


def make_input_remainder(strs, res, rem):
    """make the 2 lines for input result and remainder with ':' and 'in'"""
    #print('in make_input_remainder: len(strs)=', len(strs))
    #print('in make_input_remainder: strs=', strs)
    #print('in make_input_remainder: res=', res, ', rem=', rem)
    _strs = copy.deepcopy(strs)
    if len(_strs) == 13: # 9 : 2 = 4|1
        _line1 = copy.deepcopy(_strs)
        _strs[9] = res[0]
        _line2 = copy.deepcopy(_strs)
        _strs[11] = rem[0]
        _line3 = copy.deepcopy(_strs)
        #print('in make_input_remainder: line1,2,3=', _line1, _line2, _line3)
        _ins = [(0,  9, res[0], "".join(_line1), "".join(_line2), _line2),\
                (0, 11, rem[0], "".join(_line2), "".join(_line3), _line3)]
    elif len(_strs) == 14: # 17 : 2 = 8|1
        _line1 = copy.deepcopy(_strs)
        _strs[10] = res[0]
        _line2 = copy.deepcopy(_strs)
        _strs[12] = rem[0]
        _line3 = copy.deepcopy(_strs)
        _ins = [(0, 10, res[0], "".join(_line1), "".join(_line2), _line2),\
                (0, 12, rem[0], "".join(_line2), "".join(_line3), _line3)]
    elif len(_strs) == 15: # 20 : 2 = 10|0
        _line1 = copy.deepcopy(_strs)
        _strs[10] = res[0]
        _line2 = copy.deepcopy(_strs)
        _strs[11] = res[1]
        _line3 = copy.deepcopy(_strs)
        _strs[13] = rem[0]
        _line4 = copy.deepcopy(_strs)
        _ins = [(0, 10, res[0], "".join(_line1), "".join(_line2), _line2),\
                (0, 11, res[1], "".join(_line2), "".join(_line3), _line3),\
                (0, 13, rem[0], "".join(_line3), "".join(_line4), _line4)]
    else:
        print('in make_input_remainder: ERROR len(_strs)=', len(_strs))
        #exit
    _ins.reverse() # for pop()
    return _ins
#strs = [' ', '9', ' ', ':', ' ', '2', ' ', '=', ' ', '_', '|', '_', ' ']
#print(make_input_remainder(strs, ['4'], ['1']))
#[(0, 9, '4', ' 9 : 2 = _|_ ', ' 9 : 2 = 4|_ ',
#  [' ', '9', ' ', ':', ' ', '2', ' ', '=', ' ', '4', '|', '_', ' ']),
# (0, 11, '1', ' 9 : 2 = 4|_ ', ' 9 : 2 = 4|1 ',
#  [' ', '9', ' ', ':', ' ', '2', ' ', '=', ' ', '4', '|', '1', ' '])]
#strs = [' ', '1','7', ' ', ':', ' ', '2', ' ', '=', ' ', '_', '|', '_', ' ']
#print(make_input_remainder(strs, ['8'], ['1']))
#[(0, 10, '8', ' 17 : 2 = _|_ ', ' 17 : 2 = 8|_ ',
#  [' ', '1', '7', ' ', ':', ' ', '2', ' ', '=', ' ', '8', '|', '_', ' ']),
# (0, 12, '1', ' 17 : 2 = 8|_ ', ' 17 : 2 = 8|1 ',
#  [' ', '1', '7', ' ', ':', ' ', '2', ' ', '=', ' ', '8', '|', '1', ' '])]
#strs = [' ','2','0',' ',':',' ','2',' ','=',' ','_','_','|','_',' ']
#print(make_input_remainder(strs, ['1','0'], ['0']))
#[(0, 10, '1', ' 20 : 2 = __|_ ', ' 20 : 2 = 1_|_ ',
#  [' ', '2', '0', ' ', ':', ' ', '2', ' ', '=', ' ', '1', '_', '|', '_',' ']),
# (0, 11, '0', ' 20 : 2 = 1_|_ ', ' 20 : 2 = 10|_ ',
#  [' ', '2', '0', ' ', ':', ' ', '2', ' ', '=', ' ', '1', '0', '|', '_',' ']),
# (0, 13, '0', ' 20 : 2 = 10|_ ', ' 20 : 2 = 10|0 ',
#  [' ', '2', '0', ' ', ':', ' ', '2', ' ', '=', ' ', '1', '0', '|', '0',' '])]

def strip(chars, c):
    """strip all characters c from a list of characters chars"""
    _cs = []
    for _c in chars:
        if _c != c:
            _cs.append(_c)
    return _cs
#print(strip(['1','2','3','0','4','0'], '0'))
#['1', '2', '3', '4']

def to_str_99(i):
    """ Convert a number to their list of digits (as characters). """
    _digs = []
    _lead, _dig = divmod(i, 10)
    _digs.append(str(_dig))
    while _lead > 9:
        _lead, _dig = divmod(_lead, 10)
        _digs.append(str(_dig))
    if _lead > 0:
        _digs.append(str(_lead))
    _digs.reverse()
    return _digs

def flatten(ls):
    """flatten a list of lists; only one level"""
    _flat = []
    for _e in ls:
        if _e == 'in':
            _flat.append(_e)
        else:
            for _ei in _e:
                #print(_e, '  ', _ei)
                _flat.append(_ei)
    return _flat
#print(flatten([['1', '2'], '+', ['3', '4'], '=', ['4', '6']]))
#['1', '2', '+', '3', '4', '=', '4', '6']
#print(flatten([['2'], 'in', ['2', '0'], '=', ['1', '0'], '|', ['0']]))
#['2', 'in', '2', '0', '=', '1', '0', '|', '0']