Skip to content

Instantly share code, notes, and snippets.

View robert-wallis's full-sized avatar

Robert Wallis robert-wallis

View GitHub Profile
@robert-wallis
robert-wallis / gist:1235148
Created September 22, 2011 15:57
Permutations in Python (from Python website)
// directly from http://docs.python.org/library/itertools.html
def permutations(iterable, r=None):
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
# permutations(range(3)) --> 012 021 102 120 201 210
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
if r > n:
return
indices = range(n)
@robert-wallis
robert-wallis / permutations.go
Created September 22, 2011 15:54
Permutations in Go
// By: Robert Wallis - SmilingRob@gmail.com
// Returns a channel of all the possible permutations of a string containing
// r length in characters.
// adopted from http://docs.python.org/library/itertools.html
func StringPermutationsSub(s string, r int) chan string {
out := make(chan string)
go func() {
defer close(out)
pool := []int(s) // get unicode
n := len(pool)
@robert-wallis
robert-wallis / debug.js
Created September 17, 2011 06:44
Display the contents of a JavaScript object in the #debug id on the page. Or alert it if there was no debug.
// debug() - Copyright (C) 2006-2008 Robert Wallis
function debug(obj, name)
{
var t = "";
if (name) {
t += ""+name+":";
}
for (var i in obj) {
t += i+"=\""+obj[i]+"\"\n";
@robert-wallis
robert-wallis / validateCard.js
Created September 17, 2011 06:21
Make sure they typed in the card correctly, before you send it to the server and the payment gateway. Using a cool algorithm built into every credit card.
// usage: valid = validateCard('4111111111111111');
function validateCard(cardno) {
if (cardno.length <= 0 || cardno.length < 13 || cardno.length > 20 ) {
return false;
}
var sum = 0 * 0;
var digits = cardno.split('');
digits.reverse();
for (var i = 0; i < digits.length; i++) {
var d = 1 * digits[i];
@robert-wallis
robert-wallis / lorem-ipsum.txt
Created September 17, 2011 06:07
classic typography placeholder to make demos look like real data
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer tempus, felis eget suscipit posuere, tellus nibh interdum pede, vitae malesuada nibh tellus ac augue. Proin ullamcorper, nunc vel viverra egestas, risus lorem tincidunt metus, nec porttitor massa eros sed libero. Nunc felis. Aliquam vel massa. Donec ante justo, aliquam sollicitudin, condimentum consequat, volutpat at, nunc. In eu magna. Vestibulum libero. Donec sem arcu, tempus quis, tempus in, sollicitudin at, diam. Quisque odio felis, nonummy quis, euismod ac, gravida ut, lectus. Maecenas bibendum metus eu risus.
Proin porta mi vulputate est. Etiam tortor quam, molestie sit amet, pharetra non, varius eu, mi. Duis tincidunt risus vel orci. Sed pede elit, sollicitudin eu, dapibus non, tincidunt sit amet, diam. Donec turpis. Integer volutpat. Suspendisse potenti. Integer eleifend velit ut diam. Nullam varius wisi vitae massa. Vivamus massa tortor, varius eget, vestibulum at, vestibulum eu, leo. Nunc rutrum. Donec eros urna, varius id, viverra n
@robert-wallis
robert-wallis / debug.php
Created September 17, 2011 06:06
nicely styled, easy to read, debug($someval, __FILE__.__LINE__); because nobody I know uses zend debugger :(
<?php
// debug() - Copyright (C) 2006-2009 Robert Wallis
// print out the value to be read in the browser
if (!function_exists('debug'))
{
function debug($value, $name='')
{
print('<pre>');
if ($name)
@robert-wallis
robert-wallis / error_reporting.php
Created September 17, 2011 06:04
turn on error reporting, because it's often off for security / bad programming reasons
<?php
error_reporting(E_ALL);
function error_handler($number, $string, $file, $line)
{
if (intval(E_WARNING) != intval($number))
{
print("PHP Error: ".$number." ".$file."(".$line.")"."<br />".$string."<br />");
}
}
@robert-wallis
robert-wallis / info.php
Created September 17, 2011 06:02
Shows phpinfo, because I always forget the function name and paramater value.
<?php
phpinfo(INFO_ALL);
@robert-wallis
robert-wallis / json_database.php
Created September 17, 2011 06:00
Convert a standard database array into JSON
<?php
// Copyright (C) 2007 Robert Wallis - July 11, 2007
function json_database($recordset)
{
if (0==count($recordset))
return '[]';
$o = "[\n";
foreach($recordset as $records)
{
$o .= '{';