Skip to content

Commit 8002426

Browse files
committed
Fixing naming convention
Hacker rank repository added.
1 parent f132a5e commit 8002426

File tree

15 files changed

+559
-73
lines changed

15 files changed

+559
-73
lines changed

Algorithms/Graph/Dijkstra.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
*/
77

8-
function Dijkstra(array $graph, string $source, string $target): array {
8+
function dijkstra(array $graph, string $source, string $target): array {
99
$dist = [];
1010
$pred = [];
1111
$Queue = new SplPriorityQueue();
@@ -60,7 +60,7 @@ function Dijkstra(array $graph, string $source, string $target): array {
6060
$source = "A";
6161
$target = "F";
6262

63-
$result = Dijkstra($graph, $source, $target);
63+
$result = dijkstra($graph, $source, $target);
6464
extract($result);
6565

6666
echo "Distance from $source to $target is $distance \n";

Algorithms/Graph/Kruskal.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*/
99

10-
function Kruskal(array $graph): array {
10+
function kruskal(array $graph): array {
1111
$len = count($graph);
1212
$tree = [];
1313

@@ -67,7 +67,7 @@ function unionSet(array &$set, int $i, int $j) {
6767
[0, 0, 4, 2, 6, 0]
6868
];
6969

70-
$mst = Kruskal($graph);
70+
$mst = kruskal($graph);
7171

7272
$minimumCost = 0;
7373

Algorithms/Numbers-Maths/Sieve.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* Author: Mizanur rahman <[email protected]>
5+
*
6+
*/
7+
8+
function sieveOfEratosthenes(int $n)
9+
{
10+
11+
$prime = array_fill(0, $n + 1, true);
12+
13+
for ($p = 2; $p * $p <= $n; $p++) {
14+
// If prime[p] is not changed, then it is a prime
15+
if (isset($prime[$p]) && $prime[$p] == true) {
16+
// Update all multiples of p
17+
for ($i = $p * 2; $i <= $n; $i += $p)
18+
$prime[$i] = false;
19+
}
20+
}
21+
22+
// Print all prime numbers
23+
for ($p = 2; $p <= $n; $p++)
24+
if ($prime[$p])
25+
echo $p . " ";
26+
}
27+
28+
29+
sieveOfEratosthenes(5000000);

Algorithms/Recursion-DP-Others/BacktrackingSudoku.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
define("N", 9);
1010
define("UNASSIGNED", 0);
1111

12-
function SolveSudoku(array &$grid): bool {
12+
function solveSudoku(array &$grid): bool {
1313
$row = $col = 0;
1414

15-
if (!FindUnassignedLocation($grid, $row, $col))
15+
if (!findUnassignedLocation($grid, $row, $col))
1616
return true; // success! no empty space
1717

1818
for ($num = 1; $num <= N; $num++) {
1919
if (isSafe($grid, $row, $col, $num)) {
2020
$grid[$row][$col] = $num; // make tentative assignment
2121

22-
if (SolveSudoku($grid))
22+
if (solveSudoku($grid))
2323
return true; // return, if success// return, if success
2424

2525
$grid[$row][$col] = UNASSIGNED; // failure, unmake & try again
@@ -28,23 +28,23 @@ function SolveSudoku(array &$grid): bool {
2828
return false; // triggers backtracking
2929
}
3030

31-
function FindUnassignedLocation(array &$grid, int &$row, int &$col): bool {
31+
function findUnassignedLocation(array &$grid, int &$row, int &$col): bool {
3232
for ($row = 0; $row < N; $row++)
3333
for ($col = 0; $col < N; $col++)
3434
if ($grid[$row][$col] == UNASSIGNED)
3535
return true;
3636
return false;
3737
}
3838

39-
function UsedInRow(array &$grid, int $row, int $num): bool {
39+
function usedInRow(array &$grid, int $row, int $num): bool {
4040
return in_array($num, $grid[$row]);
4141
}
4242

43-
function UsedInColumn(array &$grid, int $col, int $num): bool {
43+
function usedInColumn(array &$grid, int $col, int $num): bool {
4444
return in_array($num, array_column($grid, $col));
4545
}
4646

47-
function UsedInBox(array &$grid, int $boxStartRow, int $boxStartCol, int $num):bool {
47+
function usedInBox(array &$grid, int $boxStartRow, int $boxStartCol, int $num):bool {
4848
for ($row = 0; $row < 3; $row++)
4949
for ($col = 0; $col < 3; $col++)
5050
if ($grid[$row + $boxStartRow][$col + $boxStartCol] == $num)
@@ -54,9 +54,9 @@ function UsedInBox(array &$grid, int $boxStartRow, int $boxStartCol, int $num):b
5454

5555
function isSafe(array $grid, int $row, int $col, int $num): bool {
5656

57-
return !UsedInRow($grid, $row, $num) &&
58-
!UsedInColumn($grid, $col, $num) &&
59-
!UsedInBox($grid, $row - $row % 3, $col - $col % 3, $num);
57+
return !usedInRow($grid, $row, $num) &&
58+
!usedInColumn($grid, $col, $num) &&
59+
!usedInBox($grid, $row - $row % 3, $col - $col % 3, $num);
6060
}
6161

6262
/* A utility function to print grid */
@@ -80,7 +80,7 @@ function printGrid(array $grid) {
8080
[0, 0, 2, 0, 9, 0, 5, 0, 0]
8181
];
8282

83-
if (SolveSudoku($grid) == true)
83+
if (solveSudoku($grid) == true)
8484
printGrid($grid);
8585
else
8686
echo "No solution exists";

Algorithms/Recursion-DP-Others/DNASequencing.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
define("GP", -1);
1212
define("MS", -1);
1313

14-
function NWSquencing(string $s1, string $s2) {
14+
function nwSquencing(string $s1, string $s2) {
1515
$grid = [];
1616
$M = strlen($s1);
1717
$N = strlen($s2);
@@ -89,4 +89,4 @@ function printSequence($grid, $s1, $s2, $j, $i) {
8989

9090
$X = "GAATTCAGTTA";
9191
$Y = "GGATCGA";
92-
NWSquencing($X, $Y);
92+
nwSquencing($X, $Y);

Algorithms/Recursion-DP-Others/KMPMatching.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
*
66
*/
77

8-
function KMPStringMatching(string $str, string $pattern): array {
8+
function kmpStringMatching(string $str, string $pattern): array {
99
$matches = [];
1010
$M = strlen($pattern);
1111
$N = strlen($str);
1212
$i = $j = 0;
1313
$lps = [];
1414

15-
ComputeLPS($pattern, $lps);
15+
computeLPS($pattern, $lps);
1616

1717
while ($i < $N) {
1818
if ($pattern[$j] == $str[$i]) {
@@ -33,7 +33,7 @@ function KMPStringMatching(string $str, string $pattern): array {
3333
return $matches;
3434
}
3535

36-
function ComputeLPS(string $pattern, array &$lps) {
36+
function computeLPS(string $pattern, array &$lps) {
3737
$len = 0;
3838
$i = 1;
3939
$M = strlen($pattern);
@@ -58,7 +58,7 @@ function ComputeLPS(string $pattern, array &$lps) {
5858

5959
$txt = "AABAACAADAABABBBAABAA";
6060
$pattern = "AABA";
61-
$matches = KMPStringMatching($txt, $pattern);
61+
$matches = kmpStringMatching($txt, $pattern);
6262

6363
if ($matches) {
6464
foreach ($matches as $pos) {

DS/Heap/Classes/MinHeap.php

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,77 +7,86 @@
77

88
namespace DS\Heap\Classes;
99

10-
class MinHeap {
10+
class MinHeap
11+
{
1112

1213
public $heap;
1314
public $count;
1415

15-
public function __construct(int $size) {
16-
$this->heap = array_fill(0, $size + 1, 0);
17-
$this->count = 0;
16+
public function __construct(int $size)
17+
{
18+
$this->heap = array_fill(0, $size + 1, 0);
19+
$this->count = 0;
1820
}
1921

20-
public function create(array $arr = []) {
21-
if ($arr) {
22-
foreach ($arr as $val) {
23-
$this->insert($val);
24-
}
25-
}
22+
public function create(array $arr = [])
23+
{
24+
if ($arr) {
25+
foreach ($arr as $val) {
26+
$this->insert($val);
27+
}
28+
}
2629
}
2730

28-
public function display() {
29-
echo implode("\t", array_slice($this->heap, 1)) . "\n";
31+
public function display()
32+
{
33+
echo implode("\t", array_slice($this->heap, 1)) . "\n";
3034
}
3135

32-
public function insert(int $i) {
33-
if ($this->count == 0) {
34-
$this->heap[1] = $i;
35-
$this->count = 2;
36-
} else {
37-
$this->heap[$this->count++] = $i;
38-
$this->siftUp();
39-
}
36+
public function insert(int $i)
37+
{
38+
if ($this->count == 0) {
39+
$this->heap[1] = $i;
40+
$this->count = 2;
41+
} else {
42+
$this->heap[$this->count++] = $i;
43+
$this->siftUp();
44+
}
4045
}
4146

42-
public function siftUp() {
43-
$tmpPos = $this->count - 1;
44-
$tmp = intval($tmpPos / 2);
47+
public function siftUp()
48+
{
49+
$tmpPos = $this->count - 1;
50+
$tmp = intval($tmpPos / 2);
4551

46-
while ($tmpPos > 0 && $this->heap[$tmp] > $this->heap[$tmpPos]) {
47-
$this->swap($tmpPos, $tmp);
48-
$tmpPos = intval($tmpPos / 2);
49-
$tmp = intval($tmpPos / 2);
50-
}
52+
while ($tmpPos > 0 && $this->heap[$tmp] > $this->heap[$tmpPos]) {
53+
$this->swap($tmpPos, $tmp);
54+
$tmpPos = intval($tmpPos / 2);
55+
$tmp = intval($tmpPos / 2);
56+
}
5157
}
5258

53-
public function swap(int $a, int $b) {
54-
$tmp = $this->heap[$a];
55-
$this->heap[$a] = $this->heap[$b];
56-
$this->heap[$b] = $tmp;
59+
public function swap(int $a, int $b)
60+
{
61+
$tmp = $this->heap[$a];
62+
$this->heap[$a] = $this->heap[$b];
63+
$this->heap[$b] = $tmp;
5764
}
5865

59-
public function extractMin() {
60-
$min = $this->heap[1];
61-
$this->heap[1] = $this->heap[$this->count - 1];
62-
$this->heap[--$this->count] = 0;
63-
$this->siftDown(1);
64-
return $min;
66+
public function extractMin()
67+
{
68+
$min = $this->heap[1];
69+
$this->heap[1] = $this->heap[$this->count - 1];
70+
$this->heap[--$this->count] = 0;
71+
$this->siftDown(1);
72+
return $min;
6573
}
6674

67-
public function siftDown(int $k) {
68-
$smallest = $k;
69-
$left = 2 * $k;
70-
$right = 2 * $k + 1;
71-
if ($left < $this->count && $this->heap[$smallest] > $this->heap[$left]) {
72-
$smallest = $left;
73-
}
74-
if ($right < $this->count && $this->heap[$smallest] > $this->heap[$right]) {
75-
$smallest = $right;
76-
}
77-
if ($smallest != $k) {
78-
$this->swap($k, $smallest);
79-
$this->siftDown($smallest);
80-
}
75+
public function siftDown(int $k)
76+
{
77+
$smallest = $k;
78+
$left = 2 * $k;
79+
$right = 2 * $k + 1;
80+
if ($left < $this->count && $this->heap[$smallest] > $this->heap[$left]) {
81+
$smallest = $left;
82+
}
83+
if ($right < $this->count && $this->heap[$smallest] > $this->heap[$right]) {
84+
$smallest = $right;
85+
}
86+
if ($smallest != $k) {
87+
$this->swap($k, $smallest);
88+
$this->siftDown($smallest);
89+
}
8190
}
8291

8392
}

HackerRank/Euler/12.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Author: Mizanur Rahman <[email protected]>
4+
*
5+
* Project Euler #12: Highly divisible triangular number
6+
*/
7+
8+
$triangleNumbers = [];
9+
10+
function numberOfDivisors($triangle) {
11+
12+
global $triangleNumbers;
13+
14+
if (isset($triangleNumbers[$triangle])) {
15+
return $triangleNumbers[$triangle];
16+
} else {
17+
$tempTriangle = $triangle;
18+
$primePowerCount = 1;
19+
$count = 0;
20+
while ($triangle % 2 == 0) {
21+
$triangle =floor($triangle/ 2);
22+
$count += 1;
23+
}
24+
$primePowerCount *= ($count + 1);
25+
for ($i = 3; $i <= intval(sqrt($triangle)); $i++) {
26+
$count = 0;
27+
while ($triangle % $i == 0) {
28+
$count += 1;
29+
$triangle = floor($triangle/$i);
30+
}
31+
$primePowerCount *= ($count + 1);
32+
}
33+
if ($triangle > 2) {
34+
$primePowerCount *= 2;
35+
}
36+
$triangleNumbers[$tempTriangle] = $primePowerCount;
37+
return $triangleNumbers[$tempTriangle];
38+
}
39+
}
40+
41+
function getTriangle($number) {
42+
return floor($number * ($number + 1)/ 2);
43+
}
44+
45+
46+
$handle = fopen ("php://stdin","r");
47+
$t = fgets($handle);
48+
for($i=0; $i<$t; $i++) {
49+
50+
$n = trim(fgets($handle));
51+
$number = 1;
52+
$triangle = getTriangle($number);
53+
$divisorCount = numberOfDivisors($triangle);
54+
$triangleNumbers[$number] = $divisorCount;
55+
while ($divisorCount <= $n) {
56+
$number += 1;
57+
$triangle = getTriangle($number);
58+
$divisorCount = numberOfDivisors($triangle);
59+
$triangleNumbers[$triangle] = $divisorCount;
60+
61+
}
62+
echo $triangle."\n";
63+
}

0 commit comments

Comments
 (0)