Last active
January 29, 2017 14:29
-
-
Save stknohg/6374ac0b08a316f8c4ace417c41d605d to your computer and use it in GitHub Desktop.
PowerShellでRot13( https://docs.google.com/document/d/1MPmU4xRsuCRyn1FVTSbeeeOpfXJ1x0XVXvl9SUZilXc のh29_0112)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Rot13を愚直に実装 | |
# | |
filter Rot13([switch]$Decript) { | |
if ($_ -isnot [string]) { | |
$_ = $_.ToString() | |
} | |
$radix = 13 | |
$output = "" | |
foreach ($c in [char[]]$_) { | |
switch ([int]$c) { | |
# A-Z | |
{$_ -ge 65 -and $_ -le 90} { | |
if ($Decript) { | |
$output += [char]((($_-90-$radix)%26)+90) | |
} else { | |
$output += [char]((($_-65+$radix)%26)+65) | |
} | |
} | |
# a-z | |
{$_ -ge 97 -and $_ -le 122} { | |
if ($Decript) { | |
$output += [char]((($_-122-$radix)%26)+122) | |
} else { | |
$output += [char]((($_-97+$radix)%26)+97) | |
} | |
} | |
Default { | |
$output += $c | |
} | |
} | |
} | |
Write-Output $output | |
} | |
# てすと | |
Import-Module Pester | |
Describe "Rot13のテスト" { | |
It "Rot13関数の基本形" { | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | Rot13 | Should Be "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm" | |
"NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm" | Rot13 -Decrpt | Should Be "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | |
} | |
It "アルファベット以外の文字がある場合" { | |
"Hello World!" | Rot13 | Should Be "Uryyb Jbeyq!" | |
"Uryyb Jbeyq!" | Rot13 -Decrpt | Should Be "Hello World!" | |
"こんにちは世界" | Rot13 | Should Be "こんにちは世界" | |
"こんにちは世界" | Rot13 -Decrpt | Should Be "こんにちは世界" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# ワンライナー版 | |
# ASCIIコードの変換テーブル $t で一発変換する富豪的実装です。 | |
# | |
"Hello"-as[char[]]|%{$t=,$null*65+78..90+65..77+,$null*6+110..122+97..109;$o=""}{if($w=[char]$t[[int]$_]){$o+=$w}else{$o+=$_}}{$o} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment