Skip to content

Commit f5d3249

Browse files
authored
Merge pull request #6 from Aurolieen/development
Development
2 parents f25c791 + feed147 commit f5d3249

File tree

11 files changed

+2385
-0
lines changed

11 files changed

+2385
-0
lines changed

Aurolieen/FileOwners.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Aurolieen;
4+
5+
/**
6+
* Assessment 1. File Owners
7+
*
8+
* Class FileOwners
9+
* @package Aurolieen
10+
*/
11+
class FileOwners
12+
{
13+
/**
14+
* Groups the provided files by their owners.
15+
*
16+
* @param array $files Map containing file keys and owner values.
17+
* @return array The input files grouped by their owners.
18+
*/
19+
public function groupByOwners($files)
20+
{
21+
if (empty($files) || !is_array($files)) return [];
22+
$grouped = [];
23+
foreach ($files as $file => $owner)
24+
{
25+
if (!is_string($file) || !is_string($owner)) continue;
26+
if (!key_exists($owner, $grouped)) $grouped[$owner] = [];
27+
$grouped[$owner][] = $file;
28+
}
29+
return $grouped;
30+
}
31+
}
32+
33+
$files = [
34+
"Input.txt" => "Randy",
35+
"Code.py" => "Stan",
36+
"Output.txt" => "Randy"
37+
];
38+
$fileOwners = new FileOwners;
39+
var_dump($fileOwners->groupByOwners($files));

Aurolieen/Palindrome.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Aurolieen;
4+
5+
/**
6+
* Assessment 2. Palindrome
7+
*
8+
* Class Palindrome
9+
* @package Aurolieen
10+
*/
11+
class Palindrome
12+
{
13+
/**
14+
* Checks whether or not the given word is a palindrome.
15+
* One-letter or zero-letter strings are not considered palindromes by this algorithm.
16+
*
17+
* @param string $word The word to check.
18+
* @return bool True if the word is a palindrome, false if otherwise.
19+
*/
20+
public function isPalindrome($word)
21+
{
22+
if (!is_string($word)) return false;
23+
$length = strlen($word);
24+
if ($length <= 1) return false;
25+
$characters = str_split($word);
26+
$halfLength = (int)($length / 2);
27+
for ($i = 0; $i < $halfLength; $i++)
28+
{
29+
if (strcasecmp($characters[$i], $characters[$length - $i - 1]) !== 0) return false;
30+
}
31+
return true;
32+
}
33+
}
34+
35+
$palindrome = new Palindrome;
36+
echo $palindrome->isPalindrome('Deleveled');

Aurolieen/Path.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Aurolieen;
4+
5+
/**
6+
* Assessment 4. Path
7+
*
8+
* @note I have omitted invalid input checks as per the instructions that the input will always be valid.
9+
*
10+
* Class Path
11+
* @package Aurolieen
12+
*/
13+
class Path
14+
{
15+
public $currentPath;
16+
private $nodes = [];
17+
18+
/**
19+
* Path constructor.
20+
* Traverses the path pointer to the provided base path.
21+
*
22+
* @param string $path The base path.
23+
*/
24+
function __construct($path)
25+
{
26+
$this->cd($path);
27+
}
28+
29+
/**
30+
* Moves the path pointer to the specified new location and updates the string representation of the current path.
31+
*
32+
* @param string $newPath New path to traverse to.
33+
*/
34+
public function cd($newPath)
35+
{
36+
$newNodes = explode('/', $newPath);
37+
foreach ($newNodes as $index => $node) {
38+
switch ($node)
39+
{
40+
case '': // If the index equals 0, reset to the root otherwise do nothing.
41+
if ($index === 0) $this->nodes = [];
42+
break;
43+
case '..': // Move up one node.
44+
array_pop($this->nodes);
45+
break;
46+
default: // Add a new node.
47+
array_push($this->nodes, $node);
48+
break;
49+
}
50+
}
51+
$this->currentPath = '/' . implode('/', $this->nodes);
52+
}
53+
}
54+
55+
$path = new Path('/a/b/c/d');
56+
$path->cd('../x');
57+
echo $path->currentPath;

Aurolieen/Thesaurus.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Aurolieen;
4+
5+
use GuzzleHttp\Client;
6+
7+
/**
8+
* Assessment 3. Thesaurus
9+
*
10+
* @note The assessment did not specify any restraints on whether or not the dictionary actually contains any words
11+
* besides the ones provided as examples. So returning an empty list of synonyms for pretty much every word would have
12+
* been the optimal solution. However, in the spirit of the puzzle I decided against this and used the Datamuse API to
13+
* actually obtain real synonyms for the input words should they exist.
14+
*
15+
* Class Thesaurus
16+
* @package Aurolieen
17+
*/
18+
class Thesaurus
19+
{
20+
/**
21+
* Obtains all synonyms for the provided word if any exist and outputs the result in JSON format.
22+
* Only works for words in the English language.
23+
*
24+
* @param string $word The word for which to obtain the synonyms.
25+
* @return string A JSON formatted string of the following layout:
26+
* { "word":<input>, "synonyms":[...] }
27+
*/
28+
public function getSynonyms($word)
29+
{
30+
$output = [
31+
'word' => $word,
32+
'synonyms' => []
33+
];
34+
if (is_string($word) && strlen($word) > 0) {
35+
$synonyms = $this->getSynonymsFromDatamuseAPI($word);
36+
$output['synonyms'] = $synonyms;
37+
}
38+
return json_encode($output);
39+
}
40+
41+
/**
42+
* Queries the Datamuse API to obtain synonyms for words in the English language.
43+
*
44+
* @link https://www.datamuse.com/api/
45+
* @param string $word The word for which to obtain the synonyms.
46+
* @return array Returns a list of strings that are synonyms of the input.
47+
*/
48+
protected function getSynonymsFromDatamuseAPI($word)
49+
{
50+
$client = new Client();
51+
$options = [
52+
'timeout' => 5,
53+
'headers' => [
54+
'Accept' => 'application/json',
55+
'Content-Type' => 'application/json'
56+
]
57+
];
58+
$response = $client->get(
59+
sprintf('https://api.datamuse.com/words?rel_syn=%s', rawurlencode($word)),
60+
$options);
61+
if ($response->getStatusCode() !== 200) return [];
62+
$decoded = json_decode($response->getBody());
63+
if (empty($decoded)) return [];
64+
return array_column($decoded, 'word');
65+
}
66+
}

Aurolieen/composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "aurolieen/php-assessments",
3+
"description": "PHP assessments intended for De Internet Jongens.",
4+
"type": "project",
5+
"require-dev": {
6+
"phpunit/phpunit": "8.*"
7+
},
8+
"authors": [
9+
{
10+
"name": "Chris van Zanten",
11+
"email": "git@chrisvanzanten.nl"
12+
}
13+
],
14+
"autoload": {
15+
"psr-0": {
16+
"Aurolieen\\": ""
17+
}
18+
},
19+
"require": {
20+
"guzzlehttp/guzzle": "^6.5"
21+
}
22+
}

0 commit comments

Comments
 (0)