[Codewars] 085: Find the unknown digit

题目

To give credit where credit is due: This problem was taken from the ACMICPC-Northwest Regional Programming Contest. Thank you problem writers.

You are helping an archaeologist decipher some runes. He knows that this ancient society used a Base 10 system, and that they never start a number with a leading zero. He's figured out most of the digits as well as a few operators, but he needs your help to figure out the rest.

The professor will give you a simple math expression, of the form

[number][op][number]=[number]

He has converted all of the runes he knows into digits. The only operators he knows are addition (+),subtraction(-), and multiplication (*), so those are the only ones that will appear. Each number will be in the range from -1000000 to 1000000, and will consist of only the digits 0-9, possibly a leading -, and maybe a few ?s. If there are ?s in an expression, they represent a digit rune that the professor doesn't know (never an operator, and never a leading -). All of the ?s in an expression will represent the same digit (0-9), and it won't be one of the other given digits in the expression. No number will begin with a 0 unless the number itself is 0, therefore 00 would not be a valid number.

Given an expression, figure out the value of the rune represented by the question mark. If more than one digit works, give the lowest one. If no digit works, well, that's bad news for the professor - it means that he's got some of his runes wrong. output -1 in that case.

Complete the method to solve the expression to find the value of the unknown rune. The method takes a string as a paramater repressenting the expression and will return an int value representing the unknown rune or -1 if no such rune exists.

我的答案

import re
def solve_runes(runes):
    nums = list(set(re.findall('\d', runes)))
    maybe = sorted([i for i in ['0','1','2','3','4','5','6','7','8','9'] if i not in nums])
    result = []
    if '+' in runes:
        for each in maybe:
            temp = runes.replace('?', each)
            digits = re.split(r'[\+\=]', temp)
            if int(digits[0]) + int(digits[1]) == int(digits[2]):
                if re.match(r'^0\d+$',digits[0]) or re.match(r'^0\d+$',digits[1]) or re.match(r'^0\d+$',digits[2]):
                    pass
                else:
                    result.append(int(each))
                    break
        if result:
            return result[0]
        else:
            return -1
    elif '*' in runes:
        for each in maybe:
            temp = runes.replace('?', each)
            digits = re.split(r'[\*\=]', temp)
            if int(digits[0]) * int(digits[1]) == int(digits[2]):
                if re.match(r'^0\d+$',digits[0]) or re.match(r'^0\d+$',digits[1]) or re.match(r'^0\d+$',digits[2]):
                    pass
                else:
                    result.append(int(each))
                    break
        if result:
            return result[0]
        else:
            return -1
    else:
        for each in maybe:
            temp = runes.replace('?', each)
            digit = re.split(r'(?<=\d)-', temp)
            digits = re.split(r'\=', digit[1])
            if int(digit[0]) - int(digits[0]) == int(digits[1]):
                if re.match(r'^0\d+$',digit[0]) or re.match(r'^0\d+$',digits[0]) or re.match(r'^0\d+$',digits[1]):
                    pass
                else:
                    result.append(int(each))
                    break
        if result:
            return result[0]
        else:
            return -1

其他精彩答案

import re

def solve_runes(runes):
    for d in sorted(set("0123456789") - set(runes)):
        toTest = runes.replace("?",d)
        if re.search(r'([^\d]|\b)0\d+', toTest): continue
        l,r = toTest.split("=")
        if eval(l) == eval(r): return int(d)
    return -1
def solve_runes(runes):
    for c in sorted(set('0123456789') - set(runes)):        
        s = runes.replace('?', c).replace('-', ' - ').replace('+', ' + ').replace('*', ' * ').replace('=', ' == ')
        if not any(e[0] == '0' and e != '0' for e in s.split()) and eval(s): return int(c)
    return -1
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容