diff options
author | Walther Neuper <wneuper@localhost.(none)> | 2010-05-21 06:36:39 (GMT) |
---|---|---|
committer | Walther Neuper <wneuper@localhost.(none)> | 2010-05-21 06:36:39 (GMT) |
commit | a8801d6881f82be01845d5b3c8872b562638be4b (patch) | |
tree | 31f56470a66862a469d672aef2f4ac4037c6cabf /ReckonPrimer.activity | |
parent | 561c3272ba205a1aa3b1f6b2156504e71fa0bc8b (diff) |
end session schoenhart, zierler: flaw in calc interaction
Diffstat (limited to 'ReckonPrimer.activity')
-rw-r--r-- | ReckonPrimer.activity/exercises/exaddsub.py | 334 | ||||
-rw-r--r-- | ReckonPrimer.activity/exercises/exercise.py | 8 | ||||
-rw-r--r-- | ReckonPrimer.activity/exstore.py | 87 | ||||
-rw-r--r-- | ReckonPrimer.activity/functions.py | 751 | ||||
-rw-r--r-- | ReckonPrimer.activity/learner.py | 7 |
5 files changed, 753 insertions, 434 deletions
diff --git a/ReckonPrimer.activity/exercises/exaddsub.py b/ReckonPrimer.activity/exercises/exaddsub.py index ed3d8d9..19e63c4 100644 --- a/ReckonPrimer.activity/exercises/exaddsub.py +++ b/ReckonPrimer.activity/exercises/exaddsub.py @@ -1,12 +1,322 @@ -# -*- coding: utf-8 -*- -#(c) Julia Schönhart 2009 -#(c) Daniela Zierler 2009 - -from exercise import Exercise - -class ExAddSub(Exercise): - def __init__(self, display, (sett, errors)): - pass - - def _generate_calcs(self): - pass +# -*- coding: utf-8 -*-
+#(c) Julia Schönhart
+#(c) Daniela Zierler
+
+import gtk
+import pygtk
+import pango
+import random
+import copy
+from sugar.graphics import style
+
+from exercise import Exercise
+from functions import *
+
+class ExAddSub(Exercise):
+ def __init__(self, dis, (sett, errors)):
+ self._display = dis
+ self._errors = errors
+ self._sett = sett
+# {'topic' : 'exaddsub',
+# 'title' : 'template ExAddSub',
+# 'descript': 'TODO',
+# 'icon' : None, # for quick reference of the exercise
+# 'calclines': 1, # no. of lines for calc to be input.
+# 'MAX' : 50, # maximum of calcs generated.
+# # Generate fills up by varying input.
+# 'MIN' : 20, # minimum of calcs generated UNUSED
+# 'min' : 0, # minimum in size of a number in a calc
+# 'max' : 5, # maximum in size of a number in a calc
+# # 0 <= min <= max <= 10
+# 'min_pos': 1 # min position of calcultion (always 1) DO NOT CHANGE!
+# 'max_pos': 5, # maximum positions of calculation
+# 'min_lines': 2 # min lines (always 2) DO NOT CHANGE!
+# 'max_lines': 2, # maximum lines
+# 'max_calcs' : 4, # numbers of calculations
+# '+' : False, # make all additions min..max
+# '-' : False, # make all subtactions min..max
+# '_+_=_' : True, # = is _right_ from operator, e.g. 1+2=3
+# 'input=' : [1,3,5],# list of positions in calc: 1 | 3 | 5
+# # where input is possible;
+# # actual positions chosen by Generate.
+# '_=_+_' : False, # = is _left_ from operator, e.g. 3=1+2
+# '=input' : [1,3,5],# analogous to '_+_=_'
+# 'shuffle': True, # shuffle _all_ the calcs
+# 'cut-max': True # cut set of all calcs down to MAX
+# }
+ self._calcs = self._generate_calcs()
+
+ def _generate_calcs(self):
+ _dic = self._sett
+ _calcs = []
+ if _dic['+']: # '+' or '-' are True
+ _c = self._alladd(_dic['min_pos'],_dic['max_pos'], _dic['MAX'])
+ if _dic['_+_=_']: # '_+_=_' or '_=_+_' are True
+ # choose 1 actual position for input
+ _cc = [(c, random.choice(_dic['input='])) for c in _c]
+ _calcs.extend(_cc)
+ if _dic['_=_+_']:
+ _cc = [swap_eq(c) for c in _c]
+ # choose 1 actual position for input
+ _cc = [(c, random.choice(_dic['=input'])) for c in _cc]
+ _calcs.extend(_cc)
+ if _dic['-']:
+ _c = self._allsub(_dic['min'], _dic['max'])
+ if _dic['_+_=_']:
+ _cc = [(c, random.choice(_dic['input='])) for c in _c]
+ _calcs.extend(_cc)
+ if _dic['_=_+_']:
+ _cc = [swap_eq(c) for c in _c]
+ _cc = [(c, random.choice(_dic['=input'])) for c in _cc]
+ _calcs.extend(_cc)
+ #if len(_calcs) < _dic['MIN']: TODO
+ if _dic['shuffle']:
+ random.shuffle(_calcs)
+ if _dic['cut-max']:
+ _c = copy.deepcopy(_calcs) # make a copy
+ _calcs = _c[:_dic['MAX']]
+ #print('in exaddsub.addsub_simp calcs=', _calcs)
+ return [['6','5','+','4','3','=','1','0','8'],
+ ['6','5','+','4','3','=','1','0','8']]
+ #return _calcs
+
+ def format(self, calc):
+ """format the calc for display, prepare overlays for input"""
+ _ccs = collect_digits(calc)
+ #print('in Display.format_addsub_simp: _ccs=',_ccs )
+ _l0 = self.make_line_sub(_ccs)
+ _ip = self.make_input_sub(_ccs)
+ return (_l0, _ip)
+
+ def make_line_sub(self,calc):
+ """from ([['6','5'],'+',['4','3'],'=',['1','0','8']])
+ make [[' ',' ',' ',' ',' ','6','5',' ',' ',' ',' ',' '],
+ [' ',' ',' ',' ','+','4','3',' ',' ',' ',' ',' '],
+ [' ',' ',' ',' ','-','-','-',' ',' ',' ',' ',' '],
+ [' ',' ',' ',' ','-','-','-',' ',' ',' ',' ',' ']]
+ """
+ _width = 14 #_width = 14 is Display.width,
+ _blanksright = 4 #FIXXXME: _blanksright depends on _width when centering
+ _line1 = make_chars(' ', _width - len(calc[0]) - _blanksright) + \
+ calc[0] + \
+ make_chars(' ', _blanksright)
+ _line2 = make_chars(' ', _width - len(calc[2]) - _blanksright-1)+[calc[1]]+\
+ calc[2] + \
+ make_chars(' ', _blanksright)
+ _line3 = make_chars(' ', _width - max_len(calc) - _blanksright) + \
+ make_chars('-', max_len(calc)) + \
+ make_chars(' ', _blanksright)
+ _line4 = make_chars(' ', _width - len(calc[4]) - _blanksright) +\
+ make_chars('_', len(calc[4])) + \
+ make_chars(' ', _blanksright)
+ return [_line1, _line2, _line3, _line4]
+
+ def make_input_sub(self, calc):
+ """from ([['6','5'],'+',['4','3'],'=',['1','0','8']])
+ make [(3,6,'8','65+43=___','65+43=__8',
+ [' ',' ',' ',' ','-','-','8',' ',' ',' ',' ',' ']),
+ (3,5,'0','65+43=__8','65+43=_08',
+ [' ',' ',' ',' ','-','0','8',' ',' ',' ',' ',' ']),
+ (3,4,'1','65+43=_08','65+43=108',
+ [' ',' ',' ',' ','1','0','8',' ',' ',' ',' ',' '])]
+ TODO _width = 14 is Display.width,
+ TODO _blanksright depends on _width when centering
+ """
+ _blanksright = 4
+ _width = 14
+ _lines = []
+ _sum = calc[4]
+ _lensum = len(_sum)
+ _protoc = flatten(calc[0:-1]) + make_chars('_', len(_sum))
+ for i in range (len(_sum)):
+ _input = _sum.pop()
+ _protoc2 = copy.deepcopy(_protoc)
+ _protoc2[len(_protoc)-i-1] = _input
+ _line = (3, 10-i, _input,
+ strs2str(flatten(_protoc)), strs2str(flatten(_protoc2)),
+ make_chars(' ', _width - _lensum - _blanksright) +
+ _protoc2[-_lensum:] +
+ make_chars(' ', _blanksright))
+ _lines.append(_line)
+ _protoc = copy.deepcopy(_protoc2)
+ return _lines
+
+ def make_blanks(self,num_blanks):
+ _c = [' ']
+ for i in range(1,num_blanks+1):
+ _c.append(' ')
+ return _c
+
+ def make_underline(self,num_underline):
+ _c = []
+ for i in range(1,num_underline+1):
+ _c.append('_')
+ return _c
+
+ def define_buttons(self):
+
+ # Buttons V top
+ self.arrow_butts = []
+ for i in range(1,6):
+ self.toggle = gtk.ToggleButton("V")
+ self.toggle_label = self.toggle.get_child()
+ self.toggle.modify_font(pango.FontDescription("sans 12"))
+ self.toggle.connect("toggled", self.toggle_arrow_callback, i)
+ self._display.settings_table.attach(self.toggle, 7-i, 8-i, 3, 4 )
+ self.toggle.show()
+ self.arrow_butts.append(self.toggle)
+
+ # Buttons < right
+ self.dice_butts = []
+ for i in range(1,3):
+ self.toggle = gtk.ToggleButton("<")
+ self.toggle_label = self.toggle.get_child()
+ self.toggle.modify_font(pango.FontDescription("sans 12"))
+ self.toggle.connect("toggled", self.toggle_dice_callback, i)
+ self._display.settings_table.attach(self.toggle, 6, 7, 9-i, 10-i )
+ self.toggle.show()
+ self.dice_butts.append(self.toggle)
+
+ # + Button
+ self.toggle_plus = gtk.ToggleButton("+")
+ self.toggle_label = self.toggle_plus.get_child()
+ self.toggle_label.modify_font(pango.FontDescription("sans %d" % style.zoom(12)))
+ self.toggle_plus.connect("toggled", self.toggle_plus_callback)
+ self._display.settings_table.attach(self.toggle_plus, 0, 1, 8, 9 )
+ self.toggle_plus.show()
+
+ # - Button
+ self.toggle_minus = gtk.ToggleButton("-")
+ self.toggle_label = self.toggle_minus.get_child()
+ self.toggle_label.modify_font(pango.FontDescription("sans %d" % style.zoom(12)))
+ self.toggle_minus.connect("toggled", self.toggle_minus_callback)
+ self._display.settings_table.attach(self.toggle_minus, 0, 1, 7, 8 )
+ self.toggle_minus.show()
+
+
+## # Buttons 9 .. 0
+## self.number_butts = []
+## for i in range(0,9+1):
+## self.toggle = gtk.ToggleButton(str(i))
+## self.toggle_label = self.toggle.get_child()
+## self.toggle_label.modify_font(pango.FontDescription("sans %d" % style.zoom(12)))
+## self.toggle.connect("toggled", self.toggle_number_callback, i)
+## self._display.settings_table.attach(self.toggle, 4, 5, 10-i, 11-i)
+## self.toggle.show()
+## self.number_butts.append(self.toggle)
+
+ def set_buttons(self, sett):
+ #print("#####exaddsub.set_buttons, sett=", sett)
+ for i in range(sett['min_pos']-1, sett['max_pos']):
+ #print("#####exaddsub.set_buttons, i=", i)
+ self.arrow_butts[i].set_active(True)
+
+ for i in range(sett['min_lines']-2,sett['max_lines']):
+ self.dice_butts[i].set_active(True)
+ #if (sett[i] == False):
+ #self.toggle_dice_butts[i].set_active(True)
+
+ if ( sett['+'] == True ):
+ self.toggle_plus.set_active(True)
+ else:
+ self.toggle_plus.set_active(False)
+
+ if ( sett['-'] == True ):
+ self.toggle_minus.set_active(True)
+ else:
+ self.toggle_minus.set_active(False)
+
+
+ #**** callbacks ********************************************************
+
+ def toggle_arrow_callback(self, widget, i):
+ #print("###toggle_arrow_callback :self._sett=", self._sett)
+ if widget.get_active():
+ if( i > self._sett['max_pos'] ):
+ self._sett['max_pos'] = i
+ self.set_buttons(self._sett)
+
+ else:
+ if( i == self._sett['max_pos'] ):
+ if( self._sett['min_pos'] == self._sett['max_pos'] ):
+ widget.set_active(True)
+ else:
+ self._sett['max_pos'] = i-1
+ self.set_buttons(self._sett)
+
+ else:
+ widget.set_active(True)
+
+ def toggle_dice_callback(self, widget, i):
+
+ if widget.get_active():
+ widget.set_active(True)
+
+ # callbacks updating the settings
+ def toggle_plus_callback(self, widget):
+ if widget.get_active():
+ self._sett['+'] = True
+ else:
+ if( self.toggle_minus.get_active() ):
+ self._sett['+'] = False
+ else:
+ widget.set_active(True)
+
+ def toggle_minus_callback(self, widget):
+ if widget.get_active():
+ self._sett['-'] = True
+ else:
+ if( self.toggle_plus.get_active() ):
+ self._sett['-'] = False
+ else:
+ widget.set_active(True)
+
+
+ ##### end of public methods ############################################
+ def _alladd(self, min_pos, max_pos, max_calcs):
+ """generate all calcs for +"""
+ _adds = []
+ _a = 9
+ _b = 1
+ _num_rand = 0
+
+ for i in range(min_pos, max_pos+1):
+ _num_rand = _num_rand+_a*_b
+ _b = 10*_b
+
+ for i in range (0,max_calcs+1):
+ _first_number = random.randint(0, _num_rand)
+ _second_number = random.randint(0, _num_rand)
+ _ergebnis = _first_number + _second_number
+ _c = [to_str_99(_first_number),'+',to_str_99(_second_number),'=',
+ to_str_99(_ergebnis)]
+ _c = flatten(_c)
+ _c = strip(_c, '#')
+ _adds.append(_c)
+
+ #print("in exaddsub._alladd adds= ", _adds)
+ return _adds
+
+ def _allsub(self, min_pos,max_pos,max_calcs):
+ """generate all calcs for -"""
+ _subs = []
+ _a = 9
+ _b = 1
+ _num_rand = 0
+
+ for i in range(min_pos, max_pos+1):
+ _num_rand = _num_rand+_a*_b
+ _b = 10*_b
+ for i in range (0,max_calcs+1):
+ _fist_number = random.randint(0, _num_rand)
+ _second_number = random.randint(0, _num_rand)
+ if _second_number > _fist_number :
+ _save = _fist_number
+ _fist_number = _second_number
+ _second_number = _save
+ _ergebnis = _first_number - _second_number
+ _c = [to_str_99(_first_number), '-', to_str_99(_second_number), '=', to_str_99(_ergebnis)]
+ _c = flatten(_c)
+ _c = strip(_c, '#')
+ _subs.append(_c)
+ return _subs
diff --git a/ReckonPrimer.activity/exercises/exercise.py b/ReckonPrimer.activity/exercises/exercise.py index cbb216a..1f5ea3a 100644 --- a/ReckonPrimer.activity/exercises/exercise.py +++ b/ReckonPrimer.activity/exercises/exercise.py @@ -56,14 +56,16 @@ class Exercise: return len(self._calcs) #===== methods of subclasses different for topic-specific settings - def format(self, calc): + def format(self, calc): #+ additional args """ Prepare the representation of a calculation for Display. This method is public for eventual use on calculations - coming through the net (TODO cooperative games, etc)""" + coming through the net (TODO cooperative games, etc) + ATTENTION: can have additional args in subclasses !!! + """ pass def _generate_calcs(self): - """ Generate calculations according to topic-specific settings. + """ Generate calcuelations according to topic-specific settings. Called on each call of update_setting""" # please have a look at self.get_calcs() diff --git a/ReckonPrimer.activity/exstore.py b/ReckonPrimer.activity/exstore.py index 8f38fa1..b631610 100644 --- a/ReckonPrimer.activity/exstore.py +++ b/ReckonPrimer.activity/exstore.py @@ -100,9 +100,9 @@ class ExStore: 'max' : 1, # maximum in size of a number in a calc # 0 <= min <= max <= 10 '+' : True, # make all additions min..max - '-' : True, # make all subtactions min..max + '-' : False, # make all subtactions min..max '_+_=_' : True, # = is _right_ from operator, e.g. 1+2=3 - 'input=' : [1,3,5],# list of positions in calc: 1 | 3 | 5 + 'input=' : [5],# list of positions in calc: 1 | 3 | 5 # where input is possible; # actual positions chosen by Generate. '_=_+_' : False, # = is _left_ from operator, e.g. 3=1+2 @@ -150,7 +150,33 @@ class ExStore: 'shuffle_inner': False, # shuffle only 1st (inner) iteration 'cut-max' : True # cut set of all calcs down to MAX }, []) - + _c3t4 = Task({'topic' : 'exaddsub', + 'title' : 'Template + in a column', + 'descript': 'TODO', + 'icon' : None, # for quick reference of the exercise + 'calclines': 1, # no. of lines for calc to be input. + 'MAX' : 50, # maximum of calcs generated. + # Generate fills up by varying input. + 'MIN' : 20, # minimum of calcs generated UNUSED + 'min' : 0, # minimum in size of a number in a calc + 'max' : 5, # maximum in size of a number in a calc + # 0 <= min <= max <= 10 + 'min_pos': 1, # min position of calcultion (always 1) + 'max_pos': 5, # maximum positions of calculation + 'min_lines': 2, # min lines (always 2) DO NOT CHANGE! + 'max_lines': 2, # maximum lines + 'max_calcs' : 4, # numbers of calculations + '+' : True, # make all additions min..max + '-' : False, # make all subtactions min..max + '_+_=_' : True, # = is _right_ from operator, e.g. 1+2=3 + 'input=' : [1,3,5],# list of positions in calc: 1 | 3 | 5 + # where input is possible; + # actual positions chosen by Generate. + '_=_+_' : False, # = is _left_ from operator, e.g. 3=1+2 + '=input' : [1,3,5],# analogous to '_+_=_' + 'shuffle': True, # shuffle _all_ the calcs + 'cut-max': True # cut set of all calcs down to MAX + }, []) self._root = \ Collection("root = invisible node","required by tree structure", None, [Collection("Praxis Volksschule", #_c1 @@ -192,7 +218,7 @@ Collection("root = invisible node","required by tree structure", None, "These are the (classes of) exercises all others are derived \ from. Thus, please, do not deletem them. \ For testing exercises disabling of @ is recommended.", None, - [_c3t1, _c3t2, _c3t3]) + [_c3t1, _c3t2, _c3t3, _c3t4]) ]) @@ -221,56 +247,3 @@ Collection("root = invisible node","required by tree structure", None, module = getattr(package, class_name.lower()) return getattr(module, class_name)(self._display, (obj.get_settings(), obj.get_eval())) - """ - #@# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv - - def define_coll_gui(self): - #Define gui-elements for presenting the collection. - #TODO: define once at startup of RP ? - #WN.LV diesen Code ersetzen: exstore_table.attach(self.colldata,... - self.topic_box = gtk.HBox(True, 0) - #self._display.exstore_table.attach(self.topic_box, 0, 6, 0, 1) - self._display.exstore_table.attach(self.topic_box, 0, 9, 5, 6) - - def set_coll_gui(self, coll_data): - #Set gui-elements according to Collection.data. - #WN.LV diesen Code ersetzen !!!!! - _i = 0 - for _t in ['addsub_simp','passten','times_div']: - self.button = gtk.Button() - self.image = gtk.Image() - - if(_t == 'addsub_simp'): - self.image.set_from_file("img/addsub_simp.jpg") - elif(_t == 'passten'): - self.image.set_from_file("img/passten.jpg") - elif(_t == 'times_div'): - self.image.set_from_file("img/times_div.jpg") - - self.button.set_image(self.image) - self.button.connect("clicked", self.select_exerc_callback, _i) - self.topic_box.pack_start(self.button) - self.button.show() - _i = _i + 1 - self.topic_box.show() - #@# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - - def select_exerc_callback(self, widget, coll_key): - #Callback telling the item from the collection selected. - #WN.LV diesen Code ersetzen> statt 1..n fuer Listen kommt coll_key - print('in Collection.select_exerc_callback') - if(self._active_exerc == None): - # at startup - self._active_exerc = coll_key - self._display.set_select_exerc_semaphore(coll_key) - #self._coach.notify(('exerc-selected', coll_key)) - elif(self._active_exerc == coll_key): - # hit the same button once more - pass - elif(self._active_exerc != coll_key): - # switched to another exercise - self._active_exerc = coll_key - self._display.switch_exercise() # TODO rename - self._display.set_select_exerc_semaphore(coll_key) - #self._coach.notify(('exerc-selected', coll_key)) - """ diff --git a/ReckonPrimer.activity/functions.py b/ReckonPrimer.activity/functions.py index 4743b16..0d8bfa5 100644 --- a/ReckonPrimer.activity/functions.py +++ b/ReckonPrimer.activity/functions.py @@ -1,364 +1,374 @@ -# -*- 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 line for input result and remainder with ':' and 'in'.
- res: , rem:
- returns:
- """
- print('in functions.make_input_remainder: len(strs)=', len(strs))
- print('in functions.make_input_remainder: strs=', strs)
- print('in functions.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
- print('in functions.make_input_remainder: len(_strs) == 14')
- _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
- print('in functions.make_input_remainder: len(_strs) == 15')
- _line1 = copy.deepcopy(_strs)
- _strs[10] = res[0]
- print('', _strs[10])
- _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']
-
-
-def ins_ord(ls, n):
- """Insert n into the ordered list ls. Beware of creating doubles. """
- pos = 0
- #print("in functions.ins_ord: ls,n=",ls,n)
- for i in ls:
- #print("in functions.ins_ord: i=",i)
- if n > i:
- pos = pos + 1
- #print("in functions.ins_ord: pos=",pos)
- ls.insert(pos, n)
- return ls
-
-
-#xx = ins_ord([], 2)
-#print("result=",xx)
-
-#xx = ins_ord([2], 8)
-#print("result=",xx)
-
-#xx = ins_ord([2,8], 4)
-#print("result=",xx)
-
-#xx = ins_ord([2,4,8], 1)
-#print("result=",xx)
-
-#xx = ins_ord([2,4,8], 9)
-#print("result=",xx)
-
-#xx = ins_ord([2,4,8], 4)
+# -*- coding: utf-8 -*- +"""the module for functions of reckonprimer.py. +laced 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 + TODO replace by make_chars(' ', n)""" + _l = [] + for i in range(0, n): + _l.append(' ') + return _l +#xxx = make_blanks(3) +#print(xxx) +#xxx[1]='+' +#print(xxx) + +def make_chars(char, n): + """create a list of n characters""" + _l = [] + for i in range(0, n): + _l.append(char) + return _l + + + +#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 line for input result and remainder with ':' and 'in'. + res: , rem: + returns: + """ + print('in functions.make_input_remainder: len(strs)=', len(strs)) + print('in functions.make_input_remainder: strs=', strs) + print('in functions.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 + print('in functions.make_input_remainder: len(_strs) == 14') + _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 + print('in functions.make_input_remainder: len(_strs) == 15') + _line1 = copy.deepcopy(_strs) + _strs[10] = res[0] + print('', _strs[10]) + _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'] + + +def ins_ord(ls, n): + """Insert n into the ordered list ls. Beware of creating doubles. """ + pos = 0 + #print("in functions.ins_ord: ls,n=",ls,n) + for i in ls: + #print("in functions.ins_ord: i=",i) + if n > i: + pos = pos + 1 + #print("in functions.ins_ord: pos=",pos) + ls.insert(pos, n) + return ls + + +#xx = ins_ord([], 2) +#print("result=",xx) + +#xx = ins_ord([2], 8) +#print("result=",xx) + +#xx = ins_ord([2,8], 4) +#print("result=",xx) + +#xx = ins_ord([2,4,8], 1) +#print("result=",xx) + +#xx = ins_ord([2,4,8], 9) +#print("result=",xx) + +#xx = ins_ord([2,4,8], 4) #print("result=",xx) @@ -368,3 +378,22 @@ def ins_ord(ls, n): # print('xxx > 2') #else: # print('xxx = 2') + +def strs2str(chars): + _l = '' + for _i in range(len(chars)): + _l = _l + chars[_i] + return _l + +#print("strs2str(['6','5','+','4','3','=','_','_','8'] = ", +# strs2str(['6','5','+','4','3','=','_','_','8'])) +#'65+43=__8' + +def max_len(calc): + """maximal length of sublists of a list""" + max = 1 + for e in calc: + if len(e) > max: + max = len(e) + return max + diff --git a/ReckonPrimer.activity/learner.py b/ReckonPrimer.activity/learner.py index d49ca8b..25cffb1 100644 --- a/ReckonPrimer.activity/learner.py +++ b/ReckonPrimer.activity/learner.py @@ -17,7 +17,12 @@ class Learner: self._ex = exercise #print('start calcs: ') #+str(self._ex.get_setting()[min])) _calc = self._ex.get_next_calc() + print('start calcs: _calc = ', _calc) _lines, self._input = exercise.format(_calc) + print('start calcs: _lines = ', _lines) + print('start calcs: --------------------------------') + print('start calcs: self._input = ', self._input) + print('start calcs: ================================') self._display.display_calc(_lines) self._curr_in = self._input.pop() self._display.create_entryline(self._curr_in) @@ -32,7 +37,7 @@ class Learner: self._display.show_progress() try: # get_next_calc _calc = self._ex.get_next_calc() - print('in learner.notify: calc=', _calc) + #print('in learner.notify: calc=', _calc) _lines, self._input = self._ex.format(_calc) self._display.display_calc(_lines) self._curr_in = self._input.pop() #need _curr_in in notify |