Skip to content

Commit

Permalink
- Create dataset coding interview with unit test
Browse files Browse the repository at this point in the history
- Add readme on dataset coding interview (WIP)
  • Loading branch information
ianriizky committed Dec 25, 2021
1 parent 233d0e8 commit 0910513
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 0 deletions.
150 changes: 150 additions & 0 deletions src/DataSet/DataSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

namespace Ianriizky\CodingInterview\DataSet;

use SplFixedArray;

class DataSet
{
/**
* Current size of the collection.
*
* @var int
*/
protected int $size = 0;

/**
* Collection of the data set.
*
* @var \SplFixedArray
*/
protected SplFixedArray $collection;

/**
* Create a new instance class.
*
* @param int $initialSize
* @return void
*/
public function __construct(int $initialSize = 10)
{
$this->collection = new SplFixedArray($initialSize);
}

/**
* Add a value to the collection.
*
* @param mixed $value
* @return bool
*/
public function add($value): bool
{
if ($this->contains($value)) {
return false;
}

$this->ensureSize();

$this->collection[$this->size] = $value;
$this->size++;

return true;
}

/**
* Determine whether the given value is exists on the collection or not.
*
* @param mixed $value
* @return bool
*/
public function contains($value): bool
{
foreach ($this->collection as $item) {
if ($value === $item) {
return true;
}
}

return false;
}

/**
* Return size of collection.
*
* @return int
*/
public function size(): int
{
return $this->size;
}

/**
* Return collection of the data set.
*
* @return \SplFixedArray
*/
public function collection(): SplFixedArray
{
return $this->collection;
}

/**
* Remove a value from the collection.
*
* @param mixed $value
* @return bool
*/
public function remove($value): bool
{
if (!$this->contains($value)) {
return false;
}

$removedIndex = $this->indexOf($value);

for ($index = $removedIndex; $index <= $this->size; $index++) {
$this->collection[$index] = $this->collection[$index + 1];
}

$this->size--;

return true;
}

/**
* Return index of given value.
*
* @param mixed $value
* @return int
*/
protected function indexOf($value): int
{
for ($index = 0; $index < $this->collection->count(); $index++) {
if ($value === $this->collection[$index]) {
return $index;
}
}

return -1;
}

/**
* Ensure that the data set size is enough to contain the collection.
*
* @return void
*/
protected function ensureSize(): void
{
if ($this->size < $this->collection->count()) {
return;
}

$tempCollection = new SplFixedArray($this->size + 1);

for ($index = 0; $index < $this->collection->count(); $index++) {
$tempCollection[$index] = $this->collection[$index];
}

$this->collection = $tempCollection;
}
}
16 changes: 16 additions & 0 deletions src/DataSet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Data Set

Data Set adalah ... <sup id="pg1">[1](#fn1)</sup>.

Berikut ini adalah contoh *source code* dari studi kasus Data Set menggunakan bahasa pemrograman PHP <sup id="pg2">[2](#fn2)</sup>. Detail lengkapnya bisa dilihat [di sini](DataSet.php).
```php
///
```

---
### Catatan Kaki:

<strong id="fn1">1</strong> [Data set, Wikipedia](https://en.wikipedia.org/wiki/Data_set). [](#pg1)
<br>
<strong id="fn2">2</strong> [Programmer Zaman Now - Coding Interview Struktur Data Set](https://www.youtube.com/watch?v=OWm1vSHrC_A). [](#pg2)
<br>
111 changes: 111 additions & 0 deletions tests/Unit/DataSetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Tests\Unit;

use Ianriizky\CodingInterview\DataSet\DataSet;
use Tests\TestCase;

class DataSetTest extends TestCase
{
/**
* Assert that data set add() method is run correctly.
*
* @return void
*/
public function test_add()
{
$dataset = new DataSet;

$this->assertTrue($dataset->add('Septianata'));
$this->assertFalse($dataset->add('Septianata'));

$this->assertTrue($dataset->add('Rizky'));
$this->assertFalse($dataset->add('Rizky'));

$this->assertTrue($dataset->add('Pratama'));
$this->assertFalse($dataset->add('Pratama'));
}

/**
* Assert that data set contains() method is run correctly.
*
* @return void
*/
public function test_contains()
{
$dataset = new DataSet;

$dataset->add('Septianata');
$dataset->add('Rizky');

$this->assertTrue($dataset->contains('Septianata'));
$this->assertTrue($dataset->contains('Rizky'));
$this->assertFalse($dataset->contains('Pratama'));
}

/**
* Assert that data set size() method is run correctly.
*
* @return void
*/
public function test_size()
{
$dataset = new DataSet;
$this->assertEquals($dataset->size(), 0);

$dataset->add('Septianata');
$this->assertEquals($dataset->size(), 1);

$dataset->add('Septianata');
$this->assertEquals($dataset->size(), 1);

$dataset->add('Rizky');
$this->assertEquals($dataset->size(), 2);
}

/**
* Assert that data set remove() method is run correctly.
*
* @return void
*/
public function test_remove()
{
$dataset = new DataSet;

$dataset->add('Septianata');
$dataset->add('Rizky');
$dataset->add('Pratama');
$dataset->add('Belajar');
$dataset->add('Data Set');
$dataset->add('PHP');

$this->assertEquals(6, $dataset->size());

$dataset->remove('Rizky');

$this->assertEquals(5, $dataset->size());

$this->assertTrue($dataset->contains('Septianata'));
$this->assertTrue($dataset->contains('Pratama'));
$this->assertTrue($dataset->contains('Belajar'));
$this->assertTrue($dataset->contains('Data Set'));
$this->assertTrue($dataset->contains('PHP'));
}

/**
* Assert that data set growth() method is run correctly.
*
* @return void
*/
public function test_growth()
{
$size = rand(1, 100);
$dataset = new DataSet;

for ($index = 0; $index < $size; $index++) {
$dataset->add('item-' . $index);
}

$this->assertEquals($size, $dataset->size());
}
}

0 comments on commit 0910513

Please sign in to comment.