Conditions:
Starting digit should not be 0
Succeeding digits should be greater than preceding numbers.
The last digit can be 0
The middle digit should not be 0
We have succeeded in satisfying the first two conditions but because of the contradiction between second and third condition, I am not able to get the expected output.
For example,
The input 1234 gives output:
123
124
134
234
For the digits 12340, the output should be:
123
124
134
234
120
120
140
230
240
340
But it isn't working with what I have done.
The code:
<?
function pc_permute($items, $perms = array( )) {
if (empty($items)) {
$return = array($perms);
} else {
$return = array();
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
$return = array_merge($return, pc_permute($newitems, $newperms));
}
}
return $return;
}
// example
$chars = array('1', '2', '3','4','0');
$output = pc_permute($chars);
$a=count($output);
for ($i = 0; $i<count($output);$i++) {
for ($j = 0; $j < 3;$j++){
$c[$i] = $c[$i].$output[$i][$j];
}
}
$arr = array_unique($c);
$last = end(array_keys($arr));
$n=0;
for($i = 0;$i <= $last;$i++) {
if(!empty($arr[$i])){
$temp = $arr[$i];
$d = str_split($temp);
$e = end(array_keys($d));
$flag = 0;
for($j = 0;$j < (count($d)-1); $j++) {
if(($d[$j] < $d[$j+1] && $d[0] != 0)) {
$flag = 1;
}
else {
$flag = 0;
break;
}
}
if($flag == 1) {
echo $temp;
echo "<br>";
$n++;
}
}
}
?>
You should deviate the following steps for your program based on the rules:
Due to rule 2 you can remove duplicates
Due to rule 2 you can sort the input
Due to rule 1, 3 and 4 you can move 0 to the end of the array, if present
If that is respected you can then iterate over the input array with three foreach loops, that permutate the succeeding numbers with the preceding ones. Because it is sorted in the correct order all rules will be respected automatically.
The comments explain what is done in each step.
$input = [0, 3, 1, 2, 2, 4];
$results = [];
//Remove duplicates, necessary for rule 2
$input = array_unique($input);
//Sort Numbers, necessary for rule 2
sort($input);
// Mark 0 as the greates number, so it can only appear at the end. Necessary for rule 1, 3 and 4
if ($input[0] === 0) {
$input[] = array_shift($input);
}
$inputCount = count($input);
for( $i = 0; $i < $inputCount - 2; $i++) {
for ($j = $i + 1; $j < $inputCount - 1; $j++) {
for ($k = $j + 1; $k < $inputCount; $k++) {
$results[] = $input[$i] . $input[$j] . $input[$k];
}
}
}
foreach ($results as $result) {
echo $result . '<br>';
}
<?php
$count=count($admissions);
$divide=$count/3;
$divide=round($divide);
foreach($admissions as $key => $row)
{
if(//First Part )
{
echo "Alpha";}
else if(//2nd Part )
{
echo "Beta";
}else
{
echo "Gamma";
}
}
?>
I have a dynamic array list and i want to divide it equally in 3 parts.
if Count of array is 30.
So i want to echo for first 10 record
echo "Alpha";
Second 10 Records
Echo "Beta";
3rd 10 Records
Echo "Gamma";
if array size is 60 then it will be divided into 20 parts each.
How can i echo the alpha, beta and gamma.
I thing your question is about if conditions. So you can use this code:
$count=count($admissions);
$divide=$count/3;
$divide=round($divide);
$i = 1;
foreach($admissions as $key => $row)
{
if($i > 0 && $i <= $divide)
{
echo "Alpha";
}
else if($i > $divide && $i <= ($divide*2))
{
echo "Beta";
}
else //equal else if($i > $divide*2 )
{
echo "Gamma";
}
$i++;
}
Try using a normal for loop :
For ($i = 0; $i < $count ; $i++){
echo "alpha";
}
For ($i = $count; $i < 2*$count ; $i++){
echo "beta";
}
For ($i = 2*$count; $i < 3*$count ; $i++){
echo "gamma";
}
Try this hope you are expecting this. According to the requirement which you specified in comments.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$range=range(0,12);
$result=array_chunk($range, 4);
if(count($result[count($result)-1])!=4)
{
$temp=$result[count($result)-1];
unset($result[count($result)-1]);
$result[count($result)-1]=array_merge($result[count($result)-1],$temp);
}
print_r($result);
Let's have an array of three elements and play with the modulo:
<?php
$count=count($admissions);
$divide=$count/3;
$divide=round($divide);
$divisions = array(0 => array(), 1 => array(), 2 => array())
$modulo = 0;
foreach($admissions as $key => $row)
{
$divisions[($modulo + 1) % 3][$key] = $row;
}
This will do the partitioning you need.
I need to find the number of records that are greater than a specific float and find the group of data that repeat the most. For example, I have the data below and I need to find how many entries have values > 4.
1.5
1.7
4.5
4.7
4.8
1.4
4.5
4.9
In the above data the longest continuous repetition of values greater than 4 is 4.5,4.7,4.8. Therefore the total I would like returned should be 3. As you can see the pattern breaks after 4.8 since the number is 1.4 above. Is there a way to identify this pattern?
Try this, I have used here an array:
$arr = array(
0 => '1.5',
1 => '1.7',
2 => '4.5',
3 => '4.7',
4 => '4.8',
5 => '1.4',
6 => '4.5',
7 => '4.9'
);
$chk_val = 4; // value which is checking
$cnt = 0;$inc = 0;
foreach ($arr as $i => $val) {
if ($val > $chk_val) {
$inc++;
if ($inc > $cnt) { $cnt = $inc;}
} else {
$inc = 0;
}
}
echo $cnt;
try this
$n = 4; // number to check
$count = 0;
$max = 0;
$ele = array(1.5, 1.7, 4.5, 4.7, 4.8, 1.4, 4.5, 4.9);
for ($i = 0; $i < count($ele); $i++) {
if ($ele[$i] >= $n) { // check for greater element than given number
$count++; // increase consecutive counter variable
$arr[$max] = $count; //save continues max counter to array
} else {
$count = 0; //reset consecutive counter
$max++;
}
}
echo max($arr);
Quick and dirty...
function findNums($nums, $min = 4) {
$groups = array();
$groupcounts = array();
$groupindex = 0;
foreach($nums as $num) {
if($num > $min) {
$groups[$groupindex][] = $num;
if(array_key_exists($groupindex, $groupcounts)) {
$groupcounts[$groupindex]++;
} else {
$groupcounts[$groupindex] = 1;
}
} else {
$groupindex++;
}
}
return array($groupcounts, $groups);
}
// $your_numbers is your list
$nums = array_map('trim', explode("\n", $your_numbers));
$result = findNums($nums);
$counts = $result[0];
$maxcount = max($counts);
$groups = $result[1];
echo "max count is ".$maxcount." with values:\n";
$key = array_search($maxcount, $counts);
var_dump($groups[$key]);
I'm new on PHP and I want to find the 0 and replace with the number that is missed, inside the inner array, on a multidimensional array. If the inner array has more than two 0's, it will be ignored and goes to the next.
$list = array("First"=>array(0,1,2,3,0,5,6,7,8,9),
"Second"=>array(0,1,2,3,4,5,6,7,8,9),
"Third"=>array(0,1,2,3,4,5,0,0,8,9),
"Fourth"=>array(0,1,2,3,4,5,6,7,8,0),
"Fifth"=>array(0,1,2,3,4,5,0,7,8,9),
"Sixth"=>array(0,0,0,3,4,5,6,0,0,0),
"Seventh"=>array(0,1,2,3,0,0,6,7,8,9),
"Eighth"=>array(0,1,2,3,4,5,0,7,8,9),
"Ninth"=>array(0,1,2,3,4,0,6,7,8,9),
"Tenth"=>array(0,0,2,3,4,5,6,7,8,9));
$countZero = 0;
foreach($list as $lvl) {
foreach($lvl as $ind => $val) {
if($countZero = array_count_values($lvl[$val] === 0))
$list[$ind][$val] = 45 - array_sum($ind);
echo $count;
}
}
I want all inner arrays, that have two 0's get only one, to have all numbers in sequence i.e.
"First"=>array(0,1,2,3,4,5,6,7,8,9);
Please, help me.
I tried this code below, trying to finde the 0's.
$counts = 0;
$newArr = array();
foreach($list as $lvl) {
if(is_array($lvl)) {
for($i = 0; $i < count($lvl) - 1; $i++) {
if(($lvl[$i] == 0) < 2){
$counts++;
$newArr[$i] = 45 - array_sum($lvl);
}
}
}
}
print_r($newArr);
This is a solution using array_walk:
array_walk($list,
function(&$numbers) {
$zeroIndex = 0;
foreach($numbers as $i => $number) {
if( $number === 0 ) {
if( $zeroIndex > 0 ) {
return;
}
$zeroIndex = $i;
}
}
$numbers[$zeroIndex] = $zeroIndex;
});
You don't need to count all the zeros. You just need to check if there are less than 3 zeros.
I'm saving the index (position) of zero ($zeroIndex = $i).
I'm assuming that the first number is always a zero ($zeroIndex = 0).
The index of the second zero is greater than zero. If I find a zero when the index of the last found zero is greater than zero (if( $zeroIndex > 0 )), this means that there are more than two zeros.
In fact,here is what I've done and worked.
$list = array(array(1,2,3,0,5,6,7,8,9),
array(1,2,3,4,5,6,7,8,9),
array(1,2,3,4,5,0,0,8,9),
array(1,2,3,4,5,6,7,8,0),
array(1,2,3,4,5,0,7,8,9),
array(0,0,3,4,5,6,0,0,0),
array(1,2,3,0,0,6,7,8,9),
array(1,2,3,4,5,0,7,8,9),
array(1,2,3,4,0,6,7,8,9));
for($l = 0; $l < count($list); $l++)
{
$total = 0;
$countZ = 0;
for($i=0; $i < 9; $i++)
{
if($list[$l][$i] == 0)
{
$countZ++;
$indexZero = $i;
}
$total += $list[$l][$i];
if($countZ > 1) {
break;
}
}
$list[$l][$indexZero] = 45 - $total;
}
print_r($list);
TY all.