Viktor9769 Posted October 22, 2022 Share Posted October 22, 2022 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? Quote Link to comment https://forums.phpfreaks.com/topic/315448-i-have-some-struggle-with-a-php-task/ Share on other sites More sharing options...
Barand Posted October 22, 2022 Share Posted October 22, 2022 I don't see array_unique() anywhere - what is your current code? Quote Link to comment https://forums.phpfreaks.com/topic/315448-i-have-some-struggle-with-a-php-task/#findComment-1601860 Share on other sites More sharing options...
Solution Viktor9769 Posted October 22, 2022 Author Solution Share Posted October 22, 2022 $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); } Quote Link to comment https://forums.phpfreaks.com/topic/315448-i-have-some-struggle-with-a-php-task/#findComment-1601861 Share on other sites More sharing options...
Viktor9769 Posted October 22, 2022 Author Share Posted October 22, 2022 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.. Quote Link to comment https://forums.phpfreaks.com/topic/315448-i-have-some-struggle-with-a-php-task/#findComment-1601862 Share on other sites More sharing options...
Barand Posted October 22, 2022 Share Posted October 22, 2022 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 1 Quote Link to comment https://forums.phpfreaks.com/topic/315448-i-have-some-struggle-with-a-php-task/#findComment-1601864 Share on other sites More sharing options...
Viktor9769 Posted October 23, 2022 Author Share Posted October 23, 2022 Thank you so much! the only problem left is how im gonna solve the problem to have the results .... 1️⃣Berits candies: 1, 3 Berits candies: 1, 4 Berits candies: 1, 2, 5, 5 Berits candies: 1, 1, 1, 2, 5 Quote Link to comment https://forums.phpfreaks.com/topic/315448-i-have-some-struggle-with-a-php-task/#findComment-1601874 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.