Jump to content
php.lv forumi

Sadalit grupas (intervala)


cucumber

Recommended Posts

Isti pareizi nevaru izdomat, ka var sadalit skaitlus grupas (intervalos), pienemsim pa 5. Sanak ka skaitli tiek iedaliti piecas grupas, ja skaitlu pietrukst, lai sadalitu grupa, tad shi grupa ar skaitliem paliek tuksha. Grupa 5 ir skaitli vislielako skaitli utt, kamer grupa 1 ir mazaki skaitli no kopejiem skaitliem.

 

 

 

//TEST 1
$numbers = array(11,2,3,6,12,7,3,43,12);
$min = min($numbers);
$max = max($numbers);
$div = ceil(($min+$max+1)/5);

foreach ($numbers as $number)
   echo ceil(($number)/$div)."\n"; //2 1 1 1 2 1 1 5 2

//TEST 2    
$numbers = array(11,2);
$min = min($numbers);
$max = max($numbers);
$div = ceil(($min+$max+1)/5);     

foreach ($numbers as $number)
   echo ceil(($number)/$div)."\n"; //4 1 te it ka vajadzetu but 5 un 1

//TEST 3
//kaut kas uz to pusi bet ari nav tas
$numbers = array(11,2,3,6,12,7,3,43,12);
sort($numbers);
print_r(array_chunk($numbers, 5));

Link to comment
Share on other sites

Es laikam rīkotos kā Tavā 3. piemērā:

$cik_grupa = 5;
$numbers = array(11,2,3,6,12,7,3,43,12);
sort($numbers);
$cik_grupas_sanak = floor(sizeof($numbers)/$cik_grupa)
$grupas = array();
for($x=0;$x<$cik_grupa_sanak;$x++){
$grupas[] = array_slice($numbers,$x * $cik_grupa,$cik_grupa);
}
print_r($grupas);

Link to comment
Share on other sites

Tava piemera bik kludas.

 

Es laikam rīkotos kā Tavā 3. piemērā:

 

 

Sanak atpakal es dabuju 5 grupas, bet katra ir pa vienam elemetam, bet vajadzetu visus elemtus sadalit pa attiecigam grupam.

 

//izlabotas
$cik_grupa = 5;
$numbers = array(11,2,3,6,12,7,3,43,12);
sort($numbers);
$cik_grupas_sanak = floor(sizeof($numbers)/$cik_grupa);
$grupas = array();
for($x=0;$x<$cik_grupas_sanak;$x++){
$grupas[] = array_slice($numbers,$x * $cik_grupa,$cik_grupa);
}
print_r($grupas);

Link to comment
Share on other sites

Ideja ir tada, ka var but ari tuksha ta grupa, vinai pilnai nav jabut vienmer.

nu itkā array_slice vajadzēja atgriezt $cik_grupa garus apakšmasīvus. Un apakšmasīvam šajā piemērā būtu jābūt 1, jo masīva garums ir 9 - tātad ieiet tikai 1 pilna apakšgrupa.

Link to comment
Share on other sites

1. grupa

2,3,3

2. grupa

6,7,11,12

3.grupa

-

4. grupa

-

5. grupa

43

 

Ok - īsti nesaprotu sāpi...

Lūdzu uzraksti, kā jāizskatās šīm grupām pie masīva:

$numbers = array(11,2,3,6,12,7,3,43,12);

 

Tads vel piemers. Ja butu skaitli 1,2,3,4,5, tad grupas butu 1.gr - 1 , 2.gr - 2 .. 5.gr - 5

 

 

Es to iedomajos, ka skaitlu virkni augosha seciba, kas ir sadalita piecas dala ar vienotu vertibu intervalu.

Link to comment
Share on other sites

1. grupa

2,3,3

2. grupa

6,7,11,12

3.grupa

-

4. grupa

-

5. grupa

43

 

Kālab otrajā grupā ir 6 un 7? Vai tad intervālu no 2 līd 43, ieskaitot, sadalot 5 apakšintervālos tie nav [2..10], [11..19], [20..28], [29..37], [38..43(46)]?

Link to comment
Share on other sites

Jap laacz, tev bus taisniba

Kālab otrajā grupā ir 6 un 7? Vai tad intervālu no 2 līd 43, ieskaitot, sadalot 5 apakšintervālos tie nav [2..10], [11..19], [20..28], [29..37], [38..43(46)]?

Link to comment
Share on other sites

Jap patesteju ari uz saviem piemeriem, it ka strada ok.

Paldies.

Elementāri, Mendelson (ja tur nav kļūdu, protams):

 

 

Radas vel viena ideja, pameginashu rit realizet.

Link to comment
Share on other sites

Sanak mazak lietot, f-jas un dalishanu, strada aptuveni tapatas.

 

$data = array(11,2,3,6,12,7,3,43,12);
$max = max($data);
while ($max % 5 != 0 ) {
   $max++;
}  

$step = $max / 5;

foreach($data as $value) {
   if ($value >= $step * 0 && $value <= $step * 1 ) { $result[1][] = $value;  continue;}
   if ($value >= $step * 1 && $value <= $step * 2 ) { $result[2][] = $value;  continue;}
   if ($value >= $step * 2 && $value <= $step * 3 ) { $result[3][] = $value;  continue;}
   if ($value >= $step * 3 && $value <= $step * 4 ) { $result[4][] = $value;  continue;}
   if ($value >= $step * 4 && $value <= $step * 5 ) { $result[5][] = $value;  continue;}
}

print_r($result);

Link to comment
Share on other sites

Man kaut ka neliekas korekti, pienemsim ka max ir skaitlis ir 3, tad

 

3+4 = 7 / 5 = 1,4 * 5 = 7 //bezjega sanak dalit un tad atpakal reizinat?

 

 

To var uzrakstīt arī bez while cikla: $max = ((($max + 4)/5)*5;

Edited by cucumber
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...