Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active September 1, 2020 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/4c1a1800039cf6d94ca3f14b4b3db220 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/4c1a1800039cf6d94ca3f14b4b3db220 to your computer and use it in GitHub Desktop.
Just PHP FUN 091.
<?php
# https://www.codewars.com/kata/5ca24526b534ce0018a137b5 Texting with an old-school mobile phone.
function sendMessage($message) {
$hash = [
"a"=> "2", "A"=> "2", "b"=> "22", "B"=> "22", "c"=> "222", "C"=> "222", "d"=> "3", "D"=> "3", "e"=> "33", "E"=> "33", "f"=> "333", "F"=> "333", "g"=> "4", "G"=> "4", "h"=> "44", "H"=> "44", "i"=> "444", "I"=> "444", "j"=> "5", "J"=> "5", "k"=> "55", "K"=> "55", "l"=> "555", "L"=> "555", "m"=> "6", "M"=> "6", "n"=> "66", "N"=> "66", "o"=> "666", "O"=> "666", "p"=> "7", "P"=> "7", "q"=> "77", "Q"=> "77", "r"=> "777", "R"=> "777", "s"=> "7777", "S"=> "7777", "t"=> "8", "T"=> "8", "u"=> "88", "U"=> "88", "v"=> "888", "V"=> "888", "w"=> "9", "W"=> "9", "x"=> "99", "X"=> "99", "y"=> "999", "Y"=> "999", "z"=> "9999", "Z"=> "9999", "1"=> "1-", "2"=> "2-", "3"=> "3-", "4"=> "4-", "5"=> "5-", "6"=> "6-", "7"=> "7-", "8"=> "8-", "9"=> "9-", "0"=> "0-", "#"=> "#-", "*"=> "*-", "."=> "1", ","=> "11", "?"=> "111", "!"=> "1111", "'"=> "*", "-"=> "**", "+"=> "***", "="=> "****", " "=> "0",
];
echo "$message \n";
$ans = "";
$case = "down";
for($i = 0; $i < strlen($message); $i+=1){
$char = $message[$i];
if(preg_match('/[a-z]/i',$char)){
echo "It is character. \n";
$tmp = "";
if(preg_match('/[a-z]/',$char)){
$tmp = 'down';
}else{
$tmp = 'up';
}
if($tmp != $case) {
$ans .= "#";
}
$case = $tmp;
}
$set = $hash[$char];
$emtpy = (!empty($ans)) ? 1 : 0 ;
$size = strlen($ans);
echo "this is ans = '$ans'\n";
echo "'$char' => $set ($size , $emtpy) \n";
if(($size != 0) && ($ans[strlen($ans)-1] == $set[0])) {
$ans .= " ";
}
$ans .= $set;
}
return $ans;
}
@lbvf50mobile
Copy link
Author

class MyTestCases extends TestCase {
  public function testSampleTests() {
    $this->assertEquals("'0 0 0 0 0'", sendMessage("     "));
    $this->assertEquals("4433999", sendMessage("hey"));
    $this->assertEquals("666 6633089666084477733 33", sendMessage("one two three"));
    $this->assertEquals("#44#33555 5556660#9#66677755531111", sendMessage("Hello World!"));
    $this->assertEquals("#3#33 3330#222#666 6601-1111", sendMessage("Def Con 1!"));
    $this->assertEquals("#2**#9999", sendMessage("A-z"));
    $this->assertEquals("1-9-8-4-", sendMessage("1984"));
    $this->assertEquals("#22#444 4084426655777703336667770222443322255444664066688 806999055282", sendMessage("Big thanks for checking out my kata"));
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment