Jump to content

Viktor9769

New Members
  • Posts

    4
  • Joined

  • Last visited

Viktor9769's Achievements

Newbie

Newbie (1/5)

0

Reputation

1

Community Answers

  1. 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
  2. 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..
  3. $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); }
  4. 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?
×
×
  • 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.