Skip to content

PHP unify code style #515

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

Merged
merged 6 commits into from
Oct 27, 2018
Merged
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
2 changes: 1 addition & 1 deletion contents/bogo_sort/bogo_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: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" %}
Expand Down
22 changes: 15 additions & 7 deletions contents/bogo_sort/code/php/bogo_sort.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
<?php
declare(strict_types=1);

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;
2 changes: 1 addition & 1 deletion contents/bubble_sort/bubble_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:4-17, lang:"php"](code/php/bubble_sort.php)
{% sample lang="lisp" %}
[import:3-28, lang:"lisp"](code/lisp/bubble_sort.lisp)
{% sample lang="nim" %}
Expand Down
26 changes: 15 additions & 11 deletions contents/bubble_sort/code/php/bubble_sort.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
<?php
declare(strict_types=1);

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;
184 changes: 103 additions & 81 deletions contents/tree_traversal/code/php/tree_traversal.php
Original file line number Diff line number Diff line change
@@ -1,121 +1,143 @@
<?php
declare(strict_types=1);

class Tree implements JsonSerializable
{
private $id;
private $children = [];
private $id;
private $children = [];

public function __construct(int $id, array $children = [])
{
$this->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
function generate_tree(int $numOfRows, int $numOfChildren, 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 ($numOfRows > 1) {
for ($i = 0; $i < $numOfChildren; $i++) {
$child = generate_tree($numOfRows - 1, $numOfChildren, $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
// 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::dfs_recursive_inorder_binary($node);
TreeTraversal::DFSRecursiveInorderBinary($node);
12 changes: 6 additions & 6 deletions contents/tree_traversal/tree_traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down