Jump to content
jonnyenglish89

array challenge help

Recommended Posts

Hi guys,

 

I’d appreciate a little guidance but I’m gonna struggle to explain what the problem is first… so apologies in advance.

$drawOrder is an array of 1 to 10 letters that are drawn at random

Array

(

    [101] => C

    [102] => F

    [103] => D

    [104] => J

    [105] => B

    [106] => H

    [107] => I

    [108] => G

    [109] => E

    [110] => A

)

 

$bestOrder contains the same values as $drawOrder but the letters are given a priority.

Array

(

    [0] => A

    [1] => B

    [2] => C

    [3] => D

    [4] => E

    [5] => F

    [6] => G

    [7] => H

    [8] => I

    [9] => J

)

$bestPossibleOrder – I want this to contain the closest order possible to $bestOrder based on the draw order and 3 letter draw limit 

Array

(

    [101] => C

    [103] => D

    [105] => B

    [102] => F

    [106] => H

    [108] => G

    [109] => E

    [110] => A

    [107] => I

    [104] => J

)

$availableLetters contains 3 letters that are drawn according to $drawOrder.

 

Array

(

    [101] => C

    [102] => F

    [103] => D

)

 

 

add “[101] => C” to $bestPossibleOrder array

remove “[2] => C” from the $bestOrder array because the letters are not always unique

add “[104] => J “ to $availableLetters because it is next in the draw order.

 

Array

(

    [102] => F

    [103] => D

    [104] => J

)

 

add “[103] => D” to my $bestPossibleOrder array

remove “[103] => D” from the $bestOrder array because the letters are not always unique

add “[102] => B“ to $availableLetters because it is next in the draw order.

 

Array

(

    [102] => F

    [104] => J

    [102] => B

)

 

 

Repeat the process until all letters are in the $bestPossibleOrder array

 

 

Array

(

    [101] => C

    [103] => D

    [105] => B

    [102] => F

    [106] => H

    [108] => G

    [109] => E

    [110] => A

    [107] => I

    [104] => J

)

 

I’ve attached what I’ve written so far but I’m not sure if it’s a good approach

<?php

$BR = "<br />";



echo "best order:" . $BR;

$bestOrder = array("A","B","C","D","E","F","G","H","I","J");
print ("<pre>" . print_r($bestOrder, true) . "</pre>");

echo "draw order:" . $BR;

$drawOrder = shuffle_assoc($bestOrder);
$drawOrder = array_combine(range(101,(count($bestOrder)+100)),$drawOrder);// $drawOrder array keys have to start from 101
print ("<pre>" . print_r($drawOrder, true) . "</pre>");




echo "available Letters (FIRST DRAW):" . $BR;

$availableLetters = array_slice($drawOrder, 0, 3,true);
print ("<pre>" . print_r($availableLetters, true) . "</pre>");



$numberToPick = selectNextNumber($bestOrder,$availableLetters);
echo "The key with the closest letter to best order is [" . $numberToPick . "]";



function selectNextNumber($bestOrder,$availableLetters){
	for ($x = 0; $x <= count($bestOrder); $x++){
	$numberToPick = array_search($bestOrder[$x], $availableLetters);
		if ($numberToPick !== false) {
			//unset($bestOrder[$x]);
			//print ("<pre>" . print_r($bestOrder, true) . "</pre>");
			return $numberToPick;
		} 
	}
}

function shuffle_assoc($list) { 
  if (!is_array($list)) return $list; 

  $keys = array_keys($list); 
  shuffle($keys); 
  $random = array(); 
  foreach ($keys as $key) { 
    $random[$key] = $list[$key]; 
  }
  return $random; 
}

 

 

 

 

Share this post


Link to post
Share on other sites

Here's the English explanation of your problem, not counting the apparent importance of array keys:

Quote

 

1. Have an array of letters sorted by priority which may have duplicates: [A, B, C, D, E, F, G, H, I, J]
2. Have an array of letters in a random order which may also have duplicates: [C, F, D, J, B, H, I, G, E, A]
3. Have an empty array to put letters in: []
4. Draw 3 random letters: [C, F, D]

While there are random letters,
5a. Add the one with the highest priority to the array: [C]
5b. Remove (the first instance of) that letter from the sorted array: [A, B, D, E, F, G, H, I, J]
5c. Draw a new random letter: [F, D, J]


Random | Array               | Sorted
-------+---------------------+--------------------
C F D  |                     | A B C D E F G H I J
F D J  | C                   | A B D E F G H I J
F J B  | C D                 | A B E F G H I J
F J H  | C D B               | A E F G H I J
J H I  | C D B F             | A E G H I J
J I G  | C D B F H           | A E G I J
J I E  | C D B F H G         | A E I J
J I A  | C D B F H G E       | A I J
J I    | C D B F H G E A     | I J
J      | C D B F H G E A I   | J
       | C D B F H G E A I J |

When out of letters,
6. Show the final array: [C, D, B, F, H, G, E, A, I, J]

The code seems functional so far. Any particular problems? Otherwise keep going and see what happens.

    Share this post


    Link to post
    Share on other sites

    Thanks for the translation :)

    I could do with some help with 5b.

    I can remove the first instance of of the selected letter within the function  selectNextNumber with unset($bestOrder[$x]) and return the updated $bestOrder array and $numberToPick

    is that a good way to do it or would you recommend somethingelse?

     

    Share this post


    Link to post
    Share on other sites

    selectNextNumber() will tell you an array key in $bestOrder, right? You can use that not just to determine the letter but also to then remove it from $bestOrder before the next call.

    Share this post


    Link to post
    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.   Restore formatting

      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.


    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.