Re: code128.py
Klaas Feenstra <[email protected]>
| Newsgroups | gmane.comp.python.reportlab.user |
|---|---|
| Message-ID | <CAJcivacMqKa828Z3YhYSEsz+ucf_EwxWu1bRro7FtTZyk3Tetw@mail.gmail.com> |
Hi Robin, I have checked your test. I think we are almost there, but 0: START_B b b b b 1 2 3 4 5 6 b b b STOP(15) --> START_B b b b b TO_C 12 34 56 TO_B b b b STOP(14) 1: START_B b b b b 1 2 3 4 5 b b b STOP(14) --> START_B b b b b 1 2 3 4 5 b b b STOP(14) 2: START_B b b b b 1 2 3 4 b b b STOP(13) --> START_B b b b b 1 2 3 4 b b b STOP(13) 3: START_B b b b b 1 2 3 b b b STOP(12) --> START_B b b b b 1 2 3 b b b STOP(12) 4: START_B b b b b 1 2 b b b STOP(11) --> START_B b b b b 1 2 b b b STOP(11) 5: START_B b b b b 1 b b b STOP(10) --> START_B b b b b 1 b b b STOP(10) 6: START_B b b b b 1 2 3 4 5 6 TO_A a a STOP(15) --> START_B b b b b TO_C 12 34 56 TO_A a a STOP(13) 7: START_B b b b b 1 2 3 4 TO_A a a STOP(13) --> START_B b b b b TO_C 12 34 TO_A a a STOP(12) 8: START_B b b b b 1 2 3 TO_A a a STOP(12) --> START_B b b b b 1 2 3 TO_A a a STOP(12) 9: START_B b b b b 1 2 TO_A a a STOP(11) --> START_B b b b b 1 2 TO_A a a STOP(11) 10: START_B b b b b 1 TO_A a a STOP(10) --> START_B b b b b 1 TO_A a a STOP(10) 11: START_B b b b b 1 2 3 4 5 6 STOP(12) --> START_B b b b b TO_C 12 34 56 STOP(10) The first 12 tests I am agree. 12: START_B b b b b 1 2 3 4 5 STOP(11) --> START_B b b b b 1 2 3 4 5 STOP(11) Here we could make a win if we can convert to: START_B b b b b 1 TO_C 23 45 STOP(10) 13: START_B b b b b 1 2 3 4 STOP(10) --> START_B b b b b TO_C 12 34 STOP(9) 14: START_B b b b b 1 2 3 STOP(9) --> START_B b b b b 1 2 3 STOP(9) 15: START_B b b b b 1 2 STOP(8) --> START_B b b b b 1 2 STOP(8) 16: START_B b b b b 1 STOP(7) --> START_B b b b b 1 STOP(7) 17: START_B ± 1 2 3 4 b STOP(8) --> START_C ± 12 34 TO_B b STOP(7) 18: START_B b TO_A a TO_B ± 1 2 3 4 b STOP(12) --> START_B b TO_A a TO_C ± 12 34 TO_B b STOP(11) 19: START_B b TO_A a TO_B 1 2 STOP(8) --> START_B b TO_A a TO_C 12 STOP(7) 20: START_B b TO_A a TO_B 1 2 3 b STOP(10) --> START_B b TO_A a TO_B 1 2 3 b STOP(10) 21: START_B b TO_A a TO_B 1 2 3 4 b STOP(11) --> START_B b TO_A a TO_C 12 34 TO_B b STOP(10) So if we could solve testcase 12, I think we have found the solution for all cases. Kind regards, Klaas On Tue, Jan 5, 2016 at 3:06 PM, Robin Becker <[email protected]> wrote: > Hi Klaas, > > for some reason I assumed we always start in b, but perhaps I'm being > stupid since as you suggest we can immediately switch to C. > > I think you are suggesting that in the special case that > > l[startpos-1]=='START_X' > > we know that START_X TO_C will be reduced so we can add one to the saving. > > I believe a sequence like this > > 'START_B b TO_A a TO_B xf1 1 2 3 4 b STOP' (12) > > can occur and it is not improved by the current scheme > > effectively we seem to ignore the start character entirely. > > This should be reduced to > > 'START_B b TO_A a TO_C xf1 12 34 TO_B b STOP' (11) > > I propose one more complication to try and capture this; we add to savings > if we start the C sequence right after a start and then combine the start > and TO_C if that condition works. > > > my new test program looks like this > > ####################################################### > from string import digits > starts = ['START_B','TO_A','TO_B'] > def _trailingDigitsToC(self, l): > # Optimization: trailing digits -> set C double-digits > > i = 0 > nl = [] > while i < len(l): > startpos = i > rl = [] > savings = -1 # the TO_C costs one character > while i < len(l): > if l[i] in starts: > j = i > break > elif l[i] == '\xf1': > rl.append(l[i]) > i += 1 > continue > elif len(l[i]) == 1 and l[i] in digits \ > and len(l[i+1]) == 1 and l[i+1] in digits: > rl.append(l[i] + l[i+1]) > i += 2 > savings += 1 > continue > else: > break > > ta = not (l[i]=='STOP' or j==i) > xs = savings>=0 and (startpos and nl[-1] in starts) > if savings+int(xs) > int(ta): > if xs: > toc = nl[-1][:-1]+'C' > del nl[-1] > else: > toc = 'TO_C' > nl += [toc]+rl > if ta: > nl.append('TO'+l[j][-2:]) > nl.append(l[i]) > else: > nl += l[startpos:i+1] > i += 1 > return nl > > for x,l in enumerate(( > 'START_B b b b b 1 2 3 4 5 6 b b b STOP', > 'START_B b b b b 1 2 3 4 5 b b b STOP', > 'START_B b b b b 1 2 3 4 b b b STOP', > 'START_B b b b b 1 2 3 b b b STOP', > 'START_B b b b b 1 2 b b b STOP', > 'START_B b b b b 1 b b b STOP', > 'START_B b b b b 1 2 3 4 5 6 TO_A a a STOP', > 'START_B b b b b 1 2 3 4 TO_A a a STOP', > 'START_B b b b b 1 2 3 TO_A a a STOP', > 'START_B b b b b 1 2 TO_A a a STOP', > 'START_B b b b b 1 TO_A a a STOP', > 'START_B b b b b 1 2 3 4 5 6 STOP', > 'START_B b b b b 1 2 3 4 5 STOP', > 'START_B b b b b 1 2 3 4 STOP', > 'START_B b b b b 1 2 3 STOP', > 'START_B b b b b 1 2 STOP', > 'START_B b b b b 1 STOP', > 'START_B \xf1 1 2 3 4 b STOP', > 'START_B b TO_A a TO_B \xf1 1 2 3 4 b STOP', > 'START_B b TO_A a TO_B 1 2 STOP', > 'START_B b TO_A a TO_B 1 2 3 b STOP', > 'START_B b TO_A a TO_B 1 2 3 4 b STOP', > )): > l = l.split() > lo = _trailingDigitsToC(None, l) > print '%2d: %s(%d) --> %s(%d)' % (x,' '.join(l),len(l),' > '.join(lo),len(lo)) > ####################################################### > > > On 05/01/2016 00:13, Klaas Feenstra wrote: > >> Robin, I made a little modification and I think this is now working >> correctly. >> >> def _trailingDigitsToC(self, l): >> # Optimization: trailing digits -> set C double-digits >> >> i = 0 >> nl = [] >> while i < len(l): >> startpos = i >> rl = [] >> savings = -1 # the TO_C costs one character >> while i < len(l): >> if l[i] in (['START_B','TO_A','TO_B']): >> j = i >> break >> elif l[i] == '\xf1': >> rl.append(l[i]) >> i += 1 >> continue >> elif len(l[i]) == 1 and l[i] in digits \ >> and len(l[i+1]) == 1 and l[i+1] in digits: >> rl.append(l[i] + l[i+1]) >> i += 2 >> savings += 1 >> continue >> else: >> break >> >> ta = not (l[i]=='STOP' or j==i) >> if savings > int(ta) or savings > (startpos-1): #In case >> of >> starting with C >> nl += ['TO_C']+rl >> if ta: >> nl.append('TO'+l[j][-2:]) >> nl.append(l[i]) >> else: >> nl += l[startpos:i+1] >> i += 1 >> return nl >> >> On Tue, Jan 5, 2016 at 12:21 AM, Klaas Feenstra <[email protected]> >> wrote: >> >> Hi Robin, >>> >>> Yes, your code is working better, but is also not correct. Try the next: >>> 'START_B xf1 1 2 3 4 b STOP' (8) >>> should be >>> 'START_B TO_C xf1 12 34 TO_B b STOP' (8), but in the function encode >>> >>> # Finally, replace START_X,TO_Y with START_Y >>> if l[1] in tos: >>> l[:2] = ['START_' + l[1][-1]] >>> >>> this will be transformed to >>> 'START_C xf1 12 34 TO_B b STOP' (7) >>> >> ......... > > -- > Robin Becker >