2008脚本大赛PowerShell高级组Event 1解题及分析

中文题目位置: http://www.microsoft.com/technet/scriptcenter/funzone/games/games08/chs/aevent1.mspx

英文解题: http://www.microsoft.com/technet/scriptcenter/funzone/games/solutions08/apssol01.mspx

题目比较容易, 官方解题思路很好. 我的办法

Map-WordToDigit可以将一组字符映射为键盘上的数字. 然后将wordlist.txt中符合要求的单词(长度为7, 并且只包含规定的字符)利用Map-WordToDigit变换为对应的电话号码. 这样hash表的键是电话号码, 而值是单词. 最后利用用户输入的电话直接在hash表中查找即可. 唯一注意的就是PowerShell中对字符串的迭代要求显示取得迭代器.

$ofs   =   ''
$hash   =  @{}
$maps   =  @{
=   2 ;b  =   2 ; c  =   2 ;
=   3 ;e  =   3 ;f  =   3 ;
=   4 ;h  =   4 ;i  =   4 ;
=   5 ;k  =   5 ;l  =   5 ;
=   6 ;n  =   6 ;o  =   6 ;
=   7 ;r  =   7 ;s  =   7 ;
=   7 ;u  =   8 ;v  =   8 ;
=   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 : Scripts wordlist . txt  |   ?  { (( $_ . length   - eq  7 - and ( $_   - imatch  " ^$charRange+`$ " )) }  |  `
% { $key   =   Map - WordToDigit  $_ $hash [ $key =   $_ }
$phoneNumber   =   Read - Host  " Please enter your phone number "
$hash [ $phoneNumber ]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值