rearrange positive and negative numbers php [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
An array has negative and positive numbers. Separate the numbers such that negative numbers are at the beginning and positive numbers at the end without changing the order.
Example:
Array = {1, -3, -5, 9 , -8}
O/P = {-3, -5, -8, 1, 9}
I found many answer in c , c++ , java but not in PHP , so can any one please let me know how or better way to achieve this?
However this question put on hold , i tried with some of solutions and
find my own answer added below , hope it might be useful for someone.
function part($arr){
$j = 0;
for($i=0;$i<count($arr);$i++){
$val = $arr[$i];
$k = $i;
while($k>$j && $val < 0){
$arr[$k] = $arr[$k-1];
$k = $k-1;
if($j==$k){
$j=$j+1;
}
$arr[$k] = $val;
}
}
return $arr;
}
$arr = array(1, -3, -5, 9 , -8);
print_r(part($arr));

Without doing all the work for you.
Look into array_filter you could filter the negative values into 1 array, then filter the positive values into another array.
Then use array_merge to merge the 2.

Related

How do i get the length of a string except a character in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a string called line and it has 5 characters.
line = 1,2,3
the strlen() function gave me the answer as 5. but i need to count the number of characters except ','. how should i do it ?
You need to use explode
$values = '1, 2, 3, 4, 5, 6'; // Your values in array
$str_array = explode(',', $values); //Explode string or numbers between commas in array
$size = count($str_array);// count string or numbers in array
echo $size; //Get total
To get total sum of numbers in array :
$numbers = array(1, 2, 3, 4, 5, 6);
print_r(array_sum($numbers));
use below code
$line = "1,2,3";
echo strlen(str_replace(',', '', $line));
Fir replace , with '' and then count.
$line = "1,2,3";
$new_line = str_replace(',', '', $line);
echo strlen($new_line);

php string with all space changing [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have string like this : hello world new foo.
I need to have array like this:
[
`helloworld new foo`,
`hello worldnew foo`,
`hello world newfoo`,
`helloworldnew foo`,
`hello worldnewfoo`,
]
order doesn't matter but i need to have all case that space will be remove in current string.
You can use recursion to enumerate all the posibilities:
<?php
function combine($words, $acc=array(), $res=array()){
if(count($words)===0){
$res[] = join("", $acc);
return $res;
}
if(count($words)===1){
$res[] = join("", $acc).$words[0];
return $res;
}
$w1 = array_shift($words);
$res = combine($words, array_merge($acc, array("$w1")), $res);
$res = combine($words, array_merge($acc, array("$w1 ")), $res);
return $res;
}
var_dump( combine( explode(' ', 'hello world new foo') ) );
Another possible solution is to represent the N spaces betweeen your words, as bits in a binary number that can be on or off, and then count from 0 to 2^N-1, but I think that in PHP that will be more complex.
NB: the above recursive solution, returns all possible combinations ... so if you have an input array with 4 words, one of the results will have all 4 words joined.
From what i understood you need all posible combinations with the words given. so:
posible combinations = amountofwords*amountofspaces.
start iteration for amout of posible cominations. -> for(i=0;i<=(words*spaces);i++)
have words in array and spaces found in String so $WordArray = $string.split(" ") and $spaces = substr_count(" ")
start iteration for posible word combinations. for(j=0;j<=words;j++)
start iteration for amount of spaces. for(k=0;k<=spaces;++)
combine all.
but keep in mind that PERMUTATIONS and COMBINATIONS for computer science is what you need to learn first, so the answer above has a lot of sence.
here is a link to get you started.
http://cpsc.ualr.edu/srini/DM/chapters/review3.4.html

Keep order in array, but change the values [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Let´s say I´ve got an array like [3,2,8,4] (just an example, it can have more or less values).
I want the numbers to be in the same order but instead use numbers 1-4 (if there are 4 values as in this example), ie. [2,1,4,3].
How can I accomplish this?
$data = [3,2,8,4];
$keys = array_keys($data);
array_multisort($data, SORT_ASC, $keys);
array_walk($keys, function(&$value) { ++$value; });
var_dump($keys);
,You could go with ArrayReplace.
<?php
$base = array(3 2, 8, 4);
$replacements = array(0 => 2, 1 => 1, 2 => 4, 3 => 3);
$store = array_replace($base, $replacements);
?>

Regular expression php for a pro [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Can you help me with a regular expression for this:
$value = '["number"]';
or
$value = '["number","number"]';
or
$value = '["number","number","number"]';
or
...
...
...
...
$value = '["number","number","number","number","number","number","number"......,"number"]';
//$number can be rand(0, 99.....999);
I need the result to be validated as true or false!
Here is my version:
function test($v)
{
if (preg_match('/^\\[("number")(,"number")*\\]$/', $v))
echo 'ok<br>';
else
echo 'fail<br>';
}
or if "number" is really digits, this one:
function test($v)
{
if (preg_match('/^\\[("[0-9]+")(,"[0-9]+")*\\]$/', $v))
echo 'ok<br>';
else
echo 'fail<br>';
}
NOTE - only positive naturals are accepted, need to change to negative and decimal/floating numbers
Do you really need to use PCRE? Your example is valid json.
$array = json_decode('["number", "number", "123"]');
var_dump($array);
If you need numbers only, you can filter it.
$new_array = array_filter($array, 'ctype_digit');
$result = count($array) == $new_array? $new_array : null;
var_dump($result);

insert array of new item in any position in another array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How can I add new item to array? For example into middle of an array ?Should we use array splice or an array merge ?
Could you explain me the difference between both function ?
Say I have
$a1=array("a"=>"Horse","b"=>"Dog","c"=>"Cow",);
$a2=array("d"=>"Cat");
Now I need to add $a2 in 2 position .
Which one should I use ?
You can use array_splice, except that won't keep your keys.
$a1 = array("a"=>"Horse", "b"=>"Dog", "c"=>"Cow");
$a2 = array("d"=>"Cat");
array_splice($a1, 2, 0, $a2);
// $a1 is now: array("a"=>"Horse", "b"=>"Dog", 0=>"Cat", "c"=>"Cow");
If you want Cat to have a key of d, you can use a mix of array_slice and the array union operator (+):
$a1 = array_slice($a1, 0, 2) + $a2 + array_slice($a1, 2);
// $a1 is now: array("a"=>"Horse", "b"=>"Dog", "d"=>"Cat", "c"=>"Cow");
you can use array_push to add array at any position. ..array_splice can also be used. .
example: array_splice

Categories