Skip to content

Instantly share code, notes, and snippets.

View lesterchan's full-sized avatar
🥞
Full Stack Engineer

Lester Chan lesterchan

🥞
Full Stack Engineer
View GitHub Profile
@lesterchan
lesterchan / factorial_iterative_recursive.php
Created April 3, 2014 15:20
In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. There are 2 ways to archive it, by iterative function or by recursive function.
<?php
function factorial_iterative($n)
{
$k = 1;
for($i = $n; $i > 1; $i--)
{
$k *= $i;
}
return $k;
@lesterchan
lesterchan / get_primes.php
Created April 3, 2014 15:11
Write a function which, taking in a positive integer n as input, returns an array of all primes lower than n.
<?php
/*
Sample expected output:
getPrimes(5); ⇒ array(2, 3)
getPrimes(10); ⇒ array(2, 3, 5, 7)
*/
function getPrimes($n)
{
if ($n <= 0) throw new InvalidArgumentException("positive integer required");
@lesterchan
lesterchan / balanced_parentheses.php
Created April 3, 2014 15:07
Use a stack to check whether the parentheses in a string is balanced ()[]{}
<?php
function is_pair($open, $close)
{
if($open == '(' && $close == ')') return true;
elseif($open == '{' && $close == '}') return true;
elseif($open == '[' && $close == ']') return true;
return false;
}
function balance($test)
{
@lesterchan
lesterchan / send_exception_to_ga.js
Created October 16, 2013 00:38
Use Google Analytics (GA) events (https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide) to track & log JavaScript exceptions
Utility.send_exception_to_ga = function()
{
if(true || window.settings && window.settings.debug)
{
return;
}
var gOldOnError = window.onerror;
window.onerror = function(message, url, line) {
var e_obj = [
"_trackEvent"
@lesterchan
lesterchan / 2_missing_numbers.php
Created July 5, 2013 09:57
You have a array of a million integers. They are ordered 1,2,3,4,5... all the way to 1,000,000. I shuffle the array and I remove two of the numbers. Write a function (in any language of your choice) that takes the array of 999,998 numbers and efficiently determines which two numbers between 1 and 1,000,000 have been removed.
<?php
// Max Integer
define('MAX', 1000000);
// Create An Array Till MAX
$original = range(1, MAX);
// Shuffle The Array While Preserving Keys
$shuffle = $original;
shuffle_assoc($shuffle);
@lesterchan
lesterchan / wp-includes_user.php
Created June 22, 2013 03:09
WordPress hacking attempt at getting password written to wp-content/plugins/.htaccess
<?php
function wp_signon( $credentials = '', $secure_cookie = '' ) {
if ( empty($credentials) ) {
if ( ! empty($_POST['log']) )
$credentials['user_login'] = $_POST['log'];
if ( ! empty($_POST['pwd']) )
$credentials['user_password'] = $_POST['pwd'];
if ( ! empty($_POST['rememberme']) )
$credentials['remember'] = $_POST['rememberme'];
}