From 0910513b6806a32331ebe5e15cfe5bf95c90a168 Mon Sep 17 00:00:00 2001 From: Septianata Rizky Pratama Date: Sat, 25 Dec 2021 22:00:06 +0700 Subject: [PATCH] - Create dataset coding interview with unit test - Add readme on dataset coding interview (WIP) --- src/DataSet/DataSet.php | 150 +++++++++++++++++++++++++++++++++++++ src/DataSet/README.md | 16 ++++ tests/Unit/DataSetTest.php | 111 +++++++++++++++++++++++++++ 3 files changed, 277 insertions(+) create mode 100644 src/DataSet/DataSet.php create mode 100644 src/DataSet/README.md create mode 100644 tests/Unit/DataSetTest.php diff --git a/src/DataSet/DataSet.php b/src/DataSet/DataSet.php new file mode 100644 index 0000000..9059a8b --- /dev/null +++ b/src/DataSet/DataSet.php @@ -0,0 +1,150 @@ +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; + } +} diff --git a/src/DataSet/README.md b/src/DataSet/README.md new file mode 100644 index 0000000..26086cc --- /dev/null +++ b/src/DataSet/README.md @@ -0,0 +1,16 @@ +# Data Set + +Data Set adalah ... [1](#fn1). + +Berikut ini adalah contoh *source code* dari studi kasus Data Set menggunakan bahasa pemrograman PHP [2](#fn2). Detail lengkapnya bisa dilihat [di sini](DataSet.php). +```php +/// +``` + +--- +### Catatan Kaki: + +1 [Data set, Wikipedia](https://en.wikipedia.org/wiki/Data_set). [↩](#pg1) +
+2 [Programmer Zaman Now - Coding Interview Struktur Data Set](https://www.youtube.com/watch?v=OWm1vSHrC_A). [↩](#pg2) +
diff --git a/tests/Unit/DataSetTest.php b/tests/Unit/DataSetTest.php new file mode 100644 index 0000000..ee57f29 --- /dev/null +++ b/tests/Unit/DataSetTest.php @@ -0,0 +1,111 @@ +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()); + } +}