PowerShell 技能连载 - 智力游戏生成器

人脑可以阅读每个单词头尾字母正确而其它字母顺序错乱的橘子。以下是一个可以自己实验的 PowerShell 函数。它输入一个句子,并将除了每个单词首末字母之外的字母打乱:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function Convert-Text
{
param
(
$Text = 'Early to bed and early to rise makes a man healthy, wealthy and wise.'
)
$words = $Text -split ' '

$newWords = foreach($word in $words)
{
if ($word.Length -le 2)
{
$word
}
else
{
$firstChar = $word[0]
$lastChar = $word[-1]
$charLen = $word.Length -2
$inbetween = $word[1..$charLen]
$chars = $inbetween | Get-Random -Count $word.Length
$inbetweenScrambled = $chars -join ''
"$firstChar$inbetweenScrambled$lastChar"
}
}

$newWords -join ' '
}

如果没有输入文本,那么将采用默认文本。您可以猜出它的意思吗?

1
2
PS C:\> Convert-Text
Ealry to bed and erlay to rsie maeks a man hylhtea, wlhtaey and wies.

PowerShell 技能连载 - 智力游戏生成器

http://blog.vichamp.com/2019/03/20/mind-jogging-generator/

作者

吴波

发布于

2019-03-20

更新于

2022-07-06

许可协议

评论