Python:如何翻译?

时间:2018-04-29 15:15:27

标签: python

程序是在用户输入时#8; 8#15#23 ### 23#1#19 ### 9#20"

输出应该是"它是如何"

然而,它无法显示空间(###)。

在这里输入代码

ABSTRACT ={"A":"1","B":"2","C":"3","D":"4","E":"5","F":"6","G":"7","H":"8","I":"9", "J":"10","K":"11","L":"12","M":"13","N":"14","O":"15","P":"16","Q":"17","R":"18","S":"19","T":"20","U":"21","V":"22","W":"23", "X":"24","Y":"25","Z":"26",
"   ":"###","":"#" }
ABSTRACT_SHIFTED = {value:key for key,value in ABSTRACT.items()}

def from_abstract(s):
    result = ''
    for word in s.split('*'):
        result = result +ABSTRACT_SHIFTED.get(word)

    return result

5 个答案:

答案 0 :(得分:1)

这样可以解决问题:

#!/usr/bin/env python

InputString = "8#15#23###23#1#19###9#20"

InputString = InputString.replace("###", "##")
InputString = InputString.split("#")

DecodedMessage = ""
for NumericRepresentation in InputString:
    if NumericRepresentation == "":
        NumericRepresentation = " "
        DecodedMessage += NumericRepresentation
        continue
    else:
        DecodedMessage += chr(int(NumericRepresentation) + 64)

print(DecodedMessage)

打印:

HOW WAS IT

答案 1 :(得分:0)

你也可以使用正则表达式

import re

replacer ={"A":"1","B":"2","C":"3","D":"4","E":"5","F":"6","G":"7","H":"8","I":"9", "J":"10","K":"11","L":"12","M":"13","N":"14","O":"15","P":"16","Q":"17","R":"18","S":"19","T":"20","U":"21","V":"22","W":"23", "X":"24","Y":"25","Z":"26",
"   ":"###","":"#" }
reversed = {value:key for key,value in replacer.items()}

# Reversed because regex is greedy and it will match 1 before 15
target = '8#15#23###23#1#19###9#20'
pattern = '|'.join(map(lambda x: x + '+', list(reversed.keys())[::-1]))
repl = lambda x: reversed[x.group(0)]

print(re.sub(pattern, string=target, repl=repl))

并打印:

HOW   WAS   IT

答案 2 :(得分:0)

只需对代码进行一些最小的更改即可。 1)拆分'#',而不是'*' 2)如果未找到匹配,则默认检索'' 3)使用'##'代替'###'

//var input = Console.ReadLine();

while (true)
{
    var input = Console.ReadLine();  // put it here

    switch (input)
    {

答案 3 :(得分:0)

交换ABSTRACT的键值对,并在输入上使用简单的split + join

ip = "8#15#23###23#1#19###9#20"
ABSTRACT = dict((v,k) for k,v in ABSTRACT.items())
''.join(ABSTRACT.get(i,' ') for i in ip.split('#')).replace('  ', ' ')
#'HOW WAS IT'

答案 4 :(得分:0)

这里面临的最大挑战是"#"用作标记分隔符和空格字符,你必须知道上下文,告诉你在任何给定时间你得到了什么,这使得简单地拆分字符串变得困难。所以写一个简单的解析器。这个将接受任何东西作为令牌中的第一个角色然后抓住所有东西,直到它看到下一个"#"。

ABSTRACT ={"A":"1","B":"2","C":"3","D":"4","E":"5","F":"6","G":"7","H":"8","I":"9", "J":"10","K":"11","L":"12","M":"13","N":"14","O":"15","P":"16","Q":"17","R":"18","S":"19","T":"20","U":"21","V":"22","W":"23", "X":"24","Y":"25","Z":"26",
"   ":"###","":"#" }
ABSTRACT_SHIFTED = {value:key for key,value in ABSTRACT.items()}

user_input = "8#15#23###23#1#19###9#20"

def from_abstract(s):
    result = []
    while s:
        print 'try', s
        # tokens are terminated with #
        idx = s.find("#")
        # ...except at end of line
        if idx == -1:
            idx = len(s) - 1
        token = s[:idx]
        s = s[idx+1:]
    result.append(ABSTRACT_SHIFTED.get(token, ' '))
    return ''.join(result)

print from_abstract(user_input)