PHP merge sort not working - php

I made a merge sort in python which works fine and I've tried recreating the logic in PHP but it is not working correctly. Here is the python code:
def merge(array):
if len(array) > 1:
arrayL = []
arrayR = []
for i in range(int(len(array)/2)):
arrayL.append(array[i])
for i in range(int(len(array)/2),len(array)):
arrayR.append(array[i])
arrayL = merge(arrayL)
arrayR = merge(arrayR)
left = right = insert = 0
while left < len(arrayL) and right < len(arrayR):
if arrayL[left] < arrayR[right]:
array[insert] = arrayL[left]
left += 1
else:
array[insert] = arrayR[right]
right += 1
insert += 1
while left < len(arrayL):
array[insert] = arrayL[left]
left += 1
insert += 1
while right < len(arrayR):
array[insert] = arrayR[right]
right += 1
insert += 1
return array
And this is from the shell:
>>> merge([23,21,5,111,32,11,7,1,45])
[1, 5, 7, 11, 21, 23, 32, 45, 111]
Here is the PHP code:
function merge($array) {
$arrayLength = count($array);
if ($arrayLength > 1) {
$arrayLeft = array_slice($array, 0, $arrayLength/2);
$arrayRight = array_slice($array, $arrayLength/2);
$arrayLeft = merge($arrayLeft);
$arrayRight = merge($arrayRight);
$left = 0;
$right = 0;
$insert = 0;
while ($left < count($arrayLeft) and $right < count($arrayRight)){
if ($arrayLeft[$left] < $arrayRight[$right]) {
$array[$insert] = $arrayLeft[$left];
$left += 1;
} else {
$array[$insert] = $arrayRight[$right];
$right += 1;
}
$insert += 1;
}
while ($left < count($arrayLeft)) {
$array[$insert] = $arrayLeft[$left];
$left += 1;
$insert += 1;
}
while ($right < count($arrayRight)) {
$array[$insert] = $arrayRight[$right];
$right += 1;
$insert += 1;
}
return $array;
}
}
print_r(merge(array(23,21,5,111,32,11,7,1,45)));
And this is what it outputs:
Array ( [0] => 1 [1] => 5 [2] => 23 [3] => 21 [4] => 32 [5] => 11 [6] => 45 [7] => 45 [8] => 111 )
Any help would be very much appreciated

The return $array; statement should be outside the if scope.

Related

i need a PHP code to find longest contiguous sequence of characters in the string

