Skip to content

Commit a13cef2

Browse files
author
yinchuanwen
committed
init project
0 parents  commit a13cef2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+4169
-0
lines changed

Diff for: Algorithms/EucledianGCD.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
function consoleLog()
3+
{
4+
$args = func_get_args();
5+
if(!$args)
6+
{
7+
return;
8+
}
9+
$output = $args[0];
10+
if(count($args)>1)
11+
{
12+
$output = call_user_func_array('sprintf', $args);
13+
}
14+
else
15+
{
16+
if(!is_scalar($output))
17+
{
18+
$output = var_export($output, true);
19+
}
20+
}
21+
22+
echo "$output \n";
23+
}
24+
25+
function euclideanGCDRecursive($first, $second)
26+
{
27+
if ($second === 0) {
28+
return $first;
29+
} else {
30+
return euclideanGCDRecursive($second, $first % $second);
31+
}
32+
}
33+
34+
function euclideanGCDIterative($first, $second)
35+
{
36+
while ($second !== 0) {
37+
$temp = $second;
38+
$second = $first % $second;
39+
$first = $temp;
40+
}
41+
return $first;
42+
}
43+
44+
function main()
45+
{
46+
$first = 20;
47+
$second = 30;
48+
consoleLog("Recursive GCD for %d and %d is %d", $first, $second, euclideanGCDRecursive($first, $second));
49+
consoleLog("Iterative GCD for %d and %d is %d", $first, $second, euclideanGCDIterative($first, $second));
50+
}
51+
52+
main();
53+

Diff for: Algorithms/KadaneAlgo.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
function consoleLog()
3+
{
4+
$args = func_get_args();
5+
if(!$args)
6+
{
7+
return;
8+
}
9+
$output = $args[0];
10+
if(count($args)>1)
11+
{
12+
$output = call_user_func_array('sprintf', $args);
13+
}
14+
else
15+
{
16+
if(!is_scalar($output))
17+
{
18+
$output = var_export($output, true);
19+
}
20+
}
21+
22+
echo "$output \n";
23+
}
24+
25+
function KadaneAlgo($array)
26+
{
27+
$cummulativeSum = 0;
28+
$maxSum = 0;
29+
for ($i = 0;
30+
$i < count($array); $i++) {
31+
$cummulativeSum = $cummulativeSum + $array[$i];
32+
if ($cummulativeSum < 0) {
33+
$cummulativeSum = 0;
34+
}
35+
if ($maxSum < $cummulativeSum) {
36+
$maxSum = $cummulativeSum;
37+
}
38+
}
39+
return $maxSum;
40+
}
41+
42+
function main()
43+
{
44+
$myArray = array(1, 2, 3, 4, -6);
45+
$result = KadaneAlgo($myArray);
46+
consoleLog($result);
47+
}
48+
49+
main();
50+

Diff for: Algorithms/dynamic_programming/fibonacci.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
function consoleLog()
3+
{
4+
$args = func_get_args();
5+
if(!$args)
6+
{
7+
return;
8+
}
9+
$output = $args[0];
10+
if(count($args)>1)
11+
{
12+
$output = call_user_func_array('sprintf', $args);
13+
}
14+
else
15+
{
16+
if(!is_scalar($output))
17+
{
18+
$output = var_export($output, true);
19+
}
20+
}
21+
22+
echo "$output \n";
23+
}
24+
25+
function fib($n)
26+
{
27+
$table = array();
28+
array_push($table, 1);
29+
array_push($table, 1);
30+
for ($i = 2;$i < $n; ++$i)
31+
{
32+
array_push($table, $table[$i - 1] + $table[$i - 2]);
33+
}
34+
35+
consoleLog("Fibonacci #%d = %s", $n, $table[$n - 1]);
36+
}
37+
38+
fib(1);
39+
fib(2);
40+
fib(200);
41+
fib(5);
42+
fib(10);
43+

Diff for: Algorithms/sieveOfEratosthenes.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
function consoleLog()
3+
{
4+
$args = func_get_args();
5+
if(!$args)
6+
{
7+
return;
8+
}
9+
$output = $args[0];
10+
if(count($args)>1)
11+
{
12+
$output = call_user_func_array('sprintf', $args);
13+
}
14+
else
15+
{
16+
if(!is_scalar($output))
17+
{
18+
$output = var_export($output, true);
19+
}
20+
}
21+
22+
echo "$output \n";
23+
}
24+
25+
function sieveOfEratosthenes($n)
26+
{
27+
$primes = [];
28+
for($i=0;$i<$n+1;$i++)
29+
{
30+
$primes[$i] = true;
31+
}
32+
$primes[0] = $primes[1] = false;
33+
$sqrtn = ceil(sqrt($n));
34+
for ($i = 2;
35+
$i <= $sqrtn; $i++) {
36+
if ($primes[$i]) {
37+
for ($j = 2 * $i;
38+
$j <= $n; $j += $i) {
39+
$primes[$j] = false;
40+
}
41+
}
42+
}
43+
return $primes;
44+
}
45+
46+
function main()
47+
{
48+
$n = 69;
49+
$primes = sieveOfEratosthenes($n);
50+
for ($i = 2;
51+
$i <= $n; $i++) {
52+
if ($primes[$i]) {
53+
consoleLog($i);
54+
}
55+
}
56+
}
57+
58+
main();
59+

Diff for: Ciphers/caesarsCipher.php

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
function consoleLog()
3+
{
4+
$args = func_get_args();
5+
if (!$args) {
6+
return;
7+
}
8+
$output = $args[0];
9+
if (count($args) > 1) {
10+
$output = call_user_func_array('sprintf', $args);
11+
} else {
12+
if (!is_scalar($output)) {
13+
$output = var_export($output, true);
14+
}
15+
}
16+
17+
echo "$output \n";
18+
}
19+
20+
function stringLength($string)
21+
{
22+
return strlen(iconv('UTF-8', 'UTF-16LE', $string)) / 2;
23+
}
24+
25+
function charCodeAt($str, $index){
26+
//not working!
27+
28+
$char = mb_substr($str, $index, 1, 'UTF-8');
29+
if (mb_check_encoding($char, 'UTF-8'))
30+
{
31+
$ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
32+
return hexdec(bin2hex($ret));
33+
} else {
34+
return null;
35+
}
36+
}
37+
38+
function fromCharCode($codes)
39+
{
40+
if (is_scalar($codes)) $codes= func_get_args();
41+
$str= '';
42+
foreach ($codes as $code) $str.= chr($code);
43+
return $str;
44+
}
45+
46+
function rot13($str)
47+
{
48+
$response = array();
49+
$strLength = stringLength($str);
50+
for ($i = 0;
51+
$i < $strLength; $i++) {
52+
$char = charCodeAt($str, $i);
53+
if ($char < 65 || $char > 90 && $char < 97 || $char > 122) {
54+
array_push($response, $str[$i]);
55+
} else if ($char > 77 && $char <= 90 || $char > 109 && $char <= 122) {
56+
array_push($response, fromCharCode(charCodeAt($str, $i) - 13));
57+
} else {
58+
array_push($response, fromCharCode(charCodeAt($str, $i) + 13));
59+
}
60+
}
61+
return join("", $response);
62+
}
63+
64+
$encryptedString = "Uryyb Jbeyq";
65+
$decryptedString = rot13($encryptedString);
66+
consoleLog($decryptedString);
67+

0 commit comments

Comments
 (0)