patternphpMinor
Finding missing number from series
Viewed 0 times
numberfindingmissingseriesfrom
Problem
This code will accept user input from the command line.
The first user can enter the limit of series.
e.g: user may enter 5
After that, the user will be prompted for 5 numbers of a series.
e.g:
2
4
8
10
12
Here in this series, 6 is missing, so I am going to find the missing series from common difference series.
I got the correct output from this code. Is it the correct way? Is there any other simple code to do the same?
example input
5
2
4
8
10
12
The first user can enter the limit of series.
e.g: user may enter 5
After that, the user will be prompted for 5 numbers of a series.
e.g:
2
4
8
10
12
Here in this series, 6 is missing, so I am going to find the missing series from common difference series.
I got the correct output from this code. Is it the correct way? Is there any other simple code to do the same?
example input
5
2
4
8
10
12
Solution
Functions
The code can be more readable if it is broken into functions. This also separates the concern of reading in the series and calculating the missing element(s).
The use of functions also help to make the code more reusable. The
The
It then uses the difference of the expected series calculated using the PHP range function.
The code can be more readable if it is broken into functions. This also separates the concern of reading in the series and calculating the missing element(s).
function readSeries($fileHandle)
{
$series = array();
fscanf($fileHandle, "%d\n", $count);
for ($i = 0; $i < $count; ++$i)
{
fscanf($fileHandle, "%d\n", $series[$i]);
}
return $series;
}
function findMissing(Array $series)
{
sort($series);
$start = reset($series);
$finish = end($series);
$step = ($finish - $start) / count($series);
return array_diff(range($start, $finish, $step),
$series);
}
$series = readSeries(STDIN);
$missing = findMissing($series);
echo reset($missing);The use of functions also help to make the code more reusable. The
readSeries function can easily be used to read from standard input or another source.The
findMissing function takes advantage of some assumptions that can be made when a series is sorted:- It would be impossible to notice whether the beginning or end elements were missed, so they must both be present.
- One the beginning and end values are known then every step along this range must be present.
It then uses the difference of the expected series calculated using the PHP range function.
Code Snippets
function readSeries($fileHandle)
{
$series = array();
fscanf($fileHandle, "%d\n", $count);
for ($i = 0; $i < $count; ++$i)
{
fscanf($fileHandle, "%d\n", $series[$i]);
}
return $series;
}
function findMissing(Array $series)
{
sort($series);
$start = reset($series);
$finish = end($series);
$step = ($finish - $start) / count($series);
return array_diff(range($start, $finish, $step),
$series);
}
$series = readSeries(STDIN);
$missing = findMissing($series);
echo reset($missing);Context
StackExchange Code Review Q#40428, answer score: 3
Revisions (0)
No revisions yet.