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";
Related
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.
Given the following array:
$arr = array(0,0,1,2,2,5,6,7,7,9,10,10);
And assuming $n = 2, what is the most efficient way to get a count of each value in the array within $n of each value?
For example, 6 has 3 other values within $n: 5,7,7.
Ultimately I'd like a corresponding array with simply the counts within $n, like so:
// 0,0,1,2,2,5,6,7,7,9,10,10 // $arr, so you can see it lined up
$count_arr = array(4,4,4,4,4,3,3,4,4,4, 2, 2);
Is a simple foreach loop the way to go? CodePad Link
$arr = array(0,0,1,2,2,5,6,7,7,9,10,10);
$n = 2;
$count_arr = array();
foreach ($arr as $v) {
$range = range(($v-$n),($v+$n)); // simple range between lower and upper bound
$count = count(array_intersect($arr,$range)); // count intersect array
$count_arr[] = $count-1; // subtract 1 so you don't count itself
}
print_r($arr);
print_r($count_arr);
My last answer was written without fully groking the problem...
Try sorting the array, before processing it, and leverage that when you run through it. This has a better runtime complexity.
$arr = array(0,0,1,2,2,5,6,7,7,9,10,10);
asort($arr);
$n = 2;
$cnt = count($arr);
$counts = array_pad(array(), $cnt, 0);
for ($x=0; $x<$cnt; $x++) {
$low = $x - 1;
$lower_range_bound = $arr[$x]-$n;
while($low >= 0 && ($arr[$low] >= $lower_range_bound)) {
$counts[$x]++;
$low--;
}
$high = $x + 1;
$upper_range_bound = $arr[$x]+$n;
while($high < $cnt && $arr[$high] <= $upper_range_bound) {
$counts[$x]++;
$high++;
}
}
print_r($arr);
print_r($counts);
Play with it here: http://codepad.org/JXlZNCxW
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.
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.
I want to generate test data for testing. What I need:
I will have a list of 30 names. For each name I need to fill in the start and finish position. The position is between 1 and 30.
So I wanted to use RAND() in php but I have a problem what I can't find a good way for.
Every position can only be used once. So if I press on a button "generate data" I want for all 30 names a start and finish position without duplicate position. So in the start i will use 1 - 30 and for the finish 1 - 30 as well.
I need this data in textfield. Is there a simple function for this? Or do you know how I can do this without having to fill in 60 textfields every test.
Something like this might get you started:
$names = array(/*...*/);
$l = count($names);
$rand = array();
for ($i = 0; $i<$l; $i++) {
$rand[] = $i;
}
shuffle($rand);
for ($i = 0; $i < $l; $i++) {
$names[$i]['start'] = $rand[$i];
}
Or:
$names = array(/*...*/);
shuffle($names);
for ($i = 0, $l = count($names); $i < $l; $i++) {
$names[$i]['start'] = $i;
}
I will use shuffle (twice),
something like
$arr = range(1, 30);
$first_numbers_position = shuffle($arr);
$second_numbers_position = shuffle($arr);
if you have already array with the names
you can use this function
Very simple shuffle function preserving key with value
function shuffle_assoc( $array )
{
$keys = array_keys( $array );
shuffle( $keys );
return array_merge( array_flip( $keys ) , $array );
}
If you don't need your code to actually generate the data dynamically you could try something like this site http://www.generatedata.com/