Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions XimmerNL/fileowners.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
class FileOwners
{
public function groupByOwners($files)
{
$owners = [];
foreach($files as $file => $owner){
$owners[$owner][] = $file;
}
return $owners;

// $owners = array();
// array_walk($files, function($value, $key) use (&$owners ){
// if(!isset($owners[$value]) || !is_array($owners[$value])){
// $owners[$value] = [];
// }
// $owners[$value][] = $key;
// });
// return $owners;

}
}

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

final class FileOwnersTest extends PHPUnit\Framework\TestCase
{
public function testA(): void
{
$in = [
"Input.txt" => "Randy",
"Code.py" => "Stan",
"Output.txt" => "Randy"
];
$out = [
"Randy" => [ "Input.txt", "Output.txt" ],
"Stan" => [ "Code.py" ]
];
$fileOwners = new FileOwners;
$test = $fileOwners->groupByOwners($in);
$this->assertEquals($out, $test);
}
}
?>
13 changes: 13 additions & 0 deletions XimmerNL/palindrome.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
class Palindrome
{
public function isPalindrome($word)
{
$word = strtolower($word);
return ($word === strrev($word));
}
}

$palindrome = new Palindrome;
var_dump($palindrome->isPalindrome('Deleveled'));
?>
15 changes: 15 additions & 0 deletions XimmerNL/palindrome_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
require 'palindrome.php';

final class PalindromeTest extends PHPUnit\Framework\TestCase
{
public function testA(): void
{
$in = "Deleveled";
$out = true;
$palindrome = new Palindrome;
$test = $palindrome->isPalindrome($in);
$this->assertEquals($out, $test);
}
}
?>
58 changes: 58 additions & 0 deletions XimmerNL/path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
class Path
{
public $currentPath;

function __construct($path)
{
$this->currentPath = $path;
}

public function cd($newPath)
{
// New path given?
if(strlen($newPath) > 0){

// Check type of path
if($newPath[0] !== '/'){

// Need items is parts, it's a relative path
$curDirs = explode('/', $this->currentPath);
$chgDirs = explode('/', $newPath);

// Loop through the 'change dir' items
foreach($chgDirs as $chgDir){
// Up one directory, so remove item
if($chgDir == '..'){
// Remove last item from currentPath
array_pop($curDirs);
}else{
// Add item to currentPath
array_push($curDirs, $chgDir);
}
}

// Add slashed between dirs, to get new 'current' path
$this->currentPath = implode('/', $curDirs);

}else{

// New path is an absolute path
$this->currentPath = $newPath;

}

return $this->currentPath;

}else{
return $this->currentPath;
}

}
}

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

final class PathTest extends PHPUnit\Framework\TestCase
{
public function testA(): void
{
$in = '../x';
$out = '/a/b/c/x';
$path = new Path('/a/b/c/d');
$test = $path->cd($in);
$this->assertEquals($out, $test);
}

public function testB(): void
{
$in = '/x/y/z';
$out = '/x/y/z';
$path = new Path('/a/b/c/d');
$test = $path->cd($in);
$this->assertEquals($out, $test);
}

public function testC(): void
{
$in = '../../../../x';
$out = '/x';
$path = new Path('/a/b/c/d');
$test = $path->cd($in);
$this->assertEquals($out, $test);
}

public function testD(): void
{
$in = '';
$out = '/a/b/c/d';
$path = new Path('/a/b/c/d');
$test = $path->cd($in);
$this->assertEquals($out, $test);
}

}
?>
33 changes: 33 additions & 0 deletions XimmerNL/thesaurus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
class Thesaurus
{
public $thesaurus;

function __construct($thesaurus)
{
$this->thesaurus = $thesaurus;
}

public function getSynonyms($word)
{
// Find word in set
$synonyms = (array_key_exists($word, $this->thesaurus)) ? $this->thesaurus[$word] : [];

// Return json encoded array with search + result
return json_encode(
array(
"word" => $word,
"synonyms" => $synonyms
)
);
}
}

$thesaurus = new Thesaurus(array(
"buy" => array("purchase"),
"big" => array("great", "large")
)
);
var_dump($thesaurus->getSynonyms('big'));
var_dump($thesaurus->getSynonyms('agelast'));
?>
32 changes: 32 additions & 0 deletions XimmerNL/thesaurus_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
require 'thesaurus.php';

final class ThesaurusTest extends PHPUnit\Framework\TestCase
{
public function testA(): void
{
$in = 'big';
$out = '{"word":"big","synonyms":["great","large"]}';
$thesaurus = new Thesaurus(array(
"buy" => array("purchase"),
"big" => array("great", "large")
)
);
$test = $thesaurus->getSynonyms($in);
$this->assertEquals($out, $test);
}

public function testB(): void
{
$in = 'agelast';
$out = '{"word":"agelast","synonyms":[]}';
$thesaurus = new Thesaurus(array(
"buy" => array("purchase"),
"big" => array("great", "large")
)
);
$test = $thesaurus->getSynonyms($in);
$this->assertEquals($out, $test);
}
}
?>