From 8c82712466ab6c9a2fd0f8009beddafd0a054f20 Mon Sep 17 00:00:00 2001 From: "D.Esaulenko" Date: Thu, 14 Sep 2023 00:39:28 +0300 Subject: [PATCH 1/2] =?UTF-8?q?DEsaulenko/hw18.=20=D0=9F=D1=80=D0=B0=D0=BA?= =?UTF-8?q?=D1=82=D0=B8=D0=BA=D1=83=D0=BC=20=D1=80=D0=B5=D1=88=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B0=D0=BB=D0=B3=D0=BE=D1=80=D0=B8=D1=82=D0=BC?= =?UTF-8?q?=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=B8=D1=85=20=D0=B7=D0=B0=D0=B4?= =?UTF-8?q?=D0=B0=D1=87=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + README.md | 8 +- app/composer.json | 12 ++ app/composer.lock | 253 +++++++++++++++++++++++ app/fraction-to-recurring-decimal.php | 74 +++++++ app/intersection-of-two-linked-lists.php | 64 ++++++ app/src/ListNode.php | 16 ++ 7 files changed, 427 insertions(+), 1 deletion(-) create mode 100644 app/composer.json create mode 100644 app/composer.lock create mode 100644 app/fraction-to-recurring-decimal.php create mode 100644 app/intersection-of-two-linked-lists.php create mode 100644 app/src/ListNode.php diff --git a/.gitignore b/.gitignore index 8fb782735..471970248 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ # Другие файлы .idea/ .DS_Store +vendor/ \ No newline at end of file diff --git a/README.md b/README.md index e16b2a49b..552bde7a8 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ # PHP_2023 -https://otus.ru/lessons/razrabotchik-php/?utm_source=github&utm_medium=free&utm_campaign=otus +### HW18 + +#### https://leetcode.com/problems/intersection-of-two-linked-lists/ +Сложность 0(n+m) + +#### https://leetcode.com/problems/fraction-to-recurring-decimal/ +Сложность 0(n) \ No newline at end of file diff --git a/app/composer.json b/app/composer.json new file mode 100644 index 000000000..fd5c770c9 --- /dev/null +++ b/app/composer.json @@ -0,0 +1,12 @@ +{ + "name": "desaulenko/hw18", + "type": "library", + "autoload": { + "psr-4": { + "DEsaulenko\\Hw18\\": "src/" + } + }, + "require": { + "symfony/var-dumper": "^6.3" + } +} diff --git a/app/composer.lock b/app/composer.lock new file mode 100644 index 000000000..4c27237ec --- /dev/null +++ b/app/composer.lock @@ -0,0 +1,253 @@ +{ + "_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#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "76faca479ae8f9d9bc402c93723054cb", + "packages": [ + { + "name": "symfony/deprecation-contracts", + "version": "v3.3.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/deprecation-contracts.git", + "reference": "7c3aff79d10325257a001fcf92d991f24fc967cf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/7c3aff79d10325257a001fcf92d991f24fc967cf", + "reference": "7c3aff79d10325257a001fcf92d991f24fc967cf", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "3.4-dev" + }, + "thanks": { + "name": "symfony/contracts", + "url": "https://github.com/symfony/contracts" + } + }, + "autoload": { + "files": [ + "function.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "A generic function and convention to trigger deprecation notices", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/deprecation-contracts/tree/v3.3.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2023-05-23T14:45:45+00:00" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.28.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "42292d99c55abe617799667f454222c54c60e229" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/42292d99c55abe617799667f454222c54c60e229", + "reference": "42292d99c55abe617799667f454222c54c60e229", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "provide": { + "ext-mbstring": "*" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.28-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.28.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2023-07-28T09:04:16+00:00" + }, + { + "name": "symfony/var-dumper", + "version": "v6.3.4", + "source": { + "type": "git", + "url": "https://github.com/symfony/var-dumper.git", + "reference": "2027be14f8ae8eae999ceadebcda5b4909b81d45" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/var-dumper/zipball/2027be14f8ae8eae999ceadebcda5b4909b81d45", + "reference": "2027be14f8ae8eae999ceadebcda5b4909b81d45", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "symfony/deprecation-contracts": "^2.5|^3", + "symfony/polyfill-mbstring": "~1.0" + }, + "conflict": { + "symfony/console": "<5.4" + }, + "require-dev": { + "ext-iconv": "*", + "symfony/console": "^5.4|^6.0", + "symfony/http-kernel": "^5.4|^6.0", + "symfony/process": "^5.4|^6.0", + "symfony/uid": "^5.4|^6.0", + "twig/twig": "^2.13|^3.0.4" + }, + "bin": [ + "Resources/bin/var-dump-server" + ], + "type": "library", + "autoload": { + "files": [ + "Resources/functions/dump.php" + ], + "psr-4": { + "Symfony\\Component\\VarDumper\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides mechanisms for walking through any arbitrary PHP variable", + "homepage": "https://symfony.com", + "keywords": [ + "debug", + "dump" + ], + "support": { + "source": "https://github.com/symfony/var-dumper/tree/v6.3.4" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2023-08-24T14:51:05+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.2.0" +} diff --git a/app/fraction-to-recurring-decimal.php b/app/fraction-to-recurring-decimal.php new file mode 100644 index 000000000..07af7b75b --- /dev/null +++ b/app/fraction-to-recurring-decimal.php @@ -0,0 +1,74 @@ +val = $val; } + * } + */ +class Solution +{ + + /** + * @param Integer $numerator + * @param Integer $denominator + * @return String + */ + function fractionToDecimal($numerator, $denominator) + { + if ($denominator === 0) { + return ''; + } + + $negative = ($numerator * $denominator) < 0 ? '-' : ''; + + $numerator = abs($numerator); + $denominator = abs($denominator); + + $left = floor($numerator / $denominator); + $right = $this->prepareRight($numerator, $denominator); + + return $negative . $left . $right; + } + + function prepareRight($numerator, $denominator) + { + $result = '.'; + + $store = []; + $tmp = $numerator % $denominator; + while ($tmp !== 0) { + $store[$tmp] = strlen($result); + $tmp *= 10; + $cur = floor($tmp / $denominator); + $tmp %= $denominator; + if (isset($store[$tmp])) { + return substr($result, 0, $store[$tmp]) . "(" . substr($result, $store[$tmp]) . $cur . ")"; + } + $result .= $cur; + } + + return $result === '.' ? '' : $result; + } +} + +$numerators = [1, 2, 4, 1, 4, 6, 4, 1, -50, 1]; +$denumerators = [2, 1, 333, 3, 3, 73, 11, 6, 8, 214748364]; +//$numerators = [1]; +//$denumerators = [3]; +$solution = new Solution(); +foreach ($numerators as $i => $val) { + dump([ + $val / $denumerators[$i], + $solution->fractionToDecimal($val, $denumerators[$i]) + ]); +} + diff --git a/app/intersection-of-two-linked-lists.php b/app/intersection-of-two-linked-lists.php new file mode 100644 index 000000000..2c86e9eec --- /dev/null +++ b/app/intersection-of-two-linked-lists.php @@ -0,0 +1,64 @@ +val = $val; } + * } + */ +class Solution +{ + /** + * @param ListNode $headA + * @param ListNode $headB + * @return ListNode + */ + function getIntersectionNode($headA, $headB) + { + if ($headA === $headB) { + return $headA; + } + $lengthHeadA = $this->getLength($headA); + $lengthHeadB = $this->getLength($headB); + if ($lengthHeadA > $lengthHeadB) { + return $this->findIntersection($headA, $headB, $lengthHeadA - $lengthHeadB); + } + return $this->findIntersection($headB, $headA, $lengthHeadB - $lengthHeadA); + } + + function findIntersection(ListNode $first, ListNode $second, int $count): ?ListNode + { + for ($i = 0; $i < $count; $i++) { + $first = $first->next; + } + while ( + $first + && $second + ) { + if ($first === $second) { + return $first; + } + $first = $first->next; + $second = $second->next; + } + return null; + } + + function getLength(ListNode $list) + { + $count = 0; + while ($list !== null) { + ++$count; + $list = $list->next; + } + return $count; + } +} diff --git a/app/src/ListNode.php b/app/src/ListNode.php new file mode 100644 index 000000000..ce33dc7bd --- /dev/null +++ b/app/src/ListNode.php @@ -0,0 +1,16 @@ +val = $val; + } +} From 503f08e292156d5652704d0582a8bb40eeba53cd Mon Sep 17 00:00:00 2001 From: "D.Esaulenko" Date: Thu, 14 Sep 2023 00:44:34 +0300 Subject: [PATCH 2/2] DEsaulenko/hw18. fix psr --- app/fraction-to-recurring-decimal.php | 10 +++------- app/intersection-of-two-linked-lists.php | 8 +++++--- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/app/fraction-to-recurring-decimal.php b/app/fraction-to-recurring-decimal.php index 07af7b75b..a0bdd7604 100644 --- a/app/fraction-to-recurring-decimal.php +++ b/app/fraction-to-recurring-decimal.php @@ -2,7 +2,7 @@ declare(strict_types=1); -use DEsaulenko\Hw18\ListNode; +namespace DEsaulenko\Hw18\Fraction; require_once 'vendor/autoload.php'; @@ -16,13 +16,12 @@ */ class Solution { - /** * @param Integer $numerator * @param Integer $denominator * @return String */ - function fractionToDecimal($numerator, $denominator) + public function fractionToDecimal($numerator, $denominator) { if ($denominator === 0) { return ''; @@ -39,7 +38,7 @@ function fractionToDecimal($numerator, $denominator) return $negative . $left . $right; } - function prepareRight($numerator, $denominator) + protected function prepareRight($numerator, $denominator) { $result = '.'; @@ -62,8 +61,6 @@ function prepareRight($numerator, $denominator) $numerators = [1, 2, 4, 1, 4, 6, 4, 1, -50, 1]; $denumerators = [2, 1, 333, 3, 3, 73, 11, 6, 8, 214748364]; -//$numerators = [1]; -//$denumerators = [3]; $solution = new Solution(); foreach ($numerators as $i => $val) { dump([ @@ -71,4 +68,3 @@ function prepareRight($numerator, $denominator) $solution->fractionToDecimal($val, $denumerators[$i]) ]); } - diff --git a/app/intersection-of-two-linked-lists.php b/app/intersection-of-two-linked-lists.php index 2c86e9eec..b11c3221f 100644 --- a/app/intersection-of-two-linked-lists.php +++ b/app/intersection-of-two-linked-lists.php @@ -2,6 +2,8 @@ declare(strict_types=1); +namespace DEsaulenko\Hw18\Intersection; + use DEsaulenko\Hw18\ListNode; require_once 'vendor/autoload.php'; @@ -21,7 +23,7 @@ class Solution * @param ListNode $headB * @return ListNode */ - function getIntersectionNode($headA, $headB) + public function getIntersectionNode($headA, $headB) { if ($headA === $headB) { return $headA; @@ -34,7 +36,7 @@ function getIntersectionNode($headA, $headB) return $this->findIntersection($headB, $headA, $lengthHeadB - $lengthHeadA); } - function findIntersection(ListNode $first, ListNode $second, int $count): ?ListNode + protected function findIntersection(ListNode $first, ListNode $second, int $count): ?ListNode { for ($i = 0; $i < $count; $i++) { $first = $first->next; @@ -52,7 +54,7 @@ function findIntersection(ListNode $first, ListNode $second, int $count): ?ListN return null; } - function getLength(ListNode $list) + protected function getLength(ListNode $list) { $count = 0; while ($list !== null) {