-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeighbourLetters.php
67 lines (56 loc) · 1.95 KB
/
NeighbourLetters.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
class NeighbourLetters
{
/**
* takes a string and checks if every single character is preceded
* and followed by a character adjacent to it in the english alphabet
* return bool
*/
public static function checkNeighbourLetters(string $_paramsLetters)
{
$arrayParamsWords = str_split($_paramsLetters);
$wordsLenth = count($arrayParamsWords);
foreach($arrayParamsWords as $key => $words){
if ($key == 0) {
$tmp_key = $key + 1;
$arrayParamsWords[$tmp_key];
$result = self::checkRange($arrayParamsWords[$key], $arrayParamsWords[$tmp_key]);
if ($result == 0) {
return false;
}
}elseif ($key == $wordsLenth-1) {
$tmp_left_key = $key-1;
$result = self::checkRange($arrayParamsWords[$key], $arrayParamsWords[$tmp_left_key]);
if ($result == 0) {
return false;
}
}elseif ($key != 0) {
$tmp_left_key = $key-1;
$tmp_right_key = $key+1;
$result =self::checkRange($arrayParamsWords[$key], $arrayParamsWords[$tmp_left_key]);
$result = self::checkRange($arrayParamsWords[$key], $arrayParamsWords[$tmp_right_key]);
if ($result == 0) {
return false;
}
}
}
echo 'OK';
return true;
}
/**
* check range between 2 given words return true or false
*/
public static function checkRange($_words1, $_words2)
{
//range marche de a à z ou l'unverse(z à a)
$wordsRange = range($_words1, $_words2);
$lenthRange = count($wordsRange);
if ($lenthRange == 2) {
return 1;
}else{
return 0;
}
}
}
$words = "zbc";
NeighbourLetters::checkNeighbourLetters($words);