Eastsun 写道
另外,按LZ"通过查找字典把输入的数字变成可能的字母输出"的意思
我的上面代码得到的结果字符串中不会包含数字,也就是某些号码会无解
但我看了下原题,貌似是可以包含数字的,譬如号码"1-800-PICK-UPS "应该可以以"1800-PICK-UPS "形式作为一个解.
我写了个容许数字出现的代码,不过稍微复杂些.
贴一个python版本的,惭愧,弄了一晚上
#-*- coding: utf-8 -*-
#!/usr/bin/env python
from __future__ import with_statement
button_mapping = {
'A' : 2, 'B' : 2, 'C' : 2,
'D' : 3, 'E' : 3, 'F' : 3,
'G' : 4, 'H' : 4, 'I' : 4,
'J' : 5, 'K' : 5, 'L' : 5,
'M' : 6, 'N' : 6, 'O' : 6,
'P' : 7, 'Q' : 7, 'R' : 7, 'S' : 7,
'T' : 8, 'U' : 8, 'V' : 8,
'W' : 9, 'X' : 9, 'Y' : 9, 'Z' : 9,
}
"""
{ word_signature:word }
"""
signatures = {}
def suffeient(word):
"""
remove empty and un-mapped strings
"""
if not word:
return False
if len(word) >= 2 and word[-2] == "\'":
return False
for c in word:
if not (c.upper() in button_mapping.keys()):
return False
return True
def make_signature(word, sig_dict):
#def action_maker(seq):
# return lambda c: seq.append(c)
sig_tmp = []
#f = action_maker(sig_tmp)
[ sig_tmp.append(str(button_mapping[c])) for c in [c.upper() for c in word] ]
sig_dict[''.join(sig_tmp)] = word
def make_signatures(words):
global signatures
[ make_signature(word, signatures) for word in words if suffeient(word) ]
def build_word_list(number, words_dict, indent=0):
"""
Brief introduction of the algorithm:
Find all of the signatures that are contained in the number in the signature dict
Sort the matched keys in decend order
For each of the matched element:
divide the element into 3 parts: header, matched, reminder
call the build_word_list recursively to find the possible matched word
for the header and reminder parts, respectively
examine the result and return
There might be more than one word having the same signature, it depends the
signature-building algorithm and the signature storage's data structure. In
this solution the words having the duplicated signatures are omitted to
reduce the complexity of the implementation; this simplification should not
(um..., maybe not:-)) affect the correctness of the algorithm.
"""
def compose_result(header, matched, reminder):
ret = "%s-%s-%s" % (header, matched, reminder)
if ret[0] == '-':
return ret[1:]
elif ret[-1] == '-':
return ret[0:-1]
else:
return ret
#print '%sinput:%s' % (indent * '.', number )
if not number:
return True, number
if number in words_dict.keys():
#print '%smatches:%s' % ( indent * '.', words_dict[number] )
return True, number
else:
possible_matches = [num for num in words_dict.keys() if num in number]
possible_matches.sort(lambda a,b:len(a) < len(b) and 1 or -1)
#print '%spossible_matches: %s' % (indent * '.', [ (word, words_dict[word]) for word in possible_matches ])
for matched in possible_matches:
header_num, matched_num, reminder_num = number.partition(matched)
#print "header_num:%s, matched_num:%s, reminder_num:%s" % (header_num, matched_num, reminder_num)
header_matched, header_word = build_word_list(header_num,
words_dict, indent + 1)
reminder_matched, reminder_word = build_word_list(reminder_num,
words_dict, indent + 1)
if header_matched and reminder_matched:
return True, compose_result(header_word, matched, reminder_word)
elif header_matched and not reminder_matched:
return False, compose_result(header_word, matched, reminder_num)
elif not header_matched and reminder_matched:
return False, compose_result(header_num, matched, reminder_word)
elif not header_matched and not reminder_matched:
return False, compose_result(header_num, matched, reminder_num)
else:
pass
return False, number
def output(build_result):
global signatures
ret = ""
grouped_result = build_result[1].split('-')
#print "build result %s, %s:" % build_result
for group in grouped_result:
if signatures.has_key(group):
ret = ret + "-" + signatures[group]
else:
ret = ret + "-" + group
return ret[1:]
def input(number):
return ''.join(str(number).split('-'))
if __name__ == '__main__':
with file('words') as f:
trimed = [ line[0:-1] for line in f ]
make_signatures(trimed)
word_list = build_word_list('118737829', signatures)
print word_list and output(word_list) or "None"
word_list = build_word_list('7425877', signatures)
print word_list and output(word_list) or "None"
word_list = build_word_list('74258777425877', signatures)
print word_list and output(word_list) or "None"
word_list = build_word_list(input('1-800-7425-877'), signatures)
print word_list and output(word_list) or "None"
分享到:
相关推荐
Ruby+Selenium-Webdriver是一个强大的自动化测试工具组合,用于模拟真实用户在浏览器中与网页进行交互。Ruby是一种动态、面向对象的编程语言,而Selenium WebDriver是一个开源的自动化测试框架,支持多种浏览器和...
Ruby是一种面向对象、动态类型的脚本语言,由Yukihiro "Matz" Matsumoto于1995年创建。它以其简洁、优雅的语法和强大的编程能力而闻名,广泛应用于Web开发、脚本自动化、服务器管理等领域。RubyInstaller是Windows...
Ruby是一种面向对象、动态类型的脚本语言,由Yukihiro "Matz" Matsumoto于1995年创建。它以其简洁、优雅的语法和强大的编程能力而闻名,广泛应用于Web开发、脚本自动化、服务器管理等领域。RubyInstaller是Windows...
ruby-debug-ide是一个基于ruby-debug的库,它将调试功能暴露给IDE,使得开发者可以在图形化的环境中进行调试操作,如设置断点、查看变量值、单步执行等。ruby-debug-ide支持多种IDE,如NetBeans、RubyMine、Eclipse...
src-oepkgs/ruby-ruby2rubysrc-oepkgs/ruby-ruby2rubysrc-oepkgs/ruby-ruby2rubysrc-oepkgs/ruby-ruby2rubysrc-oepkgs/ruby-ruby2rubysrc-oepkgs/ruby-ruby2rubysrc-oepkgs/ruby-ruby2rubysrc-oepkgs/ruby-ruby2...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
标题中的"ruby-2.5.3-x64"指的是Ruby语言的特定版本,2.5.3,这是一个64位的构建。Ruby的版本迭代频繁,每个新版本通常会包含性能优化、新的特性和错误修复。2.5.3是2018年发布的一个稳定版本,它带来了诸如改进的...
ruby-oci8-2.1.5-x86-mingw32.gem,ruby连接oracle数据库gem包
Ruby,一种为简单快捷的面向对象编程(面向对象程序设计)而创的脚本语言,在20世纪90年代由日本人松本行弘(まつもとゆきひろ/Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License。...该版本为ruby-1.8.7-p302
mingw32-ruby-1.9.1-wxruby-2.0.1-setup.exe
Ruby是一种面向对象、动态类型的脚本语言,由Yukihiro "Matz" Matsumoto于1995年创建。它以其简洁、优雅的语法和强大的编程能力而闻名,广泛应用于Web开发、脚本自动化、服务器管理等领域。RubyInstaller是Windows...
《红宝石之书:冒险的动手指南》作为一本详尽且免费的Ruby语言教程,为初学者和进阶用户提供了全面的学习资源。本书不仅涵盖了Ruby编程的基础知识,还深入探讨了高级主题,使得读者能够在实践过程中掌握Ruby的核心...
ruby-1.8.7-p358-doc-chm.7z 3.65 MB 1,399 Other Other ruby-1.8.7-p358-i386-mingw32.7z 5.12 MB 1,503 i386 Other rubyinstaller-1.8.7-p358.exe 11.69 MB 13,534 i386 .exe (Windows executable)
ruby-irb-1.8.7.352-13.el6.x86_64.rpm ruby-irb-1.8.7.352-13.el6.x86_64.rpm
Ruby - Ruby 开发 - 常用知识点 backtracking、bit_manipulation、ciphers、conversions、data_structures、discrete_mathematics、dynamic_programming、electronics、maths
标题中的"ruby-1.9.2-p290.tar.gz"是一个开源编程语言Ruby的特定版本的归档文件,采用流行的tar和gzip格式进行压缩。这个版本是Ruby的1.9.2分支的一个更新点,标记为p290,意味着它是该分支的第290个补丁级别。在...
在开发过程中,调试是必不可少的一环,而`ruby-debug-1.87`就是Ruby社区中广泛使用的调试工具之一。本文将深入探讨`ruby-debug-1.87`的功能、安装与使用方法,以及它依赖的几个关键组件。 `ruby-debug-1.87`是一个...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装