- https://www.codewars.com/kata/57814d79a56c88e3e0000786 Simple Encryption #1 - Alternating Split.
Started at 23:54 06.10.2020 Tuesday Oct.
Finished at 02:36 07.10.2020 Wednesday Oct. (2rhs 42 minutes)
<?php | |
# https://www.codewars.com/kata/57814d79a56c88e3e0000786 Simple Encryption #1 - Alternating Split. | |
function encrypt($text, $n) { | |
if($n <= 0) return $text; | |
if(null === $text || 0 == strlen($text)) return $text; | |
while ($n > 0){ | |
$text = e_step($text); | |
$n -= 1; | |
} | |
return $text; | |
} | |
function decrypt($text, $n) { | |
if($n <= 0) return $text; | |
if(null === $text || 0 == strlen($text)) return $text; | |
if(null === $text || 0 == strlen($text)) return $text; | |
while ($n > 0){ | |
$text = d_step($text); | |
$n -= 1; | |
} | |
return $text; | |
} | |
function e_step($str){ | |
$a = ""; $b = ""; | |
for($i = 0; $i < strlen($str); $i+=1) | |
if(0 == $i % 2) $a .= $str[$i]; | |
else $b .= $str[$i]; | |
return $b.$a; | |
} | |
function d_step($str){ | |
$a = ""; $b = ""; $ans =""; | |
for($i = 0; $i < strlen($str); $i+=1) | |
if( $i < floor(strlen($str)/2) ) $b .= $str[$i]; | |
else $a .= $str[$i]; | |
for($i = 0, $j = 0; $i < strlen($a) && $j < strlen($b); $i+=1, $j+=1) $ans .= $a[$i].$b[$j]; | |
if($i < strlen($a)) $ans .= substr($a,$i); | |
return $ans; | |
} |
Started at 23:54 06.10.2020 Tuesday Oct.
Finished at 02:36 07.10.2020 Wednesday Oct. (2rhs 42 minutes)