从一组字符串中找到整数

时间:2019-04-14 04:18:43

标签: python-3.x puzzle

嗨,我想解决以下问题:- 如果I / P为“ OZONETOWER”,则O / P为012,即将0的字符串(ZERO)与输入的字符串进行比较,找到该字符串时,它会出现在输出中,依此类推,对于1和2。 提供一组输入和输出以供参考:-

I/P:                               O/P:
WEIGHFOXTOURIST                    2468
OURNEONFOE                         114
ETHER                               3

我已经尝试过了,但这似乎并不能提供所有情况的结果。

def puzzle(dic_number,string,key):
    dic_values=0    
    length=len(dic_number)
    for i in dic_number:
        if i in string:
            dic_values+=1
    if dic_values ==length:
        print(key)


dic1={0:"ZERO",1:"ONE",2:"TWO",3:"THREE",4:"FOUR",5:"FIVE",6:"SIX",7:"SEVEN",8:"EIGHT",9:"NINE"}
string=input("Enter number")

for i,j in enumerate(dic1.values()):
    puzzle(j,string,i)

2 个答案:

答案 0 :(得分:0)

这是实现此目的的一种方法:

numbers = {
    0: "ZERO",
    1: "ONE",
    2: "TWO",
    3: "THREE",
    4: "FOUR",
    5: "FIVE",
    6: "SIX",
    7: "SEVEN",
    8: "EIGHT",
    9: "NINE",
}


def puzzle(s):
    supper = s.upper()
    ret = []
    for n, chrs in numbers.items():
        if all(c in supper for c in chrs):
            ret.append(n)
    return ret


s = input("enter string ")

numbers_found = puzzle(s)
print('numbers found', numbers_found)

输出:

enter string WEIGHFOXTOURIST
numbers found [2, 3, 4, 6, 8]
enter string OURNEONFOE
numbers found [1, 4]
enter string ETHER
numbers found [3]
enter string OZONETOWER
numbers found [0, 1, 2]

注意

通过此实现

  • 对于输入3,您还会得到WEIGHFOXTOURIST的附加结果
  • 对于输入1, 4,您得到的是1, 1, 4而不是OURNEONFOE

编辑

要获得重复的匹配,拼图功能必须“消耗”已经使用的字符:

def puzzle(s):
    supper = s.upper()
    ret = []
    for n, chrs in numbers.items():
        while True:
            if all(c in supper for c in chrs):
                for c in chrs:
                    supper = supper.replace(c, '', 1)
                ret.append(n)
            else:
                break
    return ret

输出:

enter string WEIGHFOXTOURIST
numbers found [2, 3, 6]
enter string OURNEONFOE
numbers found [1, 1, 4]
enter string ETHER
numbers found [3]
enter string OZONETOWER
numbers found [0, 1, 2]

注意

通过此实现

  • 对于输入8,您没有得到WEIGHFOXTOURIST的结果

说明:

在这个难题中,如果在输入字符串中找到数字的所有字母,则将其视为“匹配”。

为此,我们通过在字符串的字符上创建一个list comprehension来逐个检查每个字符:

>>> chrs = 'FOUR'
>>> [c for c in chrs]
['F', 'O', 'U', 'R']

对于每个字符,我们使用in来检查是否在大写输入字符串中找到它:

>>> chrs = 'FOUR'
>>> supper = 'OURNEONFOE'
>>> [c in supper for c in chrs]
[True, True, True, True]

如果找不到字符,它将在相应位置产生False

>>> chrs = 'FOUR'
>>> supper = 'OXRNEONFOE'
>>> [c in supper for c in chrs]
[True, True, False, True]

然后用all检查列表中的所有项目是否都是True

>>> chrs = 'FOUR'
>>> supper = 'OURNEONFOE'
>>> all([c in supper for c in chrs])
True
>>> supper = 'OXRNEONFOE'
>>> all([c in supper for c in chrs])
False

PEP 289起,我们可以直接在all中使用生成器表达式

>>> chrs = 'FOUR'
>>> supper = 'OURNEONFOE'
>>> all(c in supper for c in chrs)
True

答案 1 :(得分:0)

def puzzle(s):
    supper = s.upper()
    ret = []
    for n, chrs in numbers.items():
        while True:
            if all(c in supper for c in chrs):
                for c in chrs:
                    supper = supper.replace(c, '', 1)
                ret.append(n)
            else:
                break
    return ret
相关问题