|
| 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 | + |
0 commit comments