Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

python - 模式计算器Python(Pattern Calculator Python)

I had a friend of mine show me an interesting problem he had on a coding challange at an interview.

(我有一个朋友向我展示了一个有趣的问题,他在一次采访中遇到了编码挑战。)

So you have a number like n = 3413289830 and a pattern like p = a-bcdefghij, you need to create a function that takes this input and outputs -413289827.

(因此,您有一个像n = 3413289830的数字和一个像p = a-bcdefghij的模式,您需要创建一个接受此输入并输出-413289827的函数。)

Obviously it should work for any combination of numbers and letters for addition and subtraction.

(显然,它应该适用于数字和字母的任何组合以进行加减。)

I worked out this code but I am pretty sure it can be improved as I think it's a bit inefficient.

(我编写了这段代码,但是我很确定可以改进它,因为我认为它效率不高。)

pattern = 'a-bcdefghij'       
n = '3413289830'
lst = []
def splitnb(n, pattern):
    save = dict()
    if(len(n) != len(pattern) - 1):
        print('Pattern needs to match number')
    else:
        if( '-' in pattern):
            patlst = pattern.split('-')
        elif('+' in pattern):
            patlst = pattern.split('+')
        for char in n:
            a = list(n)
        for pat in patlst:
            first  = patlst[0].split()
            rest = pat.split()
        for l in first[0]:
            f1 = l
            lst.append(f1)
        for l2 in rest[0]:
            f2 = l2
            lst.append(f2)
        save = dict(zip(lst, a))
        nb = ''
        if( '-' in pattern):
            for i in first[0]:
                numb = int(save.get(i))
            for j in rest[0]:
                nb += save.get(j)
                numb1 = numb - int(nb)
        elif('+' in pattern):
            for i in first[0]:
                numb = int(save.get(i))
            for j in rest[0]:
                nb += save.get(j)
                numb1 = numb + int(nb)
    return numb1

f1 = splitnb(n, pattern)
f2 = splitnb('1234', 'ab+cd')
f3 = splitnb('22', 'a-b')
  ask by thouxanstx translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One way to do this is to take the pattern and replace each char with the number that should be there and then eval the result

(一种方法是采用模式并将每个字符替换为应该存在的数字,然后eval结果)

string.ascii_letters is a string of all ascii characters in alphabetical order starting with lowercase.

(string.ascii_letters是所有ASCII字符的字符串,按字母顺序string.ascii_letters写开始。)

This can be used to convert the char into the index of the digit that should be extracted from n

(这可用于将char转换为应从n提取的数字的索引)

>>> [n[string.ascii_letters.index(x)] if x in string.ascii_letters else x for x in pattern]
['3', '-', '4', '1', '3', '2', '8', '9', '8', '3', '0']

We add if x in string.ascii_letters else x so that the operators are not converted.

(我们if x in string.ascii_letters else x添加if x in string.ascii_letters else x以便不转换运算符。)

Then you join the resulting list to get the string

(然后,您加入结果列表以获取字符串)

>>> ''.join(n[string.ascii_letters.index(x)] if x in string.ascii_letters else x for x in pattern)
'3-413289830'

Removing the brackets turns the list comprehension into a generator which should be slightly more performant.

(删除括号将列表理解变成一个生成器,它应该会稍微好一些。)

You can then use eval to run this string as if it was python code

(然后,您可以使用eval来运行此字符串,就好像它是python代码一样)

>>> eval(''.join(n[string.ascii_letters.index(x)] if x in string.ascii_letters else x for x in pattern))
-413289827

You should only use eval if you trust the input that you are being given as it can execute arbitrary code

(仅当您信任输入信息时,才应使用eval,因为它可以执行任意代码)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...