How to know the possible sets of n numbers? - php

I have a set of numbers S = [1,2,3,4,5,6]; And I want to make all possible pairs among them but one thing to know the pair could not be repeat.
i have used this :-
$pairs = array('1','2','3','4','5','6');
$count = count($pairs);
$array = array();
for($i = 0;$i <= $count; $i++){
for($j = 1; $j < $count; $j++){
if($i < $j){
$array[$i][] = $pairs[$i].','.$pairs[$j];
}
}
}
I want some thing like this :-
S = [1,2,3,4,5,6];
[1,2],[1,3],[1,4],[1,5],[1,6]
[2,3],[2,4],[2,5],[2,6]
[3,4],[3,5],[3,6]
[4,5],[4,6]
[5,6]
If anyone have any better suggestion please do reply as soon as possible.

Build a double nested foreach loop over your array, build your pairs and check if you already got them.
Pseudocode:
pairs = []
foreach(entry in array) {
foreach(entry2 in array) {
pair = [entry, entry2]
if(!alreadyexists(pair in pairs) {
add(pair to pairs)
}
}
}
havefun()
Btw: You won't get hurt if you try something yourself, this problem is rather trivial.

Related

php implementation of finding cycle in minimum swaps

find out minimum number of adjacent swaps require to make the array sorted.
the approach I use is finding a repeated cycle within my array for example 3->1->4->2 is cycle of 4 so minimum swap of 3 with input of [3,1,4,2,5] to make it sorted [1,2,3,4,5]
I have three arrays, $old contains unsorted array, $arr is sorted, and $bool counts whether the position is visited or not
$old = $arr;
quicksort($arr, 0, count($arr) - 1);
$bool = array_fill(0, count($arr), false);
$count = 0;
$circle = 0;
for ($i = 0; $i < count($bool); $i++){
if ($arr[$i] !== $old[$i]) {
$circle ++;
$where = array_search($old[$i], $arr);
$bool[$i] = true;
if ($bool[$where]) {
$count += $circle;
$circle = 0;
}
}
}
return $count;
the idea is skipping element that is already in right index,
and mark element that is not, keeping count of cycle (i used $circle), once a right position is already marked, i conclude that a cycle is reached,
but this logic is not behaving what I expected, need some help.
I think what you are after is something like this, however I might be wrong.
I tried to keep it simple with a lot of loops to make it work, and explained the different steps in the code
<?php
$minSize = 3;
$arr = [3,1,2,6,9,3,5,2,6,9,3,5,1,8,4,6,6,9,3,0];
$repeats = [];
//outer loop
for($i = 0; $i < count($arr); $i++) {
$currValue = $arr[$i];
//get all the keys for where the value is the same as $currValue
//this gives a starting position to test our matching sets
$matches = array_keys($arr, $currValue);
//match loop
for($j = 0; $j < count($matches); $j++) {
$leftIndex = $currValue; //this needs to be reset for every set
$matchIndex = $matches[$j];
//now we do want matches only when the index is different, always it will always match
if($leftIndex === $matchIndex) {
continue;
}
$subsequentMatch = 0;
//isset so we don't go out of bounds of our array
while(isset($arr[$leftIndex], $arr[$matchIndex]) && $arr[$leftIndex] === $arr[$matchIndex]) {
$subsequentMatch++;
$leftIndex++;
$matchIndex++;
}
if($subsequentMatch >= $minSize) {
$repeats[] = array_slice($arr, $currValue, $subsequentMatch) ;
}
}
}
print_r($repeats);
See an example here : https://3v4l.org/cBWQ5
you could improve on this by doing
for($i = 0; $i < (count($arr) - $minSize ); $i++) {
because if the set is under $minSize it will never be added to the repeats
EDIT: based on comments, OP might be looking for this ?
<?php
$arr = [3,1,2,6,9,3,5,2,6,9,3,5,1,8,4,6,6,9,3,0];
$swapCount = 0;
usort($arr, function ($a, $b) use (&$swapCount) {
if($a === $b) {
return 0;
}
$swapCount++;
return $a <=> $b;
});
echo 'Arr Count: ' . count($arr) . PHP_EOL;
echo 'Swaps performed: ' . $swapCount;
This is a simple sort, I suppose you could write a usort and count with each different sort mechanism (the list is endless), and then find out the min value of that
list of sorting mechanism off the top of my head
Insert Sort
Bubble Sort
Quick Sort
Counting Sort
Comb Sort
Heap Sort
Merge Sort
Shell Sort
Odd Even Sort
You can google on how to implement these in PHP, there are probably more ways too.

Using an index twice in PHP

I am trying to use a for loop where it looks through an array and tries to make sure the same element is not used twice. For example, if $r or the random variable is assigned the number "3", my final array list will find the value associated with wordList[3] and add it. When the loop runs again, I don't want $r to use 3 again. Example output: 122234, where I would want something along the lines of 132456. Thanks in advance for the help.
for($i = 0; $i < $numWords; $i++){
$r = rand(0, $numWords);
$arrayTrack[$i] == $r;
$wordList[$r] = $finalArray[$i];
for($j = 0; $j <= $i; $j++){
if($arrayTrack[$j] == $r){
# Not sure what to do here. If $r is 9 once, I do not want it to be 9 again.
# I wrote this so that $r will never repeat itself
break;
}
}
Edited for clarity.
Pretty sure you are over complicating things. Try this, using array_rand():
$final_array = array();
$rand_keys = array_rand($wordList, $numWords);
foreach ($rand_keys as $key) {
$final_array[] = $wordList[$key];
}
If $numWords is 9, this will give you 9 random, unique elements from $wordList.
See demo
$range = range(0, $numWords - 1); // may be without -1, it depends..
shuffle($range);
for($i = 0; $i < $numWords; $i++) {
$r = array_pop($range);
$wordList[$r] = $finalArray[$i];
}
I do not know why you want it.. may be it is easier to shuffle($finalArray);??
So ideally "abcdefghi" in some random order.
$letters = str_split('abcdefghi');
shuffle($letters);
var_dump($letters);
ps: if you have hardcoded array $wordList and you want to take first $n elements of it and shuffle then (if this is not an associative array and you do not care about the keys)
$newArray = array_slice($wordList, 0, $n);
shuffle($newArray);
var_dump($newArray);
You can try array_rand and unset
For example:
$array = array('one','two','free','four','five');
$count = count($array);
for($i=0;$i<$count;$i++)
{
$b = array_rand($array);
echo $array[$b].'<br />';
unset($array[$b]);
}
after you have brought the data in the array, you purify and simultaneously removing the memory array
Ok... I have NO idea why you are trying to use so many variables with this.
I certainly, have no clue what you were using $arrayTrack for.
There is a very good chance I am mis-understanding all of this though.
<?php
$numWords=10;
$wordList=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$finalArray=array();
for ($i=0; $i<$numWords; $i++) {
start:
$r=rand(0,$numWords);
$wordChoice=$wordList[$r];
foreach ($finalArray as $word) {
if ($word==$wordChoice) goto start;
}
$finalArray[]=$wordChoice;
}
echo "Result: ".implode(',',$finalArray)."\n";

slice the array with pair and without pair in php

The array values are:
$input_array = array( "Student_1","Student_2","Student_3","Student_4","student_5","Student_6","Student_7","Student_8" );
Now, I need the output as:
$array = array( array('Student_1', 'Student_2'), array('Student_3', 'Student_4'),
array('Student_5', 'Student_6'), array('Student_7'), array('Student_8') );
$input_array = array("Student_1","Student_2","Student_3","Student_4","student_5","Student_6","Student_7","Student_8");
function sliceArray($arr, $pairs = 3){
$rtnArr = array();
$total = count($arr);
if($total < ($pairs * 2)) return 'Error: Invalid number of pairs!';
for($i=0; $i<$total; $i++){
if(count($rtnArr) < $pairs && isset($arr[$i+1])){ $rtnArr[] = array($arr[$i], $arr[$i+1]); $i++; }
else $rtnArr[] = array($arr[$i]);
}
return $rtnArr;
}
var_dump(sliceArray($input_array));
You can change the $pair value to get required number of pairs.
Since you didnt specify your formatting or output rules, I'll just assume the rules myself for this case.
I assume you want your array to take two inputs each from the first and pair them
array('Student_1', 'Student_2'),
But the last two are not paired
array('Student_7'), array('Student_8')
So i assume you want the last two to be seperate.
Now for the solution
NOTE: This only works for even arrays for the moment. Working on a more general answer now.
// We make a new array which takes every two elements from the first and appends to itself
for($i=0; $i < count($input_array)-2; $i=$i+2)
{
$array_output []= array($input_array[$i], $input_array[$i+1]); //append the teo elements
}
// Now add the final two elements to the array
$array_output []= array($input_array[count($input_array)-2]);
$array_output []= array($input_array[count($input_array)-1]);
//Output here
echo "<pre>";
print_r($array_temp);
echo "</pre>";
In case if you are new to php
$array []= "something"; // will append to the array
If you didn't want the last two to be seperate (assuming you made a typo),
change
for($i=0; $i < count($input_array)-2; $i=$i+2)
to
for($i=0; $i <= count($input_array); $i=$i+2)
and remove
/*
$array_output []= array($input_array[count($input_array)-2]);
$array_output []= array($input_array[count($input_array)-1]);
*/

How to sort it better?

I have 2 arrays, both are multidimensional with same number of elements and same values, which are on different positions (those values are actually ID-s from my database, so one ID appears only once). How can I sort second array with values which are in first array?
For example - if first array looks like:
$array1[0][0] = 1;
$array1[0][x] = it doesn't matter what's here
$array1[1][0] = 4;
$array1[1][x] = it doesn't matter what's here
$array1[2][0] = 3;
$array1[2][x] = it doesn't matter what's here
...
how to sort second array so it would have same values as array1 on indexes [0][0], [1][0], [2][0], etc.
How I could solve problem is:
$i=0
while ($i < (count($array1)-2)){ // * check down
$find_id = $array1[$i][0];
// here I need to search for index of that ID in other array
$position = give_index($find_id, $array2);
// swapping positions
$temp = array2[$i][0];
$array2[$i][0] = $array2[$position][0];
$array2[$position][0] = $temp;
// increasing counter
i++;
}
function give_index($needle, $haystack){
for ($j = 0, $l = count($haystack); $j < $l; ++$j) {
if (in_array($needle, $haystack[$j][0])) return $j;
}
return false;
}
*There is only -2 because indexes start from 0 and also for the last element you don't need to check since it would be automatically sorted by last iteration of while-loop.
I don't find this solution good as I think that this is quite simple issue (maybe it's not even correct). Is there easier way in PHP that I'm missing?
This is the most efficient way I can think of:
function swap(&$a, &$b) {
$t = $a;
$a = $b;
$b = $t;
}
function find_index($id, $array, $from = 0) {
$index = false;
for ($i = $from, $c = count($array); $i < $c; $i++) {
if ($array[$i][0] == $id) {
$index = $i;
break;
}
}
return $index;
}
for ($i = 0, $c = count($array1); $i < ($c - 2); $i++) {
if ($array1[$i][0] != $array2[$i][0]) {
$fi = find_index($array1[$i][0], $array2, $i);
swap($array2[$i][0], $array2[$fi][0]);
}
}
What changes from yours?
I've defined a swap() function in order to swap any variable. That doesn't cost anything and makes everything look nicer. Also you can reuse that function later if you need to.
In the find_index (give_index in your code) we stop the loop once we find the correct index. Also we avoid the cost of an in_array function call.
We modified the find_index function to start only from the part of the array we haven't checked yet. Leading to a way more efficient way of scan the array.
In the for loop (a while loop was just wrong there) we stored the count of the array once, avoiding multiple calls.
Also we swap the $array2 values only if they are in the wrong place.
Other improvements
If you know anything else of the $array2 array you can make this even more performant. For example if you know that indexes are alternated like in $array1 you can change the main for loop from:
for ($i = 0, $c = count($array1); $i < ($c - 2); $i++) {
to
for ($i = 0, $c = count($array1); $i < ($c - 2); $i+2) {
(notice the $i+2 at the end) And you could do that in the find_index function as well.
Look into usort (http://php.net/manual/en/function.usort.php).
It provides a simple way to sort arrays using a user provided comparison function.

How do I create a parameter list for a function?

I am creating an array of arrays in the following way:
$final_array = array();
for($i = 0; $i < count($elements); $i++) {
for($j = 0; $j < count($elements); $j++) {
if($i!=$j)
$final_array[] = array_intersect($elements[$i], $elements[$j]);
}
}
I am trying to find out the list of elements that occur in all the arrays inside the $final_array variable. So I was wondering how to pass this to array_intersect function. Can someone tell me how to construct args using $final_array[0], $final_array[1], ... $final_array[end_value] for array_intersect? Or if there is a better approach for this, that would be great too.
I am looking for a way to construct the following:
array_intersect($final_array[0], $final_array[1], $final_array[2], ...)
Why do you need to do all of this work? Just use call_user_func_array.

Categories