Skip to content

Commit 72d106c

Browse files
committed
Encrypt This / Decipher This
ReverseOrRotate
1 parent e8759b3 commit 72d106c

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

Diff for: EncryptThisDecipherThis.php

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
// Deciper This!
4+
5+
//You are given a secret message you need to decipher. Here are the things you need to know to decipher it:
6+
//
7+
//For each word:
8+
//
9+
//the second and the last letter is switched (e.g. Hello becomes Holle)
10+
//the first letter is replaced by its character code (e.g. H becomes 72)
11+
//Note: there are no special characters used, only letters and spaces
12+
//
13+
//Examples
14+
//
15+
//decipherThis('72olle 103doo 100ya'); // 'Hello good day'
16+
//decipherThis('82yade 115te 103o'); // 'Ready set go'
17+
18+
19+
20+
21+
function decipherThis($str){
22+
// We know that $str is a string only letters and spaces
23+
// First we need to explode the string into an array
24+
// Then we need to loop through the array
25+
// We need to filter the array to get the numbers only and then we need to convert the numbers to letter
26+
// Then we replace word in array with the new word - first letter is replaced by its character code (e.g. H becomes 72) using chr() function, then we add last letter then we add the middle part of the word and add second letter
27+
// If the word contains only 2 letters then we dont add the middle part of the word ( because for example go will result in goo) if the word contains only 1 letter then we only use the chr() function
28+
// Then we need to implode the array back into a string
29+
30+
$arr = explode(' ', $str);
31+
32+
foreach ($arr as $key => $value) {
33+
$charCode = filter_var($arr[$key], FILTER_SANITIZE_NUMBER_INT);
34+
$charLen = strlen($charCode);
35+
if(strlen($arr[$key]) - $charLen >= 2) {
36+
$arr[$key] = chr($charCode) . substr($arr[$key], -1, 1) . substr($arr[$key], $charLen+1, -1) . substr($arr[$key], strlen($charCode), 1);
37+
} elseif (strlen($arr[$key]) - $charLen >= 1) {
38+
$arr[$key] = chr($charCode) . substr($arr[$key], -1, 1) . substr($arr[$key], $charLen+1, -1);
39+
} else {
40+
$arr[$key] = chr($charCode);
41+
}
42+
}
43+
44+
return implode(' ', $arr);
45+
}
46+
47+
48+
// Cleanest solution I found:
49+
50+
function decipherThis($str) {
51+
return preg_replace_callback("/(\d+)([^ ]*)/", function ($m) {
52+
return chr($m[1]) . (strlen($m[2]) < 2 ? $m[2] : substr($m[2], -1) . substr($m[2], 1, -1) . $m[2][0]);
53+
}, $str);
54+
}
55+
56+
57+
58+
59+
// Encrypt this!
60+
61+
//Acknowledgments:
62+
//I thank yvonne-liu for the idea and for the example tests :)
63+
//
64+
//Description:
65+
//Encrypt this!
66+
//
67+
//You want to create secret messages which can be deciphered by the Decipher this! (https://www.codewars.com/kata/decipher-this) kata. Here are the conditions:
68+
//
69+
//Your message is a string containing space separated words.
70+
//You need to encrypt each word in the message using the following rules:
71+
//The first letter must be converted to its ASCII code.
72+
//The second letter must be switched with the last letter
73+
//Keepin' it simple: There are no special characters in the input.
74+
//Examples:
75+
//encryptThis("Hello") === "72olle"
76+
//encryptThis("good") === "103doo"
77+
//encryptThis("hello world") === "104olle 119drlo"
78+
79+
80+
81+
82+
function encryptThis($text): string
83+
{
84+
// Doing decryption first and then encryption later is easier
85+
// We need to explode the string into an array
86+
// Then we need to loop through the array
87+
// First letter needs to be converted to its ASCII code using ord() function
88+
// Then we need to check if the word contains more than 2 letters
89+
// If the word contains more than 2 letters then we need to add the last letter then we need to add the middle part of the word and then we need to add the second letter
90+
91+
$arr = explode(' ', $text);
92+
93+
foreach ($arr as $key => $value) {
94+
$arr[$key] = ord($arr[$key][0]) . (strlen($arr[$key]) > 2 ? substr($arr[$key], -1) . substr($arr[$key], 2, -1) . substr($arr[$key], 1, 1) : substr($arr[$key], 1));
95+
}
96+
97+
return implode(' ', $arr);
98+
}
99+
100+
101+
102+
103+
104+
105+
106+
107+

Diff for: OppositeNumber.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
// Very simple, given an integer or a floating-point number, find its opposite.
4+
//
5+
//Examples:
6+
//
7+
//1: -1
8+
//14: -14
9+
//-34: 34
10+
11+
function opposite($n) {
12+
return -$n;
13+
}

Diff for: ReverseOrRotate.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
//The input is a string str of digits. Cut the string into chunks (a chunk here is a substring of the initial string) of size sz (ignore the last chunk if its size is less than sz).
4+
//
5+
//If a chunk represents an integer such as the sum of the cubes of its digits is divisible by 2, reverse that chunk; otherwise rotate it to the left by one position. Put together these modified chunks and return the result as a string.
6+
//
7+
//If
8+
//
9+
//sz is <= 0 or if str is empty return ""
10+
//sz is greater (>) than the length of str it is impossible to take a chunk of size sz hence return "".
11+
//Examples:
12+
//revrot("123456987654", 6) --> "234561876549"
13+
//revrot("123456987653", 6) --> "234561356789"
14+
//revrot("66443875", 4) --> "44668753"
15+
//revrot("66443875", 8) --> "64438756"
16+
//revrot("664438769", 8) --> "67834466"
17+
//revrot("123456779", 8) --> "23456771"
18+
//revrot("", 8) --> ""
19+
//revrot("123456779", 0) --> ""
20+
//revrot("563000655734469485", 4) --> "0365065073456944"
21+
//Example of a string rotated to the left by one position:
22+
//s = "123456" gives "234561".
23+
24+
25+
function revRot($s, $sz) {
26+
27+
//This one might be a bit tricky. I will try to explain it as best as I can.
28+
//First I create a condition that says that if the size of the chunk is less than or equal to 0 or if the size of the chunk is greater than the length of the string, then return an empty string.
29+
//Then I create a variable $chunks and i split the string into chunks of size $sz.
30+
//Then I create a variable $result and set it to an empty string.
31+
//Then I create a foreach loop that will loop through the chunks.
32+
//Then I create a condition that says that if the length of the chunk is equal to the size of the chunk, then do the following:
33+
//1. Create a variable $sum and set it to 0.
34+
//2. Create a foreach loop that will loop through the chunk.
35+
//3. Create a condition that says that if the sum of the cubes of the digits of the chunk is divisible by 2, then reverse the chunk and push it to the $result variable.
36+
//4. Create a condition that says that if the sum of the cubes of the digits of the chunk is not divisible by 2, then rotate the chunk to the left by one position and push it to the $result variable.
37+
//5. Return the $result variable.
38+
39+
if($sz <= 0 || $sz > strlen($s)) return "";
40+
$chunks = str_split($s, $sz);
41+
$result = "";
42+
43+
foreach($chunks as $chunk) {
44+
if(strlen($chunk) === $sz) {
45+
$sum = 0;
46+
foreach(str_split($chunk) as $num) {
47+
$sum += $num * $num * $num;
48+
}
49+
$result .= $sum % 2 == 0 ? strrev($chunk) : substr($chunk, 1) . substr($chunk, 0, 1);
50+
}
51+
}
52+
53+
return $result;
54+
}

0 commit comments

Comments
 (0)