I need a PHP code to find longest contiguous sequence of characters in the string. So if b is coming together for maximum number of times your program should echo b and count
Example string:
aaabababbbbbaaaaabbbbbbbbaa
Output must be:
b 8
Using
- preg_match_all to get sequences of repeating characters,
- array_map along with strlen to get the string length of each sequence
- max to get the biggest value in the array.
Consider the following example:
$string = "aaabababbbbbaaaaabbbbbbbbaa";
preg_match_all('#(\w)\1+#',$string,$matches);
print_r($matches);
Will output
Array
(
[0] => Array
(
[0] => aaa
[1] => bbbbb
[2] => aaaaa
[3] => bbbbbbbb
[4] => aa
)
[1] => Array
(
[0] => a
[1] => b
[2] => a
[3] => b
[4] => a
)
)
Next we get the sizes for each string of repeating characters
$sizes = array_map('strlen', $matches[0]);
print_r($sizes);
Will output
Array
(
[0] => 3
[1] => 5
[2] => 5
[3] => 8
[4] => 2
)
Now let's get the biggest value of the $sizes array
print max($sizes);
Will give us
8
We need the key for the max value to pick up the letter
$maxKey = array_keys($sizes, max($sizes));
print $matches[1][$maxKey[0]];
Will output
b
Since you're looking for continuous sequences:
$string = 'aaabababbbbbaaaaabbbbbbbbaa';
$count = strlen($string);
if ($count > 0)
{
$mostFrequentChar = $curChar = $string[0];
$maxFreq = $curFreq = 1;
for ($i = 1; $i < $count; $i++)
{
if ($string[$i] == $curChar)
{
$curFreq++;
if ($curFreq > $maxFreq)
{
$mostFrequentChar = $curChar;
$maxFreq = $curFreq;
}
}
else
{
$curChar = $string[$i];
$curFreq = 1;
}
}
}
echo $mostFrequentChar . ' ' . $maxFreq;
$string = 'aaabababbbbbaaaaabbbbbbbbaa';
$occurrence = [];
$count = strlen($string);
for ($x = 0; $x < $count; $x++) {
if(isset($ocurrence[$string[$x]])) {
$ocurrence[$string[$x]]++;
} else {
$ocurrence[$string[$x]] = 0;
}
}
var_dump($occurrence);
This should do the trick.
<?php
$x = "aaaaaaabbbbbbbaaaacccccccaaaaaaaaaaaaaaaaabbbbbbaaaaadddddddddddddddddddddddddddddddddd";
$count = strlen($x);
$y =array();
$n =0;
$d =1;
$first = $x{1};
for($j = $d;$j < $count;$j++)
{
if($x{$j} == $first )
{
$y[$j] = $x{$j};
$first = $x{$j};
}
elseif($x{$j} != $first )
{
$y[$j] = ",".$x{$j};
$first = $x{$j};
}
}
$xy = implode("",$y);
$xy1 = explode(",",$xy);
$c_count = count($xy1);
$dg = 1;
for($g = 0;$g < $c_count;$g++)
{
$cnt = strlen($xy1[$g]);
$cntm = $xy1[$g];
$replace = $cntm{1};
if($cnt > $dg)
{
$ab = str_replace($cntm,$replace,$cntm);
$dg = $cnt.".".$ab ;
}
}
echo $dg;
?>

Generate combination array PHP [duplicate]

This question already has answers here:
PHP array combinations
(8 answers)
Closed 7 years ago.
Hi Guys i have an array like this
$array1 = array('a','b','c','d)
and i want to combine with output like this
'a,b,c'
'a,b,d'
'a,c,d'
'b,c,d'
The problem is to create a function with a variable number and not multiple variable, can anybody help me?
Make a try
[akshay#localhost tmp]$ cat permutation_comb.php
<?php
function _perm($comb,$arr)
{
$arr_len = count($arr);
$comb = intval($comb);
if ($comb > $arr_len)
{
$p = 0;
}
elseif ($arr_len == $comb)
{
$p = 1;
}
else {
if ($comb >= $arr_len - $comb)
{
$l = $comb+1;
for ($i = $l+1 ; $i <= $arr_len ; $i++)
$l *= $i;
$m = 1;
for ($i = 2 ; $i <= $arr_len-$comb ; $i++)
$m *= $i;
}
else {
$l = ($arr_len-$comb) + 1;
for ($i = $l+1 ; $i <= $arr_len ; $i++)
$l *= $i;
$m = 1;
for ($i = 2 ; $i <= $comb ; $i++)
$m *= $i;
}
}
if(!isset($p)){ $p = $l/$m ; }
$out = array_fill(0, $p, array_fill(0, $comb, '') );
$t = array();
for ($i = 0 ; $i < $comb ; $i++)
$t[$i] = $i;
$out[0] = $t;
for ($i = 1 ; $i < $p ; $i++)
{
if ($t[$comb-1] != count($arr)-1)
{
$t[$comb-1]++;
}
else {
$xx = -1;
for ($j = $comb-2 ; $j >= 0 ; $j--)
if ($t[$j]+1 != $t[$j+1])
{
$xx = $j;
break;
}
if ($xx == -1)
break;
$t[$xx]++;
for ($j = $xx+1 ; $j < $comb ; $j++)
$t[$j] = $t[$xx]+$j-$xx;
}
$out[$i] = $t;
}
for ($i = 0 ; $i < $p ; $i++)
for ($j = 0 ; $j < $comb ; $j++)
$out[$i][$j] = $arr[$out[$i][$j]];
return $out;
}
$Input = array('a','b','c','d');
$output = array_map(function($a){ return implode(",",$a); },_perm(3, $Input));
// Input
print_r($Input);
// Combination output
print_r($output);
?>
Output
[akshay#localhost tmp]$ php permutation_comb.php
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
Array
(
[0] => a,b,c
[1] => a,b,d
[2] => a,c,d
[3] => b,c,d
)
To get all possible combination of chars modify calling part of function like below
$output = array();
for($i=1; $i<=count($Input); $i++)
{
$output = array_merge($output, array_map(function($a){ return implode(",",$a); },_perm($i, $Input)) ) ;
}
Which results
[akshay#localhost tmp]$ php permutation_comb.php
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => a,b
[5] => a,c
[6] => a,d
[7] => b,c
[8] => b,d
[9] => c,d
[10] => a,b,c
[11] => a,b,d
[12] => a,c,d
[13] => b,c,d
[14] => a,b,c,d
)

Is my number not good enough?

Excuse the tardy title.
This question is related to variable by reference.
Lets say we have a little for loop like this:
for($i = 0; $i < 5; $i++) {
$r = 15;
$t = &$i + $r;
array_push($a, $t);
}
Expected output:
Array
(
[0] => 15
[1] => 16
[2] => 17
[3] => 18
[4] => 19
)
Returned output:
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
)
Yet if I have something like this:
for($i = 0; $i < 5; $i++) {
$r = 15;
$num = &$i;
$t = $num + $r;
array_push($a, $t);
}
It returns the exact expected result as above.
Could somebody please shed some light on this.
I'll have a go...
In the second example:
for($i = 0; $i < 5; $i++) {
$r = 15;
$num = &$i; // now $num and $i are the same thing - perfectly legitimate
$t = $num + $r; // equivalent to $t = $i + $r
array_push($a, $t);
}
Whereas in the first example:
for($i = 0; $i < 5; $i++) {
$r = 15;
$t = &$i + $r; // $t and $i are the same thing. $r is not involved
$t = &$i * "anything you want"; // this does exactly the same thing
array_push($a, $t);
}
Note that in the documentation, these kinds of assignments are written like $t =& $i, which I think is a useful reminder that you're just creating an alias between two things and anything else in the same statement is irrelevant.

Sum certain indice values in MultiDimensional Array

I have the following:
Array
(
[0] => Array
(
[department] => Central>ACME>BusDev
[Total_Staff] => 4
[Total_Resp] => 0
)
)
There are over 150 of these within the array.
I can easily sum, for example, Total_Staff and Total_Resp using something like:
foreach($arr as $num => $values) {
$sum_staff += $values[ 'Total_Staff' ];
$sum_resp += $values[ 'Total_Resp' ];
}
However what I need to do is sum only elements of the array, for example I need to sum Total_Staff and Total_resp that lies between indexes 0 and 7 or 12 and 58.
Not sure how to go about this.
Is this what you need
foreach($arr as $num => $values) {
if(($num >= 0 && $num <= 7) || ($num >= 12 && $num <= 58))
$sum += $values[ 'Total_Staff' ];
}
Using a for loop would be the way to solve this:
$start = 12;
$end = 58;
$sum_total_staff = 0;
$sum_total_resp = 0;
for( $i = $start; $i <= $end; $i++ ) {
$sum_total_staff += $arr[$i]["Total_Staff"];
$sum_total_resp += $arr[$i]["Total_Resp"];
}
Id say use these 2 function to slice & merge the arrays. It will get 8 arrays from which are between indexes 0-7 (with 0 and 7 included) and same with the second one. If you want without 0 and 7, it would be with arguments 1, 6, so array_slide($arr, 1, 6); and so on.
$new_arr1 = array_slice($arr, 0, 8);
$new_arr2 = array_slice($arr, 12, 47);
$arr = array_merge($new_arr1, $new_arr2);

php numbers script array loop

I try to write a script and a problem. Can you let me know if you know how i can do this or ask someone if they know how can this be possibe.
Max numbers which can be selected 1 to 20 numbers. it can loop and select any number between 1-20
ignore these numbers e.g. 1,2,4,6,9,12 this will be array which can change.
each array line can have upto 4 numbers
Each array line needs to be unique
5.I need to have around 10 arrays unique
Max 2 numbers can match previous numbers see below.
How can i go about doing this. Any help would be great.
e.g.
Array(
[0] => Array
(
[0] => 3
[1] => 16
[2] => 22
[3] => 24
)
[1] => Array
(
[0] => 3
[1] => 16
[2] => 7
[3] => 13
)
[2] => Array
(
[0] => 20
[1] => 17
[2] => 10
[3] => 18
)
)
This not allow as some array match each other
Array(
[0] => Array
(
[0] => 3
[1] => 16
[2] => 22
[3] => 24
)
[1] => Array - cant have this as 3 of the numbers matchs the previous array.only two numbers can match.
(
[0] => 3
[1] => 16
[2] => 22
[3] => 13
)
[2] => Array
(
[0] => 20
[1] => 17
[2] => 10
[3] => 18
)
)
Thank you.
This seems to satisfy your conditions: http://codepad.viper-7.com/WHkQeD
<?php
$num_arrays = 10; $num_elements = 4;
$min = 1; $max = 20;
$exclude_numbers = array( 1, 4, 6); // Add numbers here to exclude
$answer = array();
for( $i = 0; $i < $num_arrays; $i++)
{
$answer[$i] = array();
for( $j = 0; $j < $num_elements; $j++)
{
do
{
$current = rand( $min, $max);
// If the previous array exists and there are more than two common elements when we add the $current element, continue
if( isset( $answer[$i-1]) && count( array_intersect( $answer[$i-1], array_merge( $answer[$i], array( $current))) > 2)
{
continue;
}
} while( in_array( $current, $exclude_numbers) || in_array( $current, $answer[$i]));
$answer[$i][$j] = $current;
}
}
var_dump( $answer);
Edit: Here is a complete solution that satisfies all of your criteria.
Demo
<?php
$num_arrays = 10; $num_elements = 4;
$min = 1; $max = 20;
$exclude_numbers = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
$answer = array();
for( $i = 0; $i < $num_arrays; $i++)
{
$answer[$i] = array();
for( $j = 0; $j < $num_elements; $j++)
{
do
{
// Get a random element
$current = rand( $min, $max);
$new_array = array_merge( $answer[$i], array( $current));
// If the previous array has more than two common elements (because of the added $current), get a new $current
if( isset( $answer[$i-1]) && count( array_intersect( $answer[$i-1], $new_array)) > 2)
{
$answer[$i] = array_diff( $new_array, $answer[$i-1]);
$j = count( $answer[$i]) - 1;
continue;
}
} while( in_array( $current, $exclude_numbers) || in_array( $current, $answer[$i]));
$answer[$i][$j] = $current;
// If the array is complete, we need to check for unique arrays
if( count( $answer[$i]) == $num_elements)
{
$k = $i - 1;
while( $k >= 0)
{
if( count( array_diff( $answer[$k], $answer[$i])) == 0)
{
// This array is the same as a previous one, start over
$answer[$i] = array();
$j = -1;
break;
}
$k--;
}
// Optionally sort each array
sort( $answer[$i]);
}
}
}
var_dump( $answer);
somthing like:
function generate_list($max, $forbidden)
{
$list = array();
while(count($list) < 4)
{
$new = rand(1, $max);
if(in_array($new, $forbidden))
{
continue;
}
$list[] = $new;
$forbidden[] = $new;
}
return $list;
}
function count_max_same($new_list, $old_lists)
{
$max_same = 0;
foreach($old_lists as $current_list)
{
$max_same = max($max_same, count(array_intersect($new_list, $old_lists)));
}
return $max_same;
}
function generate_unique_lists($count_of_lists, $max, $forbidden, $max_same = 2, $max_tries = 1000)
{
$lists = array();
while($max_tries-- AND count($lists) < $count_of_lists)
{
$new_list = generate_list($max, $forbidden);
if(count_max_same($new_list, $lists) <= $max_same)
{
$lists[] = $new_list;
}
}
return $lists;
}

Categories