Jump to content

I have some struggle with a php task..


Go to solution Solved by Viktor9769,

Recommended Posts

This is the task.

 * Adam has been given a bag of candies from his mother.
 * His mother told him to share half of it with his sister Berit.
 * Adam likes diversity so he wants as many different types of candies as possible, while giving the rest to Berit.
 * If Adam has received an odd number of candies he can keep one more than he gives to Berit.
 *
 * The exercise is to sort through the candies Adam has been given and return the candies that he should give to Berit.
 */
/*
 * Several different test cases, add one below if you like.
 * Each value correspond to a different type of candy.
 *
 * The values after => is the values that should be returned for each test case.
 */
/*$candiesTests = [
    [1, 1, 2, 3], // Test1 => 1,3
    [1, 1, 2, 3, 4], // Test2 => 1,4
    [1, 1, 2, 2, 3, 4, 5, 5], // Test3 => 1,2,5,5
    [1, 1, 2, 2, 3, 4, 5, 5, 1, 1, 5], // Test4 => 1,1,1,2,5
];*/
/**
 * Complete this function
 *
 * u/param array $candies A one dimensional array of candies
 *
 * u/return array Berit's candies
 */
function getBeritsCandies(array $candies): array{
return [];
    }

/*
 * Code below is just to print answers. Do not edit.
 */
foreach ($candiesTests as $candies) {
$beritsCandies = getBeritsCandies($candies);
echo('Berits candies: '.implode(', ', $beritsCandies).PHP_EOL);
}

 

 

I am so stuck.. When im using the array_unique function the duplicates dissapear, I want an "reverse" array_unique maybe? can someone help a lost soul out?

Link to comment
https://forums.phpfreaks.com/topic/315448-i-have-some-struggle-with-a-php-task/
Share on other sites

  • Solution

$candiesTests = [

    [1, 1, 2, 3], // Test1 => 1,3

    [1, 1, 2, 3, 4], // Test2 => 1,4

    [1, 1, 2, 2, 3, 4, 5, 5], // Test3 => 1,2,5,5

    [1, 1, 2, 2, 3, 4, 5, 5, 1, 1, 5], // Test4 => 1,1,1,2,5

];

 

/**

 * Complete this function

 *

 * @param array $candies A one dimensional array of candies

 *

 * @return array Berit's candies

 */

function getBeritsCandies(array $candies): array{

    $newarray = array_unique($candies);

    $result= array_diff($newarray,$candies);

    $el = count($candies);

 

    if ($el % 2 == 0) {

        return $newarray;

        # code...

    }

    else {

         return $newarray;

    }

   

 

 

    return [];

 }



 

/*

 * Code below is just to print answers. Do not edit.

 */

foreach ($candiesTests as $candies) {

    $beritsCandies = getBeritsCandies($candies);

    echo('Berits candies: '.implode(', ', $beritsCandies).PHP_EOL);

}

now i have this 

function getBeritsCandies(array $candies): array{

   $removed = array_diff($candies, array_diff_assoc($candies, array_unique($candies)));

    return array_diff($candies, $removed);

}

now the output displays only ther duplicates but i have nmo idea how to get the right numbers in the right place.. 

Try

function getBeritsCandies(array $candies): array
{
    $quota = floor(count($candies)/2);                        // Berit's share

    $k = array_count_values($candies);
    $berit = [];
    arsort($k);                                               // start with ones we have most of
    foreach ($k as $t => &$n) {
        if ($n > 1 ) {                                        // if we have multiple, Berit can can have all but 1
            $m = $n - 1;                                      // until she has her quota
            for ($i=0; $i<$m; $i++) {
                if (count($berit) == $quota) break;
                $berit[] = $t;
                --$n;
            }
        }
        elseif (count($berit) < $quota) {
            $berit[] = $t;
            --$n;
        }
    }
    return $berit;
 } 

Results

Berits candies: 1, 2
Berits candies: 1, 2
Berits candies: 1, 2, 5, 3
Berits candies: 1, 1, 1, 5, 5

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

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.