From 9a5205a3a71d4cd01c60ab286b6d835307e83811 Mon Sep 17 00:00:00 2001 From: Michal Hanajik Date: Mon, 15 Oct 2018 10:49:35 +0100 Subject: [PATCH 1/6] Fixed bubble_sort --- contents/bubble_sort/bubble_sort.md | 2 +- contents/bubble_sort/code/php/bubble_sort.php | 25 +++++++++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/contents/bubble_sort/bubble_sort.md b/contents/bubble_sort/bubble_sort.md index 84903ee55..6b92712b8 100644 --- a/contents/bubble_sort/bubble_sort.md +++ b/contents/bubble_sort/bubble_sort.md @@ -47,7 +47,7 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with {% sample lang="crystal" %} [import:1-11, lang:"crystal"](code/crystal/bubble.cr) {% sample lang="php" %} -[import:3-15, lang:"php"](code/php/bubble_sort.php) +[import:3-16, lang:"php"](code/php/bubble_sort.php) {% sample lang="lisp" %} [import:3-28, lang:"lisp"](code/lisp/bubble_sort.lisp) {% sample lang="nim" %} diff --git a/contents/bubble_sort/code/php/bubble_sort.php b/contents/bubble_sort/code/php/bubble_sort.php index 50fbf547e..77366da4b 100644 --- a/contents/bubble_sort/code/php/bubble_sort.php +++ b/contents/bubble_sort/code/php/bubble_sort.php @@ -2,20 +2,23 @@ function bubble_sort(array $arr): array { - for ($i = 0, $length = count($arr); $i < $length; $i++) { - for ($j = 1; $j < $length; $j++) { - if ($arr[$j - 1] > $arr[$j]) { - $tmp = $arr[$j - 1]; - $arr[$j - 1] = $arr[$j]; - $arr[$j] = $tmp; - } + for ($i = 0, $length = count($arr); $i < $length; $i++) { + for ($j = 1; $j < $length; $j++) { + if ($arr[$j - 1] > $arr[$j]) { + $tmp = $arr[$j - 1]; + $arr[$j - 1] = $arr[$j]; + $arr[$j] = $tmp; + } + } } - } - return $arr; + + return $arr; } $unsorted = [1, 2, 6, 47, 4, 9, 3, 7, 8, 23, 15]; $bubble_sorted = bubble_sort($unsorted); -echo sprintf('Unsorted: %s%s', implode(',', $unsorted), PHP_EOL); -echo sprintf('Sorted: %s%s', implode(',', $bubble_sorted), PHP_EOL); +printf('Unsorted: %s', implode(',', $unsorted)); +echo PHP_EOL; +printf('Sorted: %s', implode(',', $bubble_sorted)); +echo PHP_EOL; From 238df2587af5d383b14b9ec217b9ab101d18dbd3 Mon Sep 17 00:00:00 2001 From: Michal Hanajik Date: Mon, 15 Oct 2018 10:51:52 +0100 Subject: [PATCH 2/6] Fixed bogo_sort --- contents/bogo_sort/bogo_sort.md | 2 +- contents/bogo_sort/code/php/bogo_sort.php | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/contents/bogo_sort/bogo_sort.md b/contents/bogo_sort/bogo_sort.md index d23475740..08cbf2e2d 100644 --- a/contents/bogo_sort/bogo_sort.md +++ b/contents/bogo_sort/bogo_sort.md @@ -40,7 +40,7 @@ In code, it looks something like this: {% sample lang="swift" %} [import:13-19, lang:"swift"](code/swift/bogosort.swift) {% sample lang="php" %} -[import:11-16, lang:"php"](code/php/bogo_sort.php) +[import:14-21, lang:"php"](code/php/bogo_sort.php) {% sample lang="nim" %} [import:16-18, lang:"nim"](code/nim/bogo_sort.nim) {% sample lang="ruby" %} diff --git a/contents/bogo_sort/code/php/bogo_sort.php b/contents/bogo_sort/code/php/bogo_sort.php index 96a299b62..ca2edf23f 100644 --- a/contents/bogo_sort/code/php/bogo_sort.php +++ b/contents/bogo_sort/code/php/bogo_sort.php @@ -2,22 +2,29 @@ function is_sorted(array $array): bool { - for ($i = 0, $count = count($array); $i < $count - 1; $i++) - if ($array[$i] < $array[$i + 1]) return false; + for ($i = 0, $count = count($array); $i < $count - 1; $i++) { + if ($array[$i] > $array[$i + 1]) { + return false; + } + } - return true; + return true; } function bogo_sort(array $array): array { - if (!is_sorted($array)) shuffle($array); + while (!is_sorted($array)) { + shuffle($array); + } - return $array; + return $array; } $unsorted = [10, 7, 3, 1, 4, 8, 5, 6, 9, 2]; $bogo_sorted = bogo_sort($unsorted); -echo sprintf('Unsorted: %s%s', implode(',', $unsorted), PHP_EOL); -echo sprintf('Sorted: %s%s', implode(',', $bogo_sorted), PHP_EOL); +printf('Unsorted: %s', implode(',', $unsorted)); +echo PHP_EOL; +printf('Sorted: %s', implode(',', $bogo_sorted)); +echo PHP_EOL; From 587e9b30ab67ef11e6a699e8791dc81cfa1cd9e8 Mon Sep 17 00:00:00 2001 From: Michal Hanajik Date: Mon, 15 Oct 2018 11:01:24 +0100 Subject: [PATCH 3/6] Fix tree_traversal --- .../code/php/tree_traversal.php | 176 ++++++++++-------- contents/tree_traversal/tree_traversal.md | 12 +- 2 files changed, 105 insertions(+), 83 deletions(-) diff --git a/contents/tree_traversal/code/php/tree_traversal.php b/contents/tree_traversal/code/php/tree_traversal.php index d5fa314a8..211813381 100644 --- a/contents/tree_traversal/code/php/tree_traversal.php +++ b/contents/tree_traversal/code/php/tree_traversal.php @@ -1,121 +1,143 @@ id = $id; - $this->children = $children; - } + public function __construct(int $id, array $children = []) + { + $this->id = $id; + $this->children = $children; + } - public function getId(): int { return $this->id; } + public function getId(): int + { + return $this->id; + } - public function getChildren(): array { return $this->children; } + public function getChildren(): array + { + return $this->children; + } - public function addChild(Tree $child): void { $this->children[] = $child; } + public function addChild(Tree $child): void + { + $this->children[] = $child; + } - public function jsonSerialize(): array - { - return [ - 'id' => $this->id, - 'children' => $this->children - ]; - } + public function jsonSerialize(): array + { + return [ + 'id' => $this->id, + 'children' => $this->children, + ]; + } } class TreeTraversal { - public static function dfs_recursive(Tree $tree): void - { - if ($tree->getId()) echo $tree->getId() . PHP_EOL; - foreach ($tree->getChildren() as $child) static::dfs_recursive($child); - } - - public static function dfs_recursive_postorder(Tree $tree): void - { - foreach ($tree->getChildren() as $child) static::dfs_recursive_postorder($child); - echo $tree->getId() . PHP_EOL; - } - - public static function dfs_recursive_inorder_binary(Tree $tree): void - { - switch (count($tree->getChildren())) { - case 2: - static::dfs_recursive_inorder_binary($tree->getChildren()[0]); - echo $tree->getId() . PHP_EOL; - static::dfs_recursive_inorder_binary($tree->getChildren()[1]); - break; - case 1: - static::dfs_recursive_inorder_binary($tree->getChildren()[0]); - echo $tree->getId() . PHP_EOL; - break; - case 0: - echo $tree->getId() . PHP_EOL; - break; - default: - throw new InvalidArgumentException('Not a binary tree!'); - break; + public static function DFSRecursive(Tree $tree): void + { + if ($tree->getId()) { + echo $tree->getId() . PHP_EOL; + } + foreach ($tree->getChildren() as $child) { + static::DFSRecursive($child); + } } - } - public static function dfs_stack(Tree $tree): void - { - $stack = [$tree]; - $temp = null; + public static function DFSRecursivePostorder(Tree $tree): void + { + foreach ($tree->getChildren() as $child) { + static::DFSRecursivePostorder($child); + } + echo $tree->getId() . PHP_EOL; + } - while (null !== ($temp = array_pop($stack))) { - echo $temp->getId() . PHP_EOL; - foreach ($temp->getChildren() as $child) $stack[] = $child; + public static function DFSRecursiveInorderBinary(Tree $tree): void + { + switch (count($tree->getChildren())) { + case 2: + static::DFSRecursiveInorderBinary($tree->getChildren()[0]); + echo $tree->getId() . PHP_EOL; + static::DFSRecursiveInorderBinary($tree->getChildren()[1]); + break; + case 1: + static::DFSRecursiveInorderBinary($tree->getChildren()[0]); + echo $tree->getId() . PHP_EOL; + break; + case 0: + echo $tree->getId() . PHP_EOL; + break; + default: + throw new InvalidArgumentException('Not a binary tree!'); + break; + } } - } - public static function dfs_queue(Tree $tree): void - { - $stack = [$tree]; - $temp = null; + public static function DFSStack(Tree $tree): void + { + $stack = [$tree]; + $temp = null; + + while (null !== ($temp = array_pop($stack))) { + echo $temp->getId() . PHP_EOL; + foreach ($temp->getChildren() as $child) { + $stack[] = $child; + } + } + } - while (null !== ($temp = array_shift($stack))) { - echo $temp->getId() . PHP_EOL; - foreach ($temp->getChildren() as $child) $stack[] = $child; + public static function DFSQueue(Tree $tree): void + { + $stack = [$tree]; + $temp = null; + + while (null !== ($temp = array_shift($stack))) { + echo $temp->getId() . PHP_EOL; + foreach ($temp->getChildren() as $child) { + $stack[] = $child; + } + } } - } } function generate_tree(int $num_of_rows, int $num_of_children, int $id = -1): Tree { - if ($id === -1) $id = 1; - $node = new Tree($id); + if ($id === -1) { + $id = 1; + } + $node = new Tree($id); - if ($num_of_rows > 1) - for ($i = 0; $i < $num_of_children; $i++) { - $child = generate_tree($num_of_rows - 1, $num_of_children, $id * 10 + $i + 1); - $node->addChild($child); + if ($num_of_rows > 1) { + for ($i = 0; $i < $num_of_children; $i++) { + $child = generate_tree($num_of_rows - 1, $num_of_children, $id * 10 + $i + 1); + $node->addChild($child); + } } - return $node; + return $node; } $node = generate_tree(3, 3); -echo sprintf('TreeTraversal in JSON format: %s%s%s', PHP_EOL, json_encode($node), PHP_EOL); echo 'DFS Recursive:' . PHP_EOL; -TreeTraversal::dfs_recursive($node); +TreeTraversal::DFSRecursive($node); echo 'DFS Recursive Postorder:' . PHP_EOL; -TreeTraversal::dfs_recursive_postorder($node); +TreeTraversal::DFSRecursivePostorder($node); echo 'DFS Stack:' . PHP_EOL; -TreeTraversal::dfs_stack($node); +TreeTraversal::DFSStack($node); echo 'DFS Queue:' . PHP_EOL; -TreeTraversal::dfs_queue($node); +TreeTraversal::DFSQueue($node); // If you want try binary order non-binary tree // Comment generation of new tree bellow // If you do that, exception will be thrown $node = generate_tree(3, 2); echo 'DFS Recursive Inorder Binary:' . PHP_EOL; -TreeTraversal::dfs_recursive_inorder_binary($node); \ No newline at end of file +TreeTraversal::DFSRecursiveInorderBinary($node); diff --git a/contents/tree_traversal/tree_traversal.md b/contents/tree_traversal/tree_traversal.md index 2cc090779..f497d1221 100644 --- a/contents/tree_traversal/tree_traversal.md +++ b/contents/tree_traversal/tree_traversal.md @@ -29,7 +29,7 @@ As a note, a `node` struct is not necessary in javascript, so this is an example {% sample lang="swift"%} [import:1-9, lang:"swift"](code/swift/tree.swift) {% sample lang="php"%} -[import:3-27, lang:"php"](code/php/tree_traversal.php) +[import:4-37, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import:1-5, lang:"crystal"](code/crystal/tree-traversal.cr) {% sample lang="go" %} @@ -65,7 +65,7 @@ Because of this, the most straightforward way to traverse the tree might be recu {% sample lang="swift"%} [import:24-30, lang:"swift"](code/swift/tree.swift) {% sample lang="php"%} -[import:31-35, lang:"php"](code/php/tree_traversal.php) +[import:41-49, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import:7-10, lang:"crystal"](code/crystal/tree-traversal.cr) {% sample lang="go" %} @@ -109,7 +109,7 @@ Now, in this case the first element searched through is still the root of the tr {% sample lang="swift"%} [import:32-38, lang:"swift"](code/swift/tree.swift) {% sample lang="php"%} -[import:37-41, lang:"php"](code/php/tree_traversal.php) +[import:51-57, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import:12-15, lang:"crystal"](code/crystal/tree-traversal.cr) {% sample lang="go" %} @@ -148,7 +148,7 @@ In this case, the first node visited is at the bottom of the tree and moves up t {% sample lang="swift"%} [import:40-53, lang:"swift"](code/swift/tree.swift) {% sample lang="php"%} -[import:43-62, lang:"php"](code/php/tree_traversal.php) +[import:59-78, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import:17-31, lang:"crystal"](code/crystal/tree-traversal.cr) {% sample lang="go" %} @@ -197,7 +197,7 @@ In code, it looks like this: {% sample lang="swift"%} [import:55-67, lang:"swift"](code/swift/tree.swift) {% sample lang="php"%} -[import:64-73, lang:"php"](code/php/tree_traversal.php) +[import:80-91, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import:33-41, lang:"crystal"](code/crystal/tree-traversal.cr) {% sample lang="go" %} @@ -238,7 +238,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can {% sample lang="swift"%} [import:69-81, lang:"swift"](code/swift/tree.swift) {% sample lang="php"%} -[import:65-74, lang:"php"](code/php/tree_traversal.php) +[import:93-104, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import:43-51, lang:"crystal"](code/crystal/tree-traversal.cr) {% sample lang="go" %} From 560dbf41af0512ac49b639d69968a455854ad20d Mon Sep 17 00:00:00 2001 From: Michal Hanajik Date: Mon, 15 Oct 2018 11:07:46 +0100 Subject: [PATCH 4/6] Added strict types --- contents/bogo_sort/bogo_sort.md | 2 +- contents/bogo_sort/code/php/bogo_sort.php | 1 + contents/bubble_sort/bubble_sort.md | 2 +- contents/bubble_sort/code/php/bubble_sort.php | 1 + 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/contents/bogo_sort/bogo_sort.md b/contents/bogo_sort/bogo_sort.md index 08cbf2e2d..1031e6705 100644 --- a/contents/bogo_sort/bogo_sort.md +++ b/contents/bogo_sort/bogo_sort.md @@ -40,7 +40,7 @@ In code, it looks something like this: {% sample lang="swift" %} [import:13-19, lang:"swift"](code/swift/bogosort.swift) {% sample lang="php" %} -[import:14-21, lang:"php"](code/php/bogo_sort.php) +[import:15-22, lang:"php"](code/php/bogo_sort.php) {% sample lang="nim" %} [import:16-18, lang:"nim"](code/nim/bogo_sort.nim) {% sample lang="ruby" %} diff --git a/contents/bogo_sort/code/php/bogo_sort.php b/contents/bogo_sort/code/php/bogo_sort.php index ca2edf23f..41ceb5b99 100644 --- a/contents/bogo_sort/code/php/bogo_sort.php +++ b/contents/bogo_sort/code/php/bogo_sort.php @@ -1,4 +1,5 @@ Date: Fri, 19 Oct 2018 13:01:11 +0100 Subject: [PATCH 5/6] Update tree_traversal.php Fixed naming --- contents/tree_traversal/code/php/tree_traversal.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contents/tree_traversal/code/php/tree_traversal.php b/contents/tree_traversal/code/php/tree_traversal.php index 211813381..0000a7d65 100644 --- a/contents/tree_traversal/code/php/tree_traversal.php +++ b/contents/tree_traversal/code/php/tree_traversal.php @@ -104,16 +104,16 @@ public static function DFSQueue(Tree $tree): void } } -function generate_tree(int $num_of_rows, int $num_of_children, int $id = -1): Tree +function generate_tree(int $numOfRows, int $numOfChildren, int $id = -1): Tree { if ($id === -1) { $id = 1; } $node = new Tree($id); - if ($num_of_rows > 1) { - for ($i = 0; $i < $num_of_children; $i++) { - $child = generate_tree($num_of_rows - 1, $num_of_children, $id * 10 + $i + 1); + if ($numOfRows > 1) { + for ($i = 0; $i < $numOfChildren; $i++) { + $child = generate_tree($numOfRows - 1, $numOfChildren, $id * 10 + $i + 1); $node->addChild($child); } } From 215eaa4896f571952d041bd0f06b3c8f8036fb35 Mon Sep 17 00:00:00 2001 From: Michal Hanajik Date: Fri, 19 Oct 2018 13:24:35 +0100 Subject: [PATCH 6/6] Update tree_traversal.php changed comment --- contents/tree_traversal/code/php/tree_traversal.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contents/tree_traversal/code/php/tree_traversal.php b/contents/tree_traversal/code/php/tree_traversal.php index 0000a7d65..fde55046a 100644 --- a/contents/tree_traversal/code/php/tree_traversal.php +++ b/contents/tree_traversal/code/php/tree_traversal.php @@ -135,9 +135,9 @@ function generate_tree(int $numOfRows, int $numOfChildren, int $id = -1): Tree echo 'DFS Queue:' . PHP_EOL; TreeTraversal::DFSQueue($node); -// If you want try binary order non-binary tree -// Comment generation of new tree bellow -// If you do that, exception will be thrown +// If you want to try to run binary order on a non-binary tree, +// comment out the generation of the new tree below. +// If you do that, an exception will be thrown $node = generate_tree(3, 2); echo 'DFS Recursive Inorder Binary:' . PHP_EOL; TreeTraversal::DFSRecursiveInorderBinary($node);