Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEsaulenko/hw18. Практикум решения алгоритмических задач 2 #584

Open
wants to merge 2 commits into
base: DEsaulenko/main
Choose a base branch
from
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
Expand Up @@ -14,3 +14,4 @@
# Другие файлы
.idea/
.DS_Store
vendor/
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions app/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "desaulenko/hw18",
"type": "library",
"autoload": {
"psr-4": {
"DEsaulenko\\Hw18\\": "src/"
}
},
"require": {
"symfony/var-dumper": "^6.3"
}
}
253 changes: 253 additions & 0 deletions app/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions app/fraction-to-recurring-decimal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace DEsaulenko\Hw18\Fraction;

require_once 'vendor/autoload.php';

/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val) { $this->val = $val; }
* }
*/
class Solution
{
/**
* @param Integer $numerator
* @param Integer $denominator
* @return String
*/
public 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;
}

protected 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];
$solution = new Solution();
foreach ($numerators as $i => $val) {
dump([
$val / $denumerators[$i],
$solution->fractionToDecimal($val, $denumerators[$i])
]);
}
Loading
Loading