diff --git a/php/LICENSE b/php/LICENSE new file mode 100755 index 00000000..08262f17 --- /dev/null +++ b/php/LICENSE @@ -0,0 +1,21 @@ +The MIT License + +Copyright (c) 2016 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/php/README.md b/php/README.md new file mode 100755 index 00000000..3a18a1d1 --- /dev/null +++ b/php/README.md @@ -0,0 +1,43 @@ +# Cracking the Code Interview with PHP +> Cracking the code interview solutions for PHP developers + +Tested solutions for chapters. + +## Installation + +After you go into the app directory, you should execute following commands. +OS X & Linux: + +```sh +composer install +``` + +For installation, we need to consider these: + + - Firstly, install PHP 7 + - Secondly you can install PHPUnit globally for testing proposes + +Now, follow these steps: + - Clone repository + - composer install + +## Development setup + +To run all tests of the app, execute these commands in the app directory. + +```sh +composer install +phpunit +``` + +## Release History + +Refer to [CHANGELOG](https://github.com/midorikocak/ctci-php/CHANGELOG.md) + +## Meta + +Midori Kocak – [@midorikocak](https://twitter.com/midorikocak) – midori@mynameismidori.com + +Distributed under the MIT license. See ``LICENSE`` for more information. + +[https://github.com/midorikocak/ctci-php](https://github.com/midorikocak/ctci-php/) \ No newline at end of file diff --git a/php/composer.json b/php/composer.json new file mode 100755 index 00000000..0e847def --- /dev/null +++ b/php/composer.json @@ -0,0 +1,36 @@ +{ + "name": "midorikocak/ctci-php", + "description": "php solutions of cracking the code interview using php", + "type": "project", + "license": "MIT", + "authors": [ + { + "name": "Midori Kocak", + "email": "mtkocak@gmail.com" + } + ], + "minimum-stability": "dev", + "autoload": { + "psr-4": { + "MidoriKocak\\": "src/", + "CTCILibrary\\":"src/CTCILibrary", + "Introduction\\":"src/Introduction", + "Chapter01\\Question1_1\\":"src/Chapter01/Question1_1", + "Chapter01\\Question1_2\\":"src/Chapter01/Question1_2", + "Chapter01\\Question1_3\\":"src/Chapter01/Question1_3", + "Chapter01\\Question1_4\\":"src/Chapter01/Question1_4", + "Chapter01\\Question1_5\\":"src/Chapter01/Question1_5", + "Chapter01\\Question1_6\\":"src/Chapter01/Question1_6", + "Chapter01\\Question1_7\\":"src/Chapter01/Question1_7", + "Chapter01\\Question1_8\\":"src/Chapter01/Question1_8", + "Chapter01\\Question1_9\\":"src/Chapter01/Question1_9", + "Chapter02\\Question2_5\\":"src/Chapter02/Question2_5" + }, + "psr-0": { + "MidoriKocakTest\\": "tests/" + } + }, + "require": { + "phpunit/phpunit": "^6.1@dev" + } +} diff --git a/php/composer.lock b/php/composer.lock new file mode 100644 index 00000000..e270922b --- /dev/null +++ b/php/composer.lock @@ -0,0 +1,1428 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "content-hash": "c58c1293b46e1677a1555b1e1a5fcfcb", + "packages": [ + { + "name": "doctrine/instantiator", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/doctrine/instantiator.git", + "reference": "5acd2bd8c2b600ad5cc4c9180ebf0a930604d6a5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/5acd2bd8c2b600ad5cc4c9180ebf0a930604d6a5", + "reference": "5acd2bd8c2b600ad5cc4c9180ebf0a930604d6a5", + "shasum": "" + }, + "require": { + "php": ">=5.3,<8.0-DEV" + }, + "require-dev": { + "athletic/athletic": "~0.1.8", + "ext-pdo": "*", + "ext-phar": "*", + "phpunit/phpunit": "~4.0", + "squizlabs/php_codesniffer": "~2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Instantiator\\": "src/Doctrine/Instantiator/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com", + "homepage": "http://ocramius.github.com/" + } + ], + "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", + "homepage": "https://github.com/doctrine/instantiator", + "keywords": [ + "constructor", + "instantiate" + ], + "time": "2017-02-16 16:15:51" + }, + { + "name": "myclabs/deep-copy", + "version": "1.6.0", + "source": { + "type": "git", + "url": "https://github.com/myclabs/DeepCopy.git", + "reference": "5a5a9fc8025a08d8919be87d6884d5a92520cefe" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/5a5a9fc8025a08d8919be87d6884d5a92520cefe", + "reference": "5a5a9fc8025a08d8919be87d6884d5a92520cefe", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "doctrine/collections": "1.*", + "phpunit/phpunit": "~4.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "DeepCopy\\": "src/DeepCopy/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Create deep copies (clones) of your objects", + "homepage": "https://github.com/myclabs/DeepCopy", + "keywords": [ + "clone", + "copy", + "duplicate", + "object", + "object graph" + ], + "time": "2017-01-26T22:05:40+00:00" + }, + { + "name": "phar-io/manifest", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/phar-io/manifest.git", + "reference": "014feadb268809af7c8e2f7ccd396b8494901f58" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phar-io/manifest/zipball/014feadb268809af7c8e2f7ccd396b8494901f58", + "reference": "014feadb268809af7c8e2f7ccd396b8494901f58", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-phar": "*", + "phar-io/version": "^1.0.1", + "php": "^5.6 || ^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Arne Blankerts", + "email": "arne@blankerts.de", + "role": "Developer" + }, + { + "name": "Sebastian Heuer", + "email": "sebastian@phpeople.de", + "role": "Developer" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "Developer" + } + ], + "description": "Component for reading phar.io manifest information from a PHP Archive (PHAR)", + "time": "2017-04-07 07:07:10" + }, + { + "name": "phar-io/version", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/phar-io/version.git", + "reference": "a70c0ced4be299a63d32fa96d9281d03e94041df" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phar-io/version/zipball/a70c0ced4be299a63d32fa96d9281d03e94041df", + "reference": "a70c0ced4be299a63d32fa96d9281d03e94041df", + "shasum": "" + }, + "require": { + "php": "^5.6 || ^7.0" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Arne Blankerts", + "email": "arne@blankerts.de", + "role": "Developer" + }, + { + "name": "Sebastian Heuer", + "email": "sebastian@phpeople.de", + "role": "Developer" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "Developer" + } + ], + "description": "Library for handling version information and constraints", + "time": "2017-03-05T17:38:23+00:00" + }, + { + "name": "phpdocumentor/reflection-common", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionCommon.git", + "reference": "144c307535e82c8fdcaacbcfc1d6d8eeb896687c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/144c307535e82c8fdcaacbcfc1d6d8eeb896687c", + "reference": "144c307535e82c8fdcaacbcfc1d6d8eeb896687c", + "shasum": "" + }, + "require": { + "php": ">=5.5" + }, + "require-dev": { + "phpunit/phpunit": "^4.6" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jaap van Otterdijk", + "email": "opensource@ijaap.nl" + } + ], + "description": "Common reflection classes used by phpdocumentor to reflect the code structure", + "homepage": "http://www.phpdoc.org", + "keywords": [ + "FQSEN", + "phpDocumentor", + "phpdoc", + "reflection", + "static analysis" + ], + "time": "2015-12-27 11:43:31" + }, + { + "name": "phpdocumentor/reflection-docblock", + "version": "3.1.1", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", + "reference": "8331b5efe816ae05461b7ca1e721c01b46bafb3e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/8331b5efe816ae05461b7ca1e721c01b46bafb3e", + "reference": "8331b5efe816ae05461b7ca1e721c01b46bafb3e", + "shasum": "" + }, + "require": { + "php": ">=5.5", + "phpdocumentor/reflection-common": "^1.0@dev", + "phpdocumentor/type-resolver": "^0.2.0", + "webmozart/assert": "^1.0" + }, + "require-dev": { + "mockery/mockery": "^0.9.4", + "phpunit/phpunit": "^4.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + } + ], + "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", + "time": "2016-09-30T07:12:33+00:00" + }, + { + "name": "phpdocumentor/type-resolver", + "version": "0.2.1", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/TypeResolver.git", + "reference": "e224fb2ea2fba6d3ad6fdaef91cd09a172155ccb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/e224fb2ea2fba6d3ad6fdaef91cd09a172155ccb", + "reference": "e224fb2ea2fba6d3ad6fdaef91cd09a172155ccb", + "shasum": "" + }, + "require": { + "php": ">=5.5", + "phpdocumentor/reflection-common": "^1.0" + }, + "require-dev": { + "mockery/mockery": "^0.9.4", + "phpunit/phpunit": "^5.2||^4.8.24" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + } + ], + "time": "2016-11-25T06:54:22+00:00" + }, + { + "name": "phpspec/prophecy", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/phpspec/prophecy.git", + "reference": "abe41cb27f4e4207c6f54a09272969fe55e0bbff" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/abe41cb27f4e4207c6f54a09272969fe55e0bbff", + "reference": "abe41cb27f4e4207c6f54a09272969fe55e0bbff", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "^1.0.2", + "php": "^5.3|^7.0", + "phpdocumentor/reflection-docblock": "^2.0|^3.0.2", + "sebastian/comparator": "^1.1|^2.0", + "sebastian/recursion-context": "^1.0|^2.0|^3.0" + }, + "require-dev": { + "phpspec/phpspec": "^2.5|^3.2", + "phpunit/phpunit": "^4.8 || ^5.6.5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.7.x-dev" + } + }, + "autoload": { + "psr-0": { + "Prophecy\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com", + "homepage": "http://everzet.com" + }, + { + "name": "Marcello Duarte", + "email": "marcello.duarte@gmail.com" + } + ], + "description": "Highly opinionated mocking framework for PHP 5.3+", + "homepage": "https://github.com/phpspec/prophecy", + "keywords": [ + "Double", + "Dummy", + "fake", + "mock", + "spy", + "stub" + ], + "time": "2017-03-03 17:09:02" + }, + { + "name": "phpunit/php-code-coverage", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-code-coverage.git", + "reference": "cff36444733ac6d3f153866f55898373ca184610" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/cff36444733ac6d3f153866f55898373ca184610", + "reference": "cff36444733ac6d3f153866f55898373ca184610", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-xmlwriter": "*", + "php": "^7.0", + "phpunit/php-file-iterator": "^1.3", + "phpunit/php-text-template": "^1.2", + "phpunit/php-token-stream": "^1.4.11 || ^2.0", + "sebastian/code-unit-reverse-lookup": "^1.0", + "sebastian/environment": "^2.0", + "sebastian/version": "^2.0" + }, + "require-dev": { + "ext-xdebug": "^2.5", + "phpunit/phpunit": "^6.0" + }, + "suggest": { + "ext-xdebug": "^2.5.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.1.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", + "homepage": "https://github.com/sebastianbergmann/php-code-coverage", + "keywords": [ + "coverage", + "testing", + "xunit" + ], + "time": "2017-04-07 04:39:58" + }, + { + "name": "phpunit/php-file-iterator", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-file-iterator.git", + "reference": "3cc8f69b3028d0f96a9078e6295d86e9bf019be5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/3cc8f69b3028d0f96a9078e6295d86e9bf019be5", + "reference": "3cc8f69b3028d0f96a9078e6295d86e9bf019be5", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "FilterIterator implementation that filters files based on a list of suffixes.", + "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", + "keywords": [ + "filesystem", + "iterator" + ], + "time": "2016-10-03 07:40:28" + }, + { + "name": "phpunit/php-text-template", + "version": "1.2.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-text-template.git", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Simple template engine.", + "homepage": "https://github.com/sebastianbergmann/php-text-template/", + "keywords": [ + "template" + ], + "time": "2015-06-21T13:50:34+00:00" + }, + { + "name": "phpunit/php-timer", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-timer.git", + "reference": "d107f347d368dd8a384601398280c7c608390ab7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/d107f347d368dd8a384601398280c7c608390ab7", + "reference": "d107f347d368dd8a384601398280c7c608390ab7", + "shasum": "" + }, + "require": { + "php": "^5.3.3 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Utility class for timing", + "homepage": "https://github.com/sebastianbergmann/php-timer/", + "keywords": [ + "timer" + ], + "time": "2017-03-07 15:42:04" + }, + { + "name": "phpunit/php-token-stream", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-token-stream.git", + "reference": "9ddb181faa4a3841fe131c357fd01de75cbb4da9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/9ddb181faa4a3841fe131c357fd01de75cbb4da9", + "reference": "9ddb181faa4a3841fe131c357fd01de75cbb4da9", + "shasum": "" + }, + "require": { + "ext-tokenizer": "*", + "php": "^5.6 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^5.7 || ^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Wrapper around PHP's tokenizer extension.", + "homepage": "https://github.com/sebastianbergmann/php-token-stream/", + "keywords": [ + "tokenizer" + ], + "time": "2017-03-07 07:36:57" + }, + { + "name": "phpunit/phpunit", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit.git", + "reference": "479db496946e8c97337a73b8063048ac91970c1b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/479db496946e8c97337a73b8063048ac91970c1b", + "reference": "479db496946e8c97337a73b8063048ac91970c1b", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-json": "*", + "ext-libxml": "*", + "ext-mbstring": "*", + "ext-xml": "*", + "myclabs/deep-copy": "^1.3", + "phar-io/manifest": "^1.0.1", + "phar-io/version": "^1.0", + "php": "^7.0", + "phpspec/prophecy": "^1.7", + "phpunit/php-code-coverage": "^5.0", + "phpunit/php-file-iterator": "^1.4", + "phpunit/php-text-template": "^1.2", + "phpunit/php-timer": "^1.0.6", + "phpunit/phpunit-mock-objects": "^4.0", + "sebastian/comparator": "^2.0", + "sebastian/diff": "^1.2", + "sebastian/environment": "^2.0", + "sebastian/exporter": "^3.1", + "sebastian/global-state": "^1.1 || ^2.0", + "sebastian/object-enumerator": "^3.0.2", + "sebastian/resource-operations": "^1.0", + "sebastian/version": "^2.0" + }, + "conflict": { + "phpdocumentor/reflection-docblock": "3.0.2", + "phpunit/dbunit": "<3.0" + }, + "require-dev": { + "ext-pdo": "*" + }, + "suggest": { + "ext-xdebug": "*", + "phpunit/php-invoker": "^1.1" + }, + "bin": [ + "phpunit" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.1.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "The PHP Unit Testing framework.", + "homepage": "https://phpunit.de/", + "keywords": [ + "phpunit", + "testing", + "xunit" + ], + "time": "2017-04-07 14:33:58" + }, + { + "name": "phpunit/phpunit-mock-objects", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", + "reference": "5cbf2424850cc702617646fca5a2ae2c77bf6f6e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/5cbf2424850cc702617646fca5a2ae2c77bf6f6e", + "reference": "5cbf2424850cc702617646fca5a2ae2c77bf6f6e", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "^1.0.2", + "php": "^7.0", + "phpunit/php-text-template": "^1.2", + "sebastian/exporter": "^3.0" + }, + "conflict": { + "phpunit/phpunit": "<6.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "suggest": { + "ext-soap": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Mock Object library for PHPUnit", + "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", + "keywords": [ + "mock", + "xunit" + ], + "time": "2017-04-01 10:07:28" + }, + { + "name": "sebastian/code-unit-reverse-lookup", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/code-unit-reverse-lookup.git", + "reference": "3488be0a7b346cd6e5361510ed07e88f9bea2e88" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/code-unit-reverse-lookup/zipball/3488be0a7b346cd6e5361510ed07e88f9bea2e88", + "reference": "3488be0a7b346cd6e5361510ed07e88f9bea2e88", + "shasum": "" + }, + "require": { + "php": "^5.6 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^5.7 || ^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Looks up which function or method a line of code belongs to", + "homepage": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/", + "time": "2017-03-04 10:23:55" + }, + { + "name": "sebastian/comparator", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/comparator.git", + "reference": "8d783f84d4d5adb5e6668589c227f95172373b6a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/8d783f84d4d5adb5e6668589c227f95172373b6a", + "reference": "8d783f84d4d5adb5e6668589c227f95172373b6a", + "shasum": "" + }, + "require": { + "php": "^7.0", + "sebastian/diff": "^1.2", + "sebastian/exporter": "^3.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides the functionality to compare PHP values for equality", + "homepage": "http://www.github.com/sebastianbergmann/comparator", + "keywords": [ + "comparator", + "compare", + "equality" + ], + "time": "2017-03-07 07:07:51" + }, + { + "name": "sebastian/diff", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/diff.git", + "reference": "763d7adeb8c35d2af2b04c0f6cafeee059074dfb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/763d7adeb8c35d2af2b04c0f6cafeee059074dfb", + "reference": "763d7adeb8c35d2af2b04c0f6cafeee059074dfb", + "shasum": "" + }, + "require": { + "php": "^5.3.3 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Kore Nordmann", + "email": "mail@kore-nordmann.de" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Diff implementation", + "homepage": "https://github.com/sebastianbergmann/diff", + "keywords": [ + "diff" + ], + "time": "2017-03-07 07:26:53" + }, + { + "name": "sebastian/environment", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/environment.git", + "reference": "144fedf9aa8e3f1c52199f3634eb699cb59741d9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/144fedf9aa8e3f1c52199f3634eb699cb59741d9", + "reference": "144fedf9aa8e3f1c52199f3634eb699cb59741d9", + "shasum": "" + }, + "require": { + "php": "^5.6 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^5.7 || ^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides functionality to handle HHVM/PHP environments", + "homepage": "http://www.github.com/sebastianbergmann/environment", + "keywords": [ + "Xdebug", + "environment", + "hhvm" + ], + "time": "2017-03-07 12:59:58" + }, + { + "name": "sebastian/exporter", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/exporter.git", + "reference": "234199f4528de6d12aaa58b612e98f7d36adb937" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/234199f4528de6d12aaa58b612e98f7d36adb937", + "reference": "234199f4528de6d12aaa58b612e98f7d36adb937", + "shasum": "" + }, + "require": { + "php": "^7.0", + "sebastian/recursion-context": "^3.0" + }, + "require-dev": { + "ext-mbstring": "*", + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.1.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides the functionality to export PHP variables for visualization", + "homepage": "http://www.github.com/sebastianbergmann/exporter", + "keywords": [ + "export", + "exporter" + ], + "time": "2017-04-03 13:19:02" + }, + { + "name": "sebastian/global-state", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/global-state.git", + "reference": "1882b954f483d44095e72b40841472a22da91d4f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/1882b954f483d44095e72b40841472a22da91d4f", + "reference": "1882b954f483d44095e72b40841472a22da91d4f", + "shasum": "" + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "suggest": { + "ext-uopz": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Snapshotting of global state", + "homepage": "http://www.github.com/sebastianbergmann/global-state", + "keywords": [ + "global state" + ], + "time": "2017-03-07 07:33:59" + }, + { + "name": "sebastian/object-enumerator", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/object-enumerator.git", + "reference": "31dd3379d16446c5d86dec32ab1ad1f378581ad8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/31dd3379d16446c5d86dec32ab1ad1f378581ad8", + "reference": "31dd3379d16446c5d86dec32ab1ad1f378581ad8", + "shasum": "" + }, + "require": { + "php": "^7.0", + "sebastian/object-reflector": "^1.0", + "sebastian/recursion-context": "^3.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Traverses array structures and object graphs to enumerate all referenced objects", + "homepage": "https://github.com/sebastianbergmann/object-enumerator/", + "time": "2017-03-12 15:17:29" + }, + { + "name": "sebastian/object-reflector", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/object-reflector.git", + "reference": "773f97c67f28de00d397be301821b06708fca0be" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/object-reflector/zipball/773f97c67f28de00d397be301821b06708fca0be", + "reference": "773f97c67f28de00d397be301821b06708fca0be", + "shasum": "" + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Allows reflection of object attributes, including inherited and non-public ones", + "homepage": "https://github.com/sebastianbergmann/object-reflector/", + "time": "2017-03-29 09:07:27" + }, + { + "name": "sebastian/recursion-context", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/recursion-context.git", + "reference": "a0e54bc9bf04e2c5b302236984cebc277631f0f1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/a0e54bc9bf04e2c5b302236984cebc277631f0f1", + "reference": "a0e54bc9bf04e2c5b302236984cebc277631f0f1", + "shasum": "" + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides functionality to recursively process PHP variables", + "homepage": "http://www.github.com/sebastianbergmann/recursion-context", + "time": "2017-03-07 15:09:59" + }, + { + "name": "sebastian/resource-operations", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/resource-operations.git", + "reference": "fadc83f7c41fb2924e542635fea47ae546816ece" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/fadc83f7c41fb2924e542635fea47ae546816ece", + "reference": "fadc83f7c41fb2924e542635fea47ae546816ece", + "shasum": "" + }, + "require": { + "php": ">=5.6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides a list of PHP built-in functions that operate on resources", + "homepage": "https://www.github.com/sebastianbergmann/resource-operations", + "time": "2016-10-03 07:43:09" + }, + { + "name": "sebastian/version", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/version.git", + "reference": "99732be0ddb3361e16ad77b68ba41efc8e979019" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/99732be0ddb3361e16ad77b68ba41efc8e979019", + "reference": "99732be0ddb3361e16ad77b68ba41efc8e979019", + "shasum": "" + }, + "require": { + "php": ">=5.6" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library that helps with managing the version number of Git-hosted PHP projects", + "homepage": "https://github.com/sebastianbergmann/version", + "time": "2016-10-03 07:35:21" + }, + { + "name": "webmozart/assert", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/webmozart/assert.git", + "reference": "4a8bf11547e139e77b651365113fc12850c43d9a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/webmozart/assert/zipball/4a8bf11547e139e77b651365113fc12850c43d9a", + "reference": "4a8bf11547e139e77b651365113fc12850c43d9a", + "shasum": "" + }, + "require": { + "php": "^5.3.3 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.6", + "sebastian/version": "^1.0.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3-dev" + } + }, + "autoload": { + "psr-4": { + "Webmozart\\Assert\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + } + ], + "description": "Assertions to validate method input/output with nice error messages.", + "keywords": [ + "assert", + "check", + "validate" + ], + "time": "2016-11-23 20:04:41" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "dev", + "stability-flags": { + "phpunit/phpunit": 20 + }, + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/php/phpunit.xml b/php/phpunit.xml new file mode 100755 index 00000000..96a83abe --- /dev/null +++ b/php/phpunit.xml @@ -0,0 +1,26 @@ + + + + + + + + tests/ + + + + + + + + + \ No newline at end of file diff --git a/php/src/Chapter01/Question1_1/Question.php b/php/src/Chapter01/Question1_1/Question.php new file mode 100644 index 00000000..1d36cf53 --- /dev/null +++ b/php/src/Chapter01/Question1_1/Question.php @@ -0,0 +1,33 @@ + 0) { + return false; + } + $checker |= (1 << $value); + } + return true; + } +} \ No newline at end of file diff --git a/php/src/Chapter01/Question1_2/Question.php b/php/src/Chapter01/Question1_2/Question.php new file mode 100644 index 00000000..3855d734 --- /dev/null +++ b/php/src/Chapter01/Question1_2/Question.php @@ -0,0 +1,75 @@ += ord('a') && $ord <= ord('z')) || + ($ord >= ord('A') && $ord <= ord('Z')) + ) { + return $ord - ord('a'); + } else { + return -1; + } + } + + public static function isPermutationOfPalindrome3(string $string): bool + { + $bitVector = self::createBitVector($string); + // one odd letter or no odd letter + return $bitVector == 0 || self::checkExactlyOneBitSet($bitVector); + } + + public static function createBitVector(string $string): int + { + $bitVector = 0; + for ($i = 0; $i < strlen($string); $i++) { + $index = self::getCharNumber($string[$i]); + $bitVector = self::toggle($bitVector, $index); + } + return $bitVector; + } + + /** + * Flips bit at index + * + * @param int $bitVector + * @param int $index + * @return int + */ + public static function toggle(int $bitVector, int $index): int + { + if ($index < 0) { + return $bitVector; + } + + $mask = (1 << $index); + + if (($bitVector & $mask) == 0) { + $bitVector |= $mask; + } else { + // ~00001000 = 11110111 + $bitVector &= ~$mask; + } + + return $bitVector; + } + + public static function checkExactlyOneBitSet(int $bitVector) + { + // 00001000 & 00000111 = 00000000 + return ($bitVector & ($bitVector - 1)) == 0; + } +} \ No newline at end of file diff --git a/php/src/Chapter01/Question1_5/Question.php b/php/src/Chapter01/Question1_5/Question.php new file mode 100644 index 00000000..0b1a849a --- /dev/null +++ b/php/src/Chapter01/Question1_5/Question.php @@ -0,0 +1,82 @@ + 1) { + return false; + } + $shorter = strlen($first) >= strlen($second) ? $second : $first; + $longer = strlen($first) >= strlen($second) ? $first : $second; + + $index1 = 0; + $index2 = 0; + + $foundDifference = false; + + while ($index2 < strlen($longer) && $index1 < strlen($shorter)) { + if ($longer[$index2] != $shorter[$index1]) { + if ($foundDifference) { + return false; + } + $foundDifference = true; + if (strlen($shorter) == strlen($longer)) { + $index1++; // on replace move shorter pointer + } + } else { + $index1++; // if matching move shorter pointer + } + $index2++; // Always move pointer for longer string; + } + return true; + } +} \ No newline at end of file diff --git a/php/src/Chapter01/Question1_6/Question.php b/php/src/Chapter01/Question1_6/Question.php new file mode 100644 index 00000000..f4fe082f --- /dev/null +++ b/php/src/Chapter01/Question1_6/Question.php @@ -0,0 +1,48 @@ += strlen($string) || $string[$i] != $string[$i + 1]) { + $count = strlen((string)$countConsecutive); + $compressedLength += 1 + $count; + $countConsecutive = 0; + } + } + + return $compressedLength; + } + + public static function compress(string $string): string + { + $finalLength = self::countCompression($string); + if ($finalLength >= strlen($string)) { + return $string; + } + $compressedString = ""; + $countConsecutive = 0; + + for ($i = 0; $i < strlen($string); $i++) { + $countConsecutive++; + + // if next char is the last char or next char is different + if ($i + 1 >= strlen($string) || $string[$i] != $string[$i + 1]) { + $compressedString .= $string[$i]; + $compressedString .= $countConsecutive; + $countConsecutive = 0; + } + } + + return $compressedString; + } +} \ No newline at end of file diff --git a/php/src/Chapter01/Question1_7/Question.php b/php/src/Chapter01/Question1_7/Question.php new file mode 100644 index 00000000..d3a6ccda --- /dev/null +++ b/php/src/Chapter01/Question1_7/Question.php @@ -0,0 +1,39 @@ + top + $matrix[$first][$i] = $matrix[$last - $offset][$first]; + + // bottom -> left + $matrix[$last - $offset][$first] = $matrix[$last][$last - $offset]; + + // right -> bottom + $matrix[$last][$last - $offset] = $matrix[$i][$last]; + + // top -> right + $matrix[$i][$last] = $top; + } + } + return $matrix; + } +} \ No newline at end of file diff --git a/php/src/Chapter01/Question1_8/Question.php b/php/src/Chapter01/Question1_8/Question.php new file mode 100644 index 00000000..dc4ce676 --- /dev/null +++ b/php/src/Chapter01/Question1_8/Question.php @@ -0,0 +1,47 @@ + $value) { + self::nullifyRow($matrix, $key); + } + + foreach ($columns as $key => $value) { + self::nullifyColumn($matrix, $key); + } + + return $matrix; + } + + public static function nullifyRow(array &$matrix, int $row) + { + for ($i = 0; $i < sizeof($matrix[0]); $i++) { + $matrix[$row][$i] = 0; + } + } + + public static function nullifyColumn(array &$matrix, $column) + { + for ($i = 0; $i < sizeof($matrix); $i++) { + $matrix[$i][$column] = 0; + } + } + + +} \ No newline at end of file diff --git a/php/src/Chapter01/Question1_9/Question.php b/php/src/Chapter01/Question1_9/Question.php new file mode 100644 index 00000000..3be706dd --- /dev/null +++ b/php/src/Chapter01/Question1_9/Question.php @@ -0,0 +1,22 @@ + 0) { + $string3 = $string2 . $string2; + return self::isSubstring($string3, $string1); + } + return false; + } + + public static function isSubstring(string $bigger, string $smaller): bool + { + return strpos($bigger, $smaller) !== false; + } +} \ No newline at end of file diff --git a/php/src/Chapter02/Question2_5/QuestionA.php b/php/src/Chapter02/Question2_5/QuestionA.php new file mode 100644 index 00000000..63c04fef --- /dev/null +++ b/php/src/Chapter02/Question2_5/QuestionA.php @@ -0,0 +1,48 @@ +data + $list2->data + $carry; + $digit = $sum % 10; + $carry = floor($sum / 10); + } elseif ($list1 && !$list2) { + $digit = $list1->data; + } elseif (!$list1 && $list2) { + $digit = $list2->data; + } + + if ($current->data == null) { + $current->data = $digit; + } else { + $current->setNext(new LinkedListNode(intval($digit))); + $current = $current->next; + } + + + if ($list1) { + $list1 = $list1->next; + } + + if ($list2) { + $list2 = $list2->next; + } + } + + return $result; + } +} \ No newline at end of file diff --git a/php/src/Chapter02/Question2_5/QuestionB.php b/php/src/Chapter02/Question2_5/QuestionB.php new file mode 100644 index 00000000..3d27a10f --- /dev/null +++ b/php/src/Chapter02/Question2_5/QuestionB.php @@ -0,0 +1,108 @@ + $list2length) { + $amount = $list1length - $list2length; + $list2 = self::nodePad($list2, 0, $amount); + } elseif ($list2length > $list1length) { + $amount = $list2length - $list1length; + $list1 = self::nodePad($list1, 0, $amount); + } + + $result = new LinkedListNode(); + $current = $result; + $carry = 0; + $digit = 0; + $sum = 0; + + while ($list1 || $list2) { + + $sum = $list1->data + $list2->data; + $digit = $sum % 10; + $carry = floor($sum / 10); + + + // $current->data can be 0, with '==', 0 could be confused with null + if ($current->data === null) { + if ($carry == 1) { + $result = new LinkedListNode($carry); + $result->setNext($current); + } + $current->data = $digit; + } else { + if ($carry == 1) { + $current->data += $carry; + } + $current->setNext(new LinkedListNode($digit)); + $current = $current->next; + } + + $list1 = $list1->next; + $list2 = $list2->next; + } + + // Scan for cascading carry + $scan = $result; + while ($scan->next) { + if ($scan->next->data > 9) { + $carry = 1; + $scan->next->data %= 10; + $scan->data += $carry; + + // start over + $scan = $result; + if ($scan->data > 9) { + $result = new LinkedListNode(1); + $scan->data %= 10; + $result->setNext($scan); + } + } + $scan = $scan->next; + } + + return $result; + } + + public static function nodePad(LinkedListNode $list, int $value, $amount) + { + // Every time check the if null, if func has no typed params; + if ($amount == 0) { + return $list; + }; + + $newHead = new LinkedListNode($value); + $newTail = $newHead; + + for ($i = 1; $i < $amount; $i++) { + $next = new LinkedListNode($value); + $newTail->setNext($next); + $newTail = $next; + } + + $newTail->setNext($list); + return $newHead; + } + + public static function length(LinkedListNode $list): int + { + $length = 0; + while ($list) { + $length++; + $list = $list->next; + } + return $length; + } +} \ No newline at end of file diff --git a/php/src/CtCILibrary/AssortedMethods.php b/php/src/CtCILibrary/AssortedMethods.php new file mode 100644 index 00000000..ec35f8f6 --- /dev/null +++ b/php/src/CtCILibrary/AssortedMethods.php @@ -0,0 +1,371 @@ +data = $data; + } + if ($next != null) { + $this->setNext($next); + } + if ($prev != null) { + $this->setPrev($prev); + } + } + + public function setNext(LinkedListNode $next) + { + $this->next = $next; + if ($this == $this->last) { + $this->last = $next; + } + if ($next != null && $next->prev != $this) { + $next->setPrev($this); + } + } + + public function setPrev(LinkedListNode $prev) + { + $this->prev = $prev; + if ($prev != null && $prev->next != $this) { + $prev->setNext($this); + } + } + + public function printForward(): string + { + if ($this->next != null) { + return $this->data . "->" . $this->next->printForward(); + } else { + // I don't know why she did this + return strval((int)$this->data); + } + } + + public function cloneNode(): LinkedListNode + { + $next2 = null; + if ($this->next != null) { + // Makes a recursive call + $next2 = $this->next->cloneNode(); + } + + $head2 = new LinkedListNode($this->data, $next2); + return $head2; + } +} \ No newline at end of file diff --git a/php/src/Introduction/CompareBinaryToHex.php b/php/src/Introduction/CompareBinaryToHex.php new file mode 100644 index 00000000..98dcfcf0 --- /dev/null +++ b/php/src/Introduction/CompareBinaryToHex.php @@ -0,0 +1,66 @@ += ord("0") && $asciiValue <= ord("9")) { + return intval($digit); + } elseif ($asciiValue >= ord("A") && $asciiValue <= ord("F")) { + return 10 + (ord($digit) - ord("A")); + } elseif ($asciiValue >= ord("a") && $asciiValue <= ord("f")) { + return 10 + (ord($digit) - ord("a")); + } + return -1; + } + + /* + * Converts the number to it's decimal value + * Starts from leftmost digit. + * + * @param string $number + * @param int $base + * @return int + */ + public static function convertToBase(string $number, int $base) + { + if ($base < 2 || ($base > 10 && $base != 16)) { + return -1; + } + $value = 0; + for ($i = strlen($number) - 1; $i >= 0; $i--) { + $digit = self::digitToValue($number[$i]); + + if ($digit < 0 || $digit >= $base) { + return -1; + } + + $exp = strlen($number) - 1 - $i; + $value += $digit * pow($base, $exp); + } + + return $value; + } + + /** + * Compares string values of a binary and hex numbers. + * + * @param string $binary + * @param string $hex + * @return bool + */ + public static function compareBinToHex(string $binary, string $hex) + { + $number1 = self::convertToBase($binary, 2); + $number2 = self::convertToBase($hex, 16); + + if ($number1 < 0 || $number2 < 0) { + return false; + } else { + return $number1 == $number2; + } + } +} \ No newline at end of file diff --git a/php/src/Introduction/SwapMinMax.php b/php/src/Introduction/SwapMinMax.php new file mode 100644 index 00000000..fe0b45f5 --- /dev/null +++ b/php/src/Introduction/SwapMinMax.php @@ -0,0 +1,163 @@ + $array[$maxIndex]) { + $maxIndex = key($array); + } + next($array); + } + return $maxIndex; + } + + /** + * Find indexes in one pass, and swap them. + * + * @param $array int[] + */ + public static function swapMinMaxWithPointers(array &$array) + { + reset($array); + $maxIndex = key($array); + $minIndex = key($array); + + while (current($array)) { + if (current($array) > $array[$maxIndex]) { + $maxIndex = key($array); + } + if (current($array) < $array[$minIndex]) { + $minIndex = key($array); + } + next($array); + } + + $temp = $array[$maxIndex]; + $array[$maxIndex] = $array[$minIndex]; + $array[$minIndex] = $temp; + } + + + /** + * Returns min integer key of the array + * Can return only integer keys + * + * @param $array int[] + * @return int + */ + public static function getMinIndex(array $array): int + { + $minIndex = 0; + for ($i = 1; $i < sizeof($array); $i++) { + if ($array[$i] < $array[$minIndex]) { + $minIndex = $i; + } + } + return $minIndex; + } + + /** + * Returns max integer key of the array + * Can return only integer keys + * + * @param $array int[] + * @return int + */ + public static function getMaxIndex(array $array): int + { + $maxIndex = 0; + for ($i = 1; $i < sizeof($array); $i++) { + if ($array[$i] > $array[$maxIndex]) { + $maxIndex = $i; + } + } + return $maxIndex; + } + + /** + * Swaps desired element with key. + * + * Note: The keys should not be integer only. + * Think about a solution with mixed keys. Hence arrays are + * hashMaps in PHP. + * + * @param $array int [] + * @param int $first + * @param int $second + */ + public static function swap(array &$array, int $first, int $second) + { + $temp = $array[$first]; + $array[$first] = $array[$second]; + $array[$second] = $temp; + } + + /** + * @param $array int [] + */ + public static function swapMinMaxBetter(array &$array) + { + $minIndex = self::getMinIndex($array); + $maxIndex = self::getMaxIndex($array); + self::swap($array, $minIndex, $maxIndex); + } + + /** + * @param $array int [] + */ + public static function swapMinMax(array &$array) + { + $minIndex = 0; + for ($i = 1; $i < sizeof($array); $i++) { + if ($array[$i] < $array[$minIndex]) { + $minIndex = $i; + } + } + + $maxIndex = 0; + for ($i = 1; $i < sizeof($array); $i++) { + if ($array[$i] > $array[$maxIndex]) { + $maxIndex = $i; + } + } + + $temp = $array[$minIndex]; + $array[$minIndex] = $array[$maxIndex]; + $array[$maxIndex] = $temp; + } +} \ No newline at end of file diff --git a/php/tests/Chapter01/Question1_1Test.php b/php/tests/Chapter01/Question1_1Test.php new file mode 100644 index 00000000..f61b1406 --- /dev/null +++ b/php/tests/Chapter01/Question1_1Test.php @@ -0,0 +1,33 @@ +words = [ + 'abcde' => true, + 'hello' => false, + 'apple' => false, + 'kite' => true, + 'padle' => true + ]; + } + + public function testIsUniqueChars() + { + foreach ($this->words as $word => $result) { + $this->assertEquals(Question::isUniqueChars($word), $result); + } + } + + public function testIsUniqueChars2() + { + foreach ($this->words as $word => $result) { + $this->assertEquals(Question::isUniqueChars2($word), $result); + } + } +} \ No newline at end of file diff --git a/php/tests/Chapter01/Question1_2Test.php b/php/tests/Chapter01/Question1_2Test.php new file mode 100644 index 00000000..612d48a0 --- /dev/null +++ b/php/tests/Chapter01/Question1_2Test.php @@ -0,0 +1,31 @@ +words = [ + ['apple', 'papel', true], + ['carrot', 'tarroc', true], + ['hello', 'llloh', false] + ]; + } + + public function testIsPermutation() + { + foreach ($this->words as $check) { + $this->assertEquals($check[2], Question::isPermutation($check[0], $check[1])); + } + } + + public function testIsPermutation2() + { + foreach ($this->words as $check) { + $this->assertEquals($check[2], Question::isPermutation2($check[0], $check[1])); + } + } +} \ No newline at end of file diff --git a/php/tests/Chapter01/Question1_3Test.php b/php/tests/Chapter01/Question1_3Test.php new file mode 100644 index 00000000..8a9148a4 --- /dev/null +++ b/php/tests/Chapter01/Question1_3Test.php @@ -0,0 +1,18 @@ +word = ['abc d e f', 'abc%20d%20e%20f']; + } + + public function testReplaceSpaces() + { + $this->assertEquals($this->word[1], Question::replaceSpaces($this->word[0])); + } +} \ No newline at end of file diff --git a/php/tests/Chapter01/Question1_4Test.php b/php/tests/Chapter01/Question1_4Test.php new file mode 100644 index 00000000..8f6a6986 --- /dev/null +++ b/php/tests/Chapter01/Question1_4Test.php @@ -0,0 +1,34 @@ +words = ['Tact Coa' => true, 'mustafa' => false]; + } + + public function testIsPermutationOfPalindrome() + { + foreach ($this->words as $word => $result) { + $this->assertEquals($result, Question::isPermutationOfPalindrome($word)); + } + } + + public function testIsPermutationOfPalindrome2() + { + foreach ($this->words as $word => $result) { + $this->assertEquals($result, Question::isPermutationOfPalindrome2($word)); + } + } + + public function testIsPermutationOfPalindrome3() + { + foreach ($this->words as $word => $result) { + $this->assertEquals($result, Question::isPermutationOfPalindrome3($word)); + } + } +} \ No newline at end of file diff --git a/php/tests/Chapter01/Question1_5Test.php b/php/tests/Chapter01/Question1_5Test.php new file mode 100644 index 00000000..9245e235 --- /dev/null +++ b/php/tests/Chapter01/Question1_5Test.php @@ -0,0 +1,31 @@ +words = [ + ['apple', 'appla', true], + ['apple', 'aple', true], + ['apple', 'app', false] + ]; + } + + public function testOneEditReplace() + { + foreach ($this->words as $check) { + $this->assertEquals($check[2], Question::oneEditAway($check[0], $check[1])); + } + } + + public function testOneEditReplace2() + { + foreach ($this->words as $check) { + $this->assertEquals($check[2], Question::oneEditAway2($check[0], $check[1])); + } + } +} \ No newline at end of file diff --git a/php/tests/Chapter01/Question1_6Test.php b/php/tests/Chapter01/Question1_6Test.php new file mode 100644 index 00000000..f6de58a4 --- /dev/null +++ b/php/tests/Chapter01/Question1_6Test.php @@ -0,0 +1,22 @@ +words = [ + 'aabcccccaaa' => 'a2b1c5a3' + ]; + } + + public function testIsPermutation() + { + foreach ($this->words as $uncompressed => $compressed) { + $this->assertEquals($compressed, Question::compress($uncompressed)); + } + } +} \ No newline at end of file diff --git a/php/tests/Chapter01/Question1_7Test.php b/php/tests/Chapter01/Question1_7Test.php new file mode 100644 index 00000000..79113859 --- /dev/null +++ b/php/tests/Chapter01/Question1_7Test.php @@ -0,0 +1,46 @@ +matrix1 = [ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12], + [13, 14, 15, 16] + ]; + + $this->matrix1rotated = [ + [13, 9, 5, 1], + [14, 10, 6, 2], + [15, 11, 7, 3], + [16, 12, 8, 4] + ]; + + $this->matrix2 = [ + [1, 2, 3], + [5, 6, 7], + [9, 10, 11] + ]; + + $this->matrix2rotated = [ + [9, 5, 1], + [10, 6, 2], + [11, 7, 3] + ]; + } + + public function testRotate() + { + $this->assertEquals($this->matrix1rotated, Question::rotate($this->matrix1)); + $this->assertEquals($this->matrix2rotated, Question::rotate($this->matrix2)); + } +} \ No newline at end of file diff --git a/php/tests/Chapter01/Question1_8Test.php b/php/tests/Chapter01/Question1_8Test.php new file mode 100644 index 00000000..2a4ad7db --- /dev/null +++ b/php/tests/Chapter01/Question1_8Test.php @@ -0,0 +1,31 @@ +matrix = [ + [1, 2, 0, 4], + [5, 6, 7, 8], + [0, 10, 11, 12], + [13, 14, 15, 16] + ]; + + $this->matrixZeros = [ + [0, 0, 0, 0], + [0, 6, 0, 8], + [0, 0, 0, 0], + [0, 14, 0, 16] + ]; + + } + public function testSetZeros() + { + $this->assertEquals($this->matrixZeros, Question::setZeros($this->matrix)); + } +} \ No newline at end of file diff --git a/php/tests/Chapter01/Question1_9Test.php b/php/tests/Chapter01/Question1_9Test.php new file mode 100644 index 00000000..9b0e5d58 --- /dev/null +++ b/php/tests/Chapter01/Question1_9Test.php @@ -0,0 +1,25 @@ +words = [ + ['apple', 'pleap', true], + ['waterbottle', 'erbottlewat', true], + ['camera', 'macera', false] + ]; + } + + + public function testIsRotation() + { + foreach ($this->words as $check) { + $this->assertEquals($check[2], Question::isRotation($check[0], $check[1])); + } + } +} \ No newline at end of file diff --git a/php/tests/Chapter02/Question2_5Test.php b/php/tests/Chapter02/Question2_5Test.php new file mode 100644 index 00000000..6fb8e5b1 --- /dev/null +++ b/php/tests/Chapter02/Question2_5Test.php @@ -0,0 +1,58 @@ +assertEquals($result->printForward(), $response->printForward()); + } + + public function testQuestionB() + { + $list1 = new LinkedListNode(6, new LinkedListNode(1, new LinkedListNode(7))); + $list2 = new LinkedListNode(2, new LinkedListNode(9, new LinkedListNode(5))); + $result = new LinkedListNode(9, new LinkedListNode(1, new LinkedListNode(2))); + $response = QuestionB::addLists($list1, $list2); + + $this->assertEquals($result->printForward(), $response->printForward()); + } + + public function testQuestionBCarryCascade() + { + $list1 = new LinkedListNode(5, new LinkedListNode(6, new LinkedListNode(9))); + $list2 = new LinkedListNode(7, new LinkedListNode(8, new LinkedListNode(4))); + $result = new LinkedListNode(1, new LinkedListNode(3, new LinkedListNode(5, new LinkedListNode(3)))); + $response = QuestionB::addLists($list1, $list2); + + $this->assertEquals($result->printForward(), $response->printForward()); + } + + public function testQuestionBCarryCascade2() + { + $list1 = new LinkedListNode(4, new LinkedListNode(4, new LinkedListNode(5))); + $list2 = new LinkedListNode(5, new LinkedListNode(5, new LinkedListNode(5))); + $result = new LinkedListNode(1, new LinkedListNode(0, new LinkedListNode(0, new LinkedListNode(0)))); + $response = QuestionB::addLists($list1, $list2); + + $this->assertEquals($result->printForward(), $response->printForward()); + } + + public function testQuestionBDiffSize() + { + $list1 = new LinkedListNode(6, new LinkedListNode(1, new LinkedListNode(7))); + $list2 = new LinkedListNode(2, new LinkedListNode(9)); + $result = new LinkedListNode(6, new LinkedListNode(4, new LinkedListNode(6))); + $response = QuestionB::addLists($list1, $list2); + + $this->assertEquals($result->printForward(), $response->printForward()); + } +} \ No newline at end of file diff --git a/php/tests/Introduction/CompareBinaryToHexTest.php b/php/tests/Introduction/CompareBinaryToHexTest.php new file mode 100755 index 00000000..f3376b53 --- /dev/null +++ b/php/tests/Introduction/CompareBinaryToHexTest.php @@ -0,0 +1,24 @@ +assertEquals(12, $value); + } + + public function testConvertToBase() + { + $converted = CompareBinaryToHex::convertToBase("100000", 2); + $this->assertEquals(32, $converted); + } + + public function testCompareBinToHex() + { + $compared = CompareBinaryToHex::compareBinToHex("111001011", "1CB"); + $this->assertTrue($compared); + } +} diff --git a/php/tests/Introduction/SwapMinMaxTest.php b/php/tests/Introduction/SwapMinMaxTest.php new file mode 100755 index 00000000..4a74db06 --- /dev/null +++ b/php/tests/Introduction/SwapMinMaxTest.php @@ -0,0 +1,56 @@ +array); + $this->assertEquals(1, $value); + } + + public function testGetMaxIndexWithPointers() + { + $value = SwapMinMax::getMaxIndexWithPointers($this->array); + $this->assertEquals(0, $value); + } + + public function testSwapMinMaxWithPointers() + { + SwapMinMax::swapMinMaxWithPointers($this->array); + $this->assertEquals([-10, 10, 10], $this->array); + } + + public function testGetMinIndex() + { + $value = SwapMinMax::getMinIndex($this->array); + $this->assertEquals(1, $value); + } + + public function testGetMaxIndex() + { + $value = SwapMinMax::getMaxIndex($this->array); + $this->assertEquals(0, $value); + } + + public function testSwap() + { + SwapMinMax::swap($this->array, 1, 2); + $this->assertEquals([10, 10, -10], $this->array); + } + + public function testSwapMinMaxBetter() + { + SwapMinMax::swapMinMaxBetter($this->array); + $this->assertEquals([-10, 10, 10], $this->array); + } + + public function testSwapMinMax() + { + SwapMinMax::swapMinMax($this->array); + $this->assertEquals([-10, 10, 10], $this->array); + } +}