Skip to content
Advertisement

Increasing the conditions in IF statement with loop PHP, Diagonal Check

I have homework to do this program in PHP, that take a matrix and the keywords, so it can find them in the matrix diagonally:

enter image description here

So this is how the first matrix looks, there are many with different keywords, here for instance keywords are “beef” and “pork”. So I made a program for an input that looks like these 2 examples:

enter image description hereenter image description here

There correct input for these two is here:

enter image description here enter image description here

Here is my code that works only in the first case, I don’t know how to make a loop that will make more and more conditions instead of writing down all these conditions with && in if statement.

Please give me some tips on how to do it:

<?PHP


$input_line = trim(fgets(STDIN));
$input_array = explode(" ",$input_line);

// get mojiban
$mojiban = array();
for($ix=0; $ix<$input_array[0];$ix++){
    $mojiban[] = str_split(trim(fgets(STDIN)));
}

//get words
    $words = array();
    for( $kx = 0; $kx < $input_array[1]; $kx++ ){
    $words[] = trim(fgets(STDIN));
}

////check verticales
//get word
    $wordCharArray = array();
for($dx=0; $dx < $input_array[1]; $dx++){
    $wordCharArray = str_split($words[$dx]);

//looping and checking
    
    for ( $line = 0; $line < $input_array[0] ; $line++) {
        for ( $column = 0; $column < $input_array[0] ; $column++ ){
           // for ($wordsNumber = 0; $wordsNumber < $input_array[1] ; $wordsNumber++){
            
                if ($mojiban[$line][$column] == $wordCharArray[0] && $mojiban[$line+1][$column+1] == $wordCharArray[1]) {
                    echo ($column+1)." ".($line+1)."n";
                
            //}
        }
    }
}

Thanks in Advance!

Advertisement

Answer

Here’s a solution to your problem right now it only checks for diagonal elements. You can refactor as you want from below.

There are many different solutions but the solution for your code I have pasted first checks for the character match in the array. If there is a match, it proceeds to check diagonally for the element from the position that it found the first element.

<?php

// First martix
// $searchWords = ["BEEF", "PORK"];
// $matrix = ["HPPLLM", "UROQUV", "FBSRZY", "DPEFKT", "GBBEUY", "EMCQFY"];

// Second martix
$searchWords = ["ABA", "BAB"];
$matrix = ["ACEG", "HBDF", "EGAC", "DFHB"];

// returns true if it diagonally matches the element in matrix from start row
function checkDiagonallyForString(string $search, array $matrix, int $startRow, int $firstMatchPosition)
{
    $endRow = $startRow + (strlen($search) - 2);
    $finding = true;

    foreach (range($startRow, $endRow) as $searchIndex => $rowValue) {
        if (!$finding) {
            break;
        }
        $char = $search[$searchIndex + 1];
        $finding = $matrix[$rowValue][$firstMatchPosition + $searchIndex] == $char;

    }
    return $finding;
}

// format: [word: [column, row]]
$found = [];

foreach ($matrix as $row => $matrixString) {
    if (!count($searchWords)) {
        break;
    }
    foreach ($searchWords as $wordRow => $word) {
        $position = strpos($matrixString, $word[1]);
        if ($position != false) {
            if (checkDiagonallyForString($word, $matrix, $row, $position)) {
                // $position = column of matrix
                // $row = row of matrix
                $found[$word] = [$position, $row];
                unset($searchWords[$wordRow]);
            }
        }
    }

}

// Pretty output
echo "<pre>";
print_r($found);

foreach ($found as $word => $indexes) {
    echo $word . " " . implode(" ", $indexes) . "n";
}

The results are: enter image description hereenter image description here

Note: This is a complete answer but before copying and pasting this please try to get a general idea and attempt to solve it yourself. This is not the only way of doing it.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement