全部博文(74)
分类: WINDOWS
2008-03-01 17:26:34
中文题目位置:
英文解题:
题目比较容易, 官方解题思路很好. 我的办法
Map-WordToDigit可以将一组字符映射为键盘上的数字. 然后将wordlist.txt中符合要求的单词(长度为7, 并且只包含规定的字符)利用Map-WordToDigit变换为对应的电话号码. 这样hash表的键是电话号码, 而值是单词. 最后利用用户输入的电话直接在hash表中查找即可. 唯一注意的就是PowerShell中对字符串的迭代要求显示取得迭代器.
$ofs = ''
$hash = @{}
$maps = @{
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;r = 7;s = 7;
t = 7;u = 8;v = 8;
w = 9;x = 9;y = 9;
}
# construct a regular expression characters range
$charRange = "[$($maps.keys)]"
function Map-WordToDigit ($word)
{
$digits = $word.GetEnumerator() | % { $maps[$_.ToString()] };
"$digits";
}
# dirty pipeline to cache all of valid words in wordlist.txt.
Get-Content -Path C:Scriptswordlist.txt | ? { (($_.length -eq 7) -and ($_ -imatch "^$charRange+`$")) } | `
%{$key = Map-WordToDigit $_; $hash[$key] = $_}
$phoneNumber = Read-Host "Please enter your phone number"
$hash[$phoneNumber]