Skip to content
39 changes: 39 additions & 0 deletions Aurolieen/FileOwners.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Aurolieen;

/**
* Assessment 1. File Owners
*
* Class FileOwners
* @package Aurolieen
*/
class FileOwners
{
/**
* Groups the provided files by their owners.
*
* @param array $files Map containing file keys and owner values.
* @return array The input files grouped by their owners.
*/
public function groupByOwners($files)
{
if (empty($files) || !is_array($files)) return [];
$grouped = [];
foreach ($files as $file => $owner)
{
if (!is_string($file) || !is_string($owner)) continue;
if (!key_exists($owner, $grouped)) $grouped[$owner] = [];
$grouped[$owner][] = $file;
}
return $grouped;
}
}

$files = [
"Input.txt" => "Randy",
"Code.py" => "Stan",
"Output.txt" => "Randy"
];
$fileOwners = new FileOwners;
var_dump($fileOwners->groupByOwners($files));
36 changes: 36 additions & 0 deletions Aurolieen/Palindrome.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Aurolieen;

/**
* Assessment 2. Palindrome
*
* Class Palindrome
* @package Aurolieen
*/
class Palindrome
{
/**
* Checks whether or not the given word is a palindrome.
* One-letter or zero-letter strings are not considered palindromes by this algorithm.
*
* @param string $word The word to check.
* @return bool True if the word is a palindrome, false if otherwise.
*/
public function isPalindrome($word)
{
if (!is_string($word)) return false;
$length = strlen($word);
if ($length <= 1) return false;
$characters = str_split($word);
$halfLength = (int)($length / 2);
for ($i = 0; $i < $halfLength; $i++)
{
if (strcasecmp($characters[$i], $characters[$length - $i - 1]) !== 0) return false;
}
return true;
}
}

$palindrome = new Palindrome;
echo $palindrome->isPalindrome('Deleveled');
57 changes: 57 additions & 0 deletions Aurolieen/Path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Aurolieen;

/**
* Assessment 4. Path
*
* @note I have omitted invalid input checks as per the instructions that the input will always be valid.
*
* Class Path
* @package Aurolieen
*/
class Path
{
public $currentPath;
private $nodes = [];

/**
* Path constructor.
* Traverses the path pointer to the provided base path.
*
* @param string $path The base path.
*/
function __construct($path)
{
$this->cd($path);
}

/**
* Moves the path pointer to the specified new location and updates the string representation of the current path.
*
* @param string $newPath New path to traverse to.
*/
public function cd($newPath)
{
$newNodes = explode('/', $newPath);
foreach ($newNodes as $index => $node) {
switch ($node)
{
case '': // If the index equals 0, reset to the root otherwise do nothing.
if ($index === 0) $this->nodes = [];
break;
case '..': // Move up one node.
array_pop($this->nodes);
break;
default: // Add a new node.
array_push($this->nodes, $node);
break;
}
}
$this->currentPath = '/' . implode('/', $this->nodes);
}
}

$path = new Path('/a/b/c/d');
$path->cd('../x');
echo $path->currentPath;
66 changes: 66 additions & 0 deletions Aurolieen/Thesaurus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Aurolieen;

use GuzzleHttp\Client;

/**
* Assessment 3. Thesaurus
*
* @note The assessment did not specify any restraints on whether or not the dictionary actually contains any words
* besides the ones provided as examples. So returning an empty list of synonyms for pretty much every word would have
* been the optimal solution. However, in the spirit of the puzzle I decided against this and used the Datamuse API to
* actually obtain real synonyms for the input words should they exist.
*
* Class Thesaurus
* @package Aurolieen
*/
class Thesaurus
{
/**
* Obtains all synonyms for the provided word if any exist and outputs the result in JSON format.
* Only works for words in the English language.
*
* @param string $word The word for which to obtain the synonyms.
* @return string A JSON formatted string of the following layout:
* { "word":<input>, "synonyms":[...] }
*/
public function getSynonyms($word)
{
$output = [
'word' => $word,
'synonyms' => []
];
if (is_string($word) && strlen($word) > 0) {
$synonyms = $this->getSynonymsFromDatamuseAPI($word);
$output['synonyms'] = $synonyms;
}
return json_encode($output);
}

/**
* Queries the Datamuse API to obtain synonyms for words in the English language.
*
* @link https://www.datamuse.com/api/
* @param string $word The word for which to obtain the synonyms.
* @return array Returns a list of strings that are synonyms of the input.
*/
protected function getSynonymsFromDatamuseAPI($word)
{
$client = new Client();
$options = [
'timeout' => 5,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]
];
$response = $client->get(
sprintf('https://api.datamuse.com/words?rel_syn=%s', rawurlencode($word)),
$options);
if ($response->getStatusCode() !== 200) return [];
$decoded = json_decode($response->getBody());
if (empty($decoded)) return [];
return array_column($decoded, 'word');
}
}
22 changes: 22 additions & 0 deletions Aurolieen/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "aurolieen/php-assessments",
"description": "PHP assessments intended for De Internet Jongens.",
"type": "project",
"require-dev": {
"phpunit/phpunit": "8.*"
},
"authors": [
{
"name": "Chris van Zanten",
"email": "git@chrisvanzanten.nl"
}
],
"autoload": {
"psr-0": {
"Aurolieen\\": ""
}
},
"require": {
"guzzlehttp/guzzle": "^6.5"
}
}
Loading