Skip to content

Instantly share code, notes, and snippets.

@joubertredrat
Last active March 30, 2016 19:56
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 joubertredrat/8da476950f3b9ae729af to your computer and use it in GitHub Desktop.
Save joubertredrat/8da476950f3b9ae729af to your computer and use it in GitHub Desktop.
PHP is_sequence() concept to implement on Math library

This is only a concept to be analyzed

is_sequence

is_sequence - Verify if number range have correct sequence or arithmetic progression

Description

bool is_sequence ( array $array , int|float $sequence [, int $flags ] )

Parameters

array

array with numbers to validation

sequence

sequence number to validation

flags

set if number sequence is positive or negative

Avaliable flags

Flag Description
SEQUENCE_POSITIVE Define sequence as positive
SEQUENCE_NEGATIVE Define sequence as negative

Return Values

This function return TRUE if array sequence is correct, FALSE otherwise.

Examples

Example #1 is_sequence() examples

<?php
$array = array(5, 7, 9, 11, 13);
$sequence = 2;

if (is_sequence($array, $sequence) === true) {
    echo "Yes, ".implode(", ", $array)." have correct sequence of ".$sequence." number";
} else {
	echo "No, ".implode(", ", $array)." don't have sequence of ".$sequence." number";
}
?>

Example #2 is_sequence() examples

<?php
$array = array(1.4, 1.2, 0.9, 0.7, 0.5);
$sequence = 0.2;

var_dump(is_sequence($array, $sequence, SEQUENCE_NEGATIVE));
?>

The above example will output:

bool(false)
<?php
define('SEQUENCE_POSITIVE', 1);
define('SEQUENCE_NEGATIVE', 0);
/**
* is_sequence conception in PHP
*
* @param $array array with numbers to validation.
* @param int|float $sequence sequence number to validation.
* @param int $flag Set if number sequence is positive or negative.
* @return bool return true if array sequence is correct, false otherwise.
*/
function is_sequence(Array $array, $sequence, $flag = SEQUENCE_POSITIVE) {
if (!is_numeric($sequence)) {
throw new Exception('$sequence param need to be numeric');
}
$decimal = strrpos($sequence, '.') !== false ? (strlen($sequence) - strrpos($sequence, '.') - 1) : 0;
if ($flag === SEQUENCE_NEGATIVE) {
$array = array_reverse($array);
}
for ($i = 0; $i < count($array); $i++) {
if ($array[$i + 1] && (number_format($array[$i] + $sequence, $decimal) != number_format($array[$i + 1], $decimal)))
return false;
}
return true;
}
<?php
require('is_sequence.php');
$array = array(1.4, 1.2, 1.0, 0.8, 0.6);
$sequence = 0.2;
var_dump(is_sequence($array, $sequence, SEQUENCE_NEGATIVE)); // true
$array = array(0.0002, 0.0017, 0.0032, 0.0047);
$sequence = 0.0015;
var_dump(is_sequence($array, $sequence)); // true
$array = array(-10, -9, -8, -7, -6, -5, -4);
$sequence = 1;
var_dump(is_sequence($array, $sequence)); // true
$array = array(-36, -24, -12, 0, 12, 24, 36);
$sequence = 12;
var_dump(is_sequence($array, $sequence)); // true
$array = array(36, 24, 12, 0, -12, -24, -36);
$sequence = 12;
var_dump(is_sequence($array, $sequence, SEQUENCE_NEGATIVE)); // true
$array = array(1.4, 1.2, 1.0, 1.0, 0.8, 0.6);
$sequence = 0.2;
var_dump(is_sequence($array, $sequence, SEQUENCE_NEGATIVE)); // false
$array = array(0.0002, 0.0017, 0.0026, 0.0047);
$sequence = 0.0015;
var_dump(is_sequence($array, $sequence)); // false
$array = array(-10, -9, 8, -7, -6, -5, -4);
$sequence = 1;
var_dump(is_sequence($array, $sequence)); // false
// This will depend on the precision of floating point sequence, because 0.00325557 can be FALSE and 0.00321067 can be TRUE
$array = array(0.0002, 0.0017, 0.00325557, 0.0047);
$sequence = 0.0015;
var_dump(is_sequence($array, $sequence)); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment