标题:关于“字母算术”的问题,求解释
取消只看楼主
SocketHack
Rank: 2
等 级:论坛游民
帖 子:14
专家分:30
注 册:2010-8-7
结帖率:50%
已结贴  问题点数:70 回复次数:0 
关于“字母算术”的问题,求解释
import re
import itertools

def solve(puzzle):
    words = re.findall('[A-Z]+',puzzle.upper())
    unique_characters = set(''.join(words))
    assert len(unique_characters) <= 10,'Too many letters'
    first_letters = {word[0] for word in words}
    n = len(first_letters)
    sorted_characters = ''.join (first_letters) +\
                        ''.join (unique_characters - first_letters)
    characters = tuple(ord(c) for c in sorted_characters)
    digits = tuple(ord(c) for c in '0123456789')
   
    zero = digits[0]
    for guess in itertools.permutations(digits,len(characters)):
         if zero not in guess[:n]:
            equation =puzzle.translate(dict(zip(characters,guess)))
            if eval(equation):
                return equation

if __name__=='__main__':
   import sys
   for puzzle in sys.argv[1:]:
       print(puzzle)
       solution = solve(puzzle)
     if solution:
         print(solution) 

求红色字体的代码用意?为啥要这么写这个条件?
谢谢...



搜索更多相关主题的帖子: import letters 字母 word 
2012-08-08 23:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-374957-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.519270 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved