Python练习题答案: 简单的条码扫描器【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战

简单的条码扫描器【难度:2级】:

答案1:

DECODED_DIGITS = {'101': 'S', '01010': 'M', '0010011': '2', '0001101': '0', '0110001': '5', '0111011': '7', '0011001': '1', '0101111': '6', '0100011': '4', '0111101': '3', '0001011': '9', '0110111': '8'}

def barcode_scanner(barcode):
    N = len(barcode)//2
    digits = barcode[3:N-2] + barcode[N+3:2*N-2].translate(str.maketrans("01","10"))
    return "".join(DECODED_DIGITS[digits[i:i+7]] for i in range(0, len(digits), 7))

答案2:

import re

L_DIGITS = {
"0001101": "0",
"0011001": "1",
"0010011": "2",
"0111101": "3",
"0100011": "4",
"0110001": "5",  
"0101111": "6",
"0111011": "7",
"0110111": "8",
"0001011": "9"}

def barcode_scanner(barcode):
    leftized = barcode[3:45] + barcode[50:-3].translate(str.maketrans("01","10"))
    return re.sub('|'.join(L_DIGITS.keys()), lambda m: L_DIGITS[m.group(0)], leftized)

答案3:

d = {n // 10 * 64 ^ ord(c): str(n % 10) for n, c in enumerate('rflB\\NPDHtMYS}cqo{wK')}
barcode_scanner = lambda s: ''.join(d[int(''.join(b),2)] for b in zip(*[iter(s[3:45] + s[-45:-3])]*7))

答案4:

TO_UPC = {'0011001': '1', '1000010': '3', '0111101': '3', '1110010': '0',
          '0001101': '0', '0100011': '4', '1000100': '7', '0101111': '6',
          '1001000': '8', '0111011': '7', '1011100': '4', '0001011': '9',
          '1101100': '2', '1001110': '5', '0110001': '5', '0010011': '2',
          '1110100': '9', '1100110': '1', '0110111': '8', '1010000': '6'}


def barcode_scanner(barcode):
    l_r = barcode[3:45] + barcode[50:-3]
    return ''.join(TO_UPC[l_r[a:a + 7]] for a in xrange(0, 84, 7))

答案5:

from itertools import groupby

def barcode_scanner(barcode):
    DIGITS = {'S': '1B1W1B', 'E': '1B1W1B', 'M': '1W1B1W1B1W', '0': '3W2B1W1B', '1': '2W2B2W1B', '2': '2W1B2W2B',
              '3': '1W4B1W1B', '4': '1W1B3W2B', '5': '1W2B3W1B', '6': '1W1B1W4B', '7': '1W3B1W2B', '8': '1W2B1W3B', '9': '3W1B1W2B'}
    s = barcode[3:45] + barcode[50:-3]
    res = ''
    for i in range(0, len(s), 7):
        t = 'WB' if i < 42 else 'BW'
        for k, v in DIGITS.items():
            if ''.join(str(len(list(g))) + c.translate(str.maketrans('01', t)) for c, g in groupby(s[i:i + 7])) == v:
                res += k
    return res​

答案6:

from itertools import groupby

def barcode_scanner(barcode):
    s = barcode[3:45] + barcode[50:-3]
    res = ''
    for i in range(0, len(s), 7):
        t = 'WB' if i < 42 else 'BW'
        for k, v in DIGITS.items():
            if ''.join(str(len(list(g))) + c.translate(str.maketrans('01', t)) for c, g in groupby(s[i:i + 7])) == v:
                res += k
    return res​

答案7:

from itertools import groupby

def barcode_scanner(barcode):
    left = split_length(barcode[3: 45], 7)
    right = split_length(barcode[50: -3], 7)
    
    
    l_str = ''.join([str_to_num(bits_to_str(bits), True) for bits in left])
    r_str = ''.join([str_to_num(bits_to_str(bits), False) for bits in right])
    
    return l_str + r_str

def bits_to_str(bits): 
    m = [''.join(g) for _, g in groupby(bits)]
    s = ['{}{}'.format(len(b), 'W' if b[0] == '1' else 'B') for b in m]
    return ''.join(s)

def str_to_num(string, left): 
    if left: 
        string = string.replace('W', 'w').replace('B', 'W').replace('w', 'B')
    for n in DIGITS: 
        if DIGITS[n] == string: 
            return n

def split_length(string, length): 
    return [string[i: i + length] for i in range(0, len(string), length)]

答案8:

DICT = {'1101100':'2', '1000010':'3', '1011100':'4', '0110001':'5', '0101111':'6', 
        '0111011':'7', '0110111':'8', '0001011':'9', '0011001':'1', '1110010': '0'}
 
def barcode_scanner(barcode):
    barcode = barcode[3:-3]
    separate, R_sep = [], []

    for x in range(6): 
        separate.append(barcode[x*7: x*7 + 7])
        R_sep.append(barcode[-43:][1+x*7: x*7 +8])
    
    decode = ''
    for i in separate:
        if not i in DICT: decode += DICT[i.replace('1', '2').replace('0', '1').replace('2', '0')]
        else: decode += DICT[i]
        
    R_deco = ''
    for i in R_sep:
        if not i in DICT: R_deco += DICT[i.replace('1', '2').replace('0', '1').replace('2', '0')]
        else: R_deco += DICT[i]
    
    return decode + R_deco ​

答案9:

def barcode_scanner(barcode):
    parts = [barcode[3:-3][:42], barcode[3:-3][47:]]
    new_left = []
    r_list = []
    new_r = ''
    new_parts = ''
    nums = ''
    result = []
    for i in range((len(parts[0])//6)-1):
        new_left.append(parts[0][7*i:7*i+7])
        r_list.append((parts[1][7*i:7*i+7]))
    for n in r_list:
        for char in n:
            if char == '0':
                new_r += '1'
            else:
                new_r += '0'
        new_r += ' '
    new_right = new_r.strip().split(' ')
    for a in new_left+new_right:
        for b in range(len(a)):
            if a[b] == '0':
                new_parts += 'W'
            else:
                new_parts += 'B'
        new_parts += ' '
    for c in new_parts.strip().split(' '):
        counter = 1
        for d in range(len(c)-1):
            if c[d] != c[d+1]:
                nums += '{}{}'.format(counter, c[d])
                counter = 1
            else:
                counter += 1
        nums += '{}{}'.format(counter, c[d+1])
        nums += ' '
    for e in nums.strip().split(' '):
        for f in DIGITS:
            if e in DIGITS[f]:
                result.append(str(f))
    return ''.join(result)

答案10:

import re
nums_r = ["1110010","1100110","1101100","1000010","1011100","1001110","1010000","1000100","1001000","1110100"]
nums_l = [''.join('1' if x == '0' else '0' for x in i) for i in nums_r]
def barcode_scanner(barcode):
    return "".join([str(nums_l.index(i)) for i in re.findall(r".{7}",barcode[3:45])] + [str(nums_r.index(i)) for i in re.findall(r".{7}",barcode[50:-3])])



Python基础训练营景越Python基础训练营QQ群

Python练习题答案: 简单的条码扫描器【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战_第1张图片
欢迎各位同学加群讨论,一起学习,共同成长!

你可能感兴趣的:(python面试题库和答案,python编程练习,算法,python面试题库和答案)