PHP finds missing numbers in hyphen array

I have an array of numbers, these numbers are sometimes decrypted, or software version numbers. What I'm trying to do is echo "Missing!" or run a specific function when the number is missing.

For instance:

$numbers = array('1', '2', '3', '5', '6', '8');

Print

1
2
3
Missing!
5
6
Missing!
8

I have problems with hyphens.

For instance:

$numbers = array('1', '1-1', '1-3', '3-1-1', '3-1-3');

Print

1
1-1
Missing!
1-3
Missing!
3-1-1
Missing!
3-1-3

Plus, my code seems awfully long / does too many things, for which - it seems to me - there should be a simple task. Is there a way or algorithm for this kind of thing?

Here is my code:

<?php

    $numbers = array(
        '1',
        '1-1',
        '1-3',
        '3-1-1',
        '3-1-3'
    );

    foreach ($numbers as $number) {
        if (isset($prev_number)) {
            $curr_number = explode('-', $number);
            $prev_levels = explode('-', $prev_number);

            if (preg_match('/-/', $number) and !preg_match('/-/', $prev_number)) {
                if (current() - $prev_levels[0] >= 1) {
                    echo 'Missing!<br>' . PHP_EOL;
                }
            }

            for ($missing = 1; ((count($curr_number) - count($prev_levels)) - $missing) >= 1; $missing++) {
                echo 'Missing!<br>' . PHP_EOL;
            }

            foreach ($curr_number as $hyphen => $part) {
                for ($missing = 1; ($part - $missing) - $prev_levels[$hyphen] >= 1; $missing++) {
                    echo 'Missing!<br>' . PHP_EOL;
                }
            }
        } else {
            if ($number != '1') {
                echo 'Missing!<br>' . PHP_EOL;

                foreach ($curr_number as $part) {
                    for ($missing = 1; $part > $missing; $missing++) {
                        echo 'Missing!<br>' . PHP_EOL;
                    }
                }
            }
        }

        echo $number . '<br>' . PHP_EOL;

        $prev_number = $number;
    }

?>
+4
source share
2 answers

You can iterate through the list, and for each pair you are trying to reach the second by applying the conversion to the first:

  • , . "1" "2", "1-1" "1-2".
  • sub, . "1" "1-1", "1-1" "1-1-1".

# 1 , .

, . :

$numbers = array('1', '1-1', '1-2', '1-3', '3-1-1', '3-1-3');

$first = array_shift($numbers);
echo "$first\n";

while (($second = array_shift($numbers)) !== null) {
    if (find_next($first, $second) === false) {
        echo "Missing\n";
    }
    echo "$second\n";
    $first = $second;
}

// attempt to transform between current and next
function find_next($current, $next)
{
    if (increment_last($current) == $next) {
        return $next; // first transformation worked
    } elseif (add_suffix($current) == $next) {
        return $next; // second transformation worked
    }
    return false; // nothing worked
}

// transformation 1
function increment_last($value)
{
    if (($pos = strpos($value, '-')) !== false) {
        $last = substr($value, $pos + 1) + 1;
        return substr_replace($value, $last, $pos + 1, strlen($last));
    } else {
        return $value + 1;
    }
}

// transformation 2
function add_suffix($value)
{
    return "$value-1";
}

, ; , , , , .

0

.

/ - - .

. , . .

: 6 explode . . , explode , .

0

Source: https://habr.com/ru/post/1531760/


All Articles