Skip to content

Instantly share code, notes, and snippets.

View andylshort's full-sized avatar

Andrew Lamzed-Short andylshort

View GitHub Profile
@andylshort
andylshort / NumericTypes.cs
Created February 20, 2018 12:20
Small function to determine if a type is a whole numeric whole or not
private static bool IsWholeNumericType(Type t)
{
var numericTypes = new[] {
typeof(Byte), typeof(Int16), typeof(Int32),
typeof(Int64), typeof(SByte), typeof(Single),
typeof(UInt16), typeof(UInt32), typeof(UInt64)
};
return numericTypes.Contains(t);
}
@andylshort
andylshort / Number.padLeft.js
Created February 20, 2018 12:23
Small JavaScript function to left pad a number
if (!Number.prototype.padLeft) {
// Pad the left of a number with a character
Number.prototype.padLeft = function (len, chr) {
var self = Math.abs(this) + '';
return (this < 0 && '-' || '') +
(String(Math.pow(10, (len || 2) - self.length))
.slice(1).replace(/0/g, chr || '0') + self);
};
}
@andylshort
andylshort / String.format.js
Created February 20, 2018 12:24
JavaScript equivalent to C#'s string.Format(...) function
if (!String.prototype.format) {
// Equivalent to C# String.Format
String.prototype.format = function () {
var args = arguments;
return this.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
@andylshort
andylshort / Range.cs
Created February 22, 2018 21:42
Range struct to handle max and min and adapts
struct Range
{
private int _min;
public int Min
{
get { return _min; }
set
{
_min = value > _max ? _max : value;
}
@andylshort
andylshort / simon_stalenhag.py
Created March 10, 2018 19:57
Small scripts to scrape the beautiful artwork of Simon Stålenhag from his website
import os
import re
import sys
import urllib.error
import urllib.request
if len(sys.argv) != 2:
print("Please specify destination directory as an argument")
sys.exit(-1)
@andylshort
andylshort / Recursively Delete Temp Folder.ps1
Created March 23, 2018 22:24
Clear the contents of the Temp folder in Windows (recursive)
Get-ChildItem $env:TEMP -Recurse | Remove-Item -Force -Recurse -WhatIf
@andylshort
andylshort / Oreilly.sh
Created March 23, 2018 22:26
Downloads all publically available, free books offered by O'Reilly
#!/bin/bash
wget -O- http://www.oreilly.com/programming/free/ | tr '"' \\n | grep http | grep free | cut -d "?" -f1 | sed 's/free/free\/files/' | sed 's/\.csp/\.pdf/' | xargs wget -P ebook
@andylshort
andylshort / Ackermann.py
Created March 23, 2018 22:28
Python implementation of the Ackermann function
def Ackermann(x, y):
answer = 0
if x == 0:
answer = y + 1
elif y == 0:
answer = Ackermann(x - 1, 1)
else:
answer = Ackermann(x - 1, Ackermann(x, y - 1))
@andylshort
andylshort / String.padLeft.js
Created March 23, 2018 22:29
The infamous left pad
if (!String.prototype.padLeft) {
String.prototype.padLeft = function(c, size) {
var s = this.valueOf();
while (s.length < size) s = c + s;
return s;
};
}
@andylshort
andylshort / chiptune.sh
Created March 23, 2018 22:30
Example chiptune bash script I found online
echo "g(i,x,t,o){return((3&x&(i*((3&i>>16?\"BY}6YB6%\":\"Qj}6jQ6%\")[t%8]+51)>>o))<<4);};main(i,n,s){for(i=0;;i++)putchar(g(i,1,n=i>>14,12)+g(i,s=i>>17,n^i>>13,10)+g(i,s/3,n+((i>>11)%3),10)+g(i,s/5,8+n-((i>>10)%3),9));}"|gcc -xc -&&./a.out|aplay