1
<?php
class FileOwners
{
    public static function groupByOwners($files)
    {
        return NULL;
    }
}

$files = array
(
    "Input.txt" => "Randy",
    "Code.py" => "Stan",
    "Output.txt" => "Randy"
);

var_dump(FileOwners::groupByOwners($files));

Implement a groupByOwners function :

  • Accepts an associative array containing the file owner name for each file name.

  • Returns an associative array containing an array of file names for each owner name, in any order.

    For example

Given the input:

["Input.txt" => "Randy", "Code.py" => "Stan", "Output.txt" => "Randy"]

groupByOwners returns:

["Randy" => ["Input.txt", "Output.txt"], "Stan" => ["Code.py"]]

3 Answers 3

5
<?php
class FileOwners
{
    public static function groupByOwners($files)
    {
        $result=array();
        foreach($files as $key=>$value)
        {
            $result[$value][]=$key;
        }
        return $result;
    }
}

$files = array
(
    "Input.txt" => "Randy",
    "Code.py" => "Stan",
    "Output.txt" => "Randy"
);
print_r(FileOwners::groupByOwners($files));

Output:

Array
(
    [Randy] => Array
        (
            [0] => Input.txt
            [1] => Output.txt
        )

    [Stan] => Array
        (
            [0] => Code.py
        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

print_r() WILL NOT RETURN THE INTENDED RESULTS for the test. Printr() is human readable which is why the test uses var_dump stackoverflow.com/questions/3406171/php-var-dump-vs-print-r
1

Here is the one witch gonna work for sure:

 function groupByOwners(array $files) : array
 {
$result =array();
foreach($files as $key =>$elem){
  $result[$elem][]=$key;
 }
return $result;
    }
$files = array
(
"Input.txt" => "Randy",
"Code.py" => "Stan",
"Output.txt" => "Randy"
 );
  var_dump(groupByOwners($files));

1 Comment

THIS IS THE CORRECT ANSWER WITH THE CORRECT RETURN: array(2) { ["Randy"]=> array(2) { [0]=> string(9) "Input.txt" [1]=> string(10) "Output.txt" } ["Stan"]=> array(1) { [0]=> string(7) "Code.py" } }
-1
$files = array("Input.txt" => "Randy", "Code.py" => "Stan","Output.txt" => "Randy");
$narr = array_fill_keys(array_keys(array_flip($files)), []);

foreach($files as $key => $value) array_push($narr[$value], $key);
return $narr;

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
The result is the same and I like the method but it is incorrect because it does not use a groupByOwners function. How will another developer find this class?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.