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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
noud/phpunit
22 changes: 22 additions & 0 deletions noud/FileOwners.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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"
);
$fileOwners = new FileOwners;
var_dump($fileOwners->groupByOwners($files));
18 changes: 18 additions & 0 deletions noud/Palindrome.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
class Palindrome
{
public static function isPalindrome($word)
{
$a = strtolower(preg_replace("/[^A-Za-z0-9]/","",$word));
if ($a==strrev($a)) {
return true;
} else {
return false;
}
}
}

$palindrome = new Palindrome;
echo $palindrome->isPalindrome('Deleveled');


37 changes: 37 additions & 0 deletions noud/Path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
class Path
{
public $currentPath;

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

public function cd($newPath)
{
$dir = explode('/', $this->currentPath);
$cd = explode('/', $newPath);
$hitung = 0;
foreach($cd as $key => $value){
if($value == '..'){
$hitung += 1;
unset($cd[$key]);
}
}

$hasil = '';
for($i=0; $i<= (count($dir)-1-$hitung); $i++){
$hasil .= $dir[$i].(($i == (count($dir)-1-$hitung)) ? '' : '/');
}

$tmp_cd = implode('/',$cd);
$this->currentPath = $hasil.(strlen($tmp_cd)>0 ? '/'.$tmp_cd : '');

return $this;
}
}

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

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

public function getSynonyms($word)
{
$synonims = (!empty($this->thesaurus[$word]) ? $this->thesaurus[$word] : array());
return json_encode(array('word'=>$word, 'synonyms'=>$synonims));
}
}

$thesaurus = new Thesaurus(
array
(
"buy" => array("purchase"),
"big" => array("great", "large")
));

echo json_encode($thesaurus->getSynonyms("big"));
echo "\n";
echo json_encode($thesaurus->getSynonyms("agelast"));
29 changes: 29 additions & 0 deletions noud/tests/FileOwnersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
use PHPUnit\Framework\TestCase;
include('FileOwners.php');

final class FileOwnersTest extends TestCase
{
public function testFileOwners()
{
$files = array(
"Input.txt" => "Randy",
"Code.py" => "Stan",
"Output.txt" => "Randy"
);
$fileOwners = new FileOwners;
$this->assertEquals(
array(
'Randy' => array(
'Input.txt',
'Output.txt'
),
'Stan' => array(
'Code.py'
)
),
$fileOwners->groupByOwners($files)
);
}
}

16 changes: 16 additions & 0 deletions noud/tests/PalindromeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
use PHPUnit\Framework\TestCase;
include('Palindrome.php');

final class PalindromeTest extends TestCase
{
public function testisPalindrome()
{
$palindrome = new Palindrome;
$this->assertEquals(
'1',
$palindrome->isPalindrome('Deleveled')
);
}
}

17 changes: 17 additions & 0 deletions noud/tests/PathTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
use PHPUnit\Framework\TestCase;
include('Path.php');

final class PathTest extends TestCase
{
public function testPath()
{
$path = new Path('/a/b/c/d');
$path->cd('../x');
$this->assertEquals(
'/a/b/c/x',
$path->currentPath
);
}
}

35 changes: 35 additions & 0 deletions noud/tests/ThesaurusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
use PHPUnit\Framework\TestCase;
include('Thesaurus.php');

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

public function testHasNotThesaurus()
{
$thesaurus = new Thesaurus(
array(
"buy" => array("purchase"),
"big" => array("great", "large")
)
);
$this->assertEquals(
'"{\"word\":\"agelast\",\"synonyms\":[]}"',
json_encode($thesaurus->getSynonyms("agelast"))
);
}
}