Skip to content

Instantly share code, notes, and snippets.

View davidejones's full-sized avatar
🤷‍♂️
keep our code running while other packages are changing theirs

David Jones davidejones

🤷‍♂️
keep our code running while other packages are changing theirs
View GitHub Profile
@davidejones
davidejones / compress_permuation_by_index.py
Created April 13, 2017 22:13
compressing permuations by using elimination coding
import math
from functools import reduce
def bit_count(x):
""" The minimum bits needed to represent a number """
# return math.ceil(math.log(x + 1) / math.log(2))
return math.ceil(math.log2(x))
@davidejones
davidejones / Makefile
Created January 19, 2017 16:59
mingw gcc dos compile (thanks to http://nullprogram.com/blog/2014/12/09/)
CFLAGS = -std=gnu99 -Wall -Wextra -Os -nostdlib -m32 -march=i386 \
-Wno-unused-function \
-ffreestanding -fomit-frame-pointer -fwrapv -fno-strict-aliasing \
-fno-leading-underscore \
-Wl,--nmagic,-static,-Tmingw.com.ld
all:
gcc $(CFLAGS) -o hello.o hello.c
objcopy -O binary hello.o hello.com
@davidejones
davidejones / useful.sh
Last active January 18, 2017 05:44
useful bash commands
# how-to-clean-log-file (http://unix.stackexchange.com/questions/92384/how-to-clean-log-file)
> logfile
cat /dev/null > logfile
dd if=/dev/null of=logfile
truncate logfile --size 0
# how to get a file octal permissions
stat -c "%a"
# check a makefile for tabs and line endings
@davidejones
davidejones / somewebsite.com
Created November 9, 2016 06:39
nginx leaks a private IP address through its HTTP headers
// if the aws elb is passing host as ip force redirect to domain
// this is for pci fixes
if ($host ~ "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}") {
rewrite ^/(.*) $scheme://www.somewebsite.com/$1 permanent;
}
@davidejones
davidejones / selection_sort.py
Created October 6, 2016 19:03
selection sort
def selection_sort(u, s=[]):
unsorted = list(u)
sorted = [] if not s else s
minimum = None
index = -1
# loop over every number
for i, item in enumerate(unsorted):
# if the number is less than the minimum or we have no minimum yet set it
if not minimum or item < minimum:
minimum = item
@davidejones
davidejones / binary_search.py
Created October 3, 2016 17:48
binary search
import math
def binary_search(haystack, needle, start=None, end=None):
start = 0 if not start else start
end = len(haystack) if not end else end
midpoint = math.floor((end - start)/2) + start
# do we match the midpoint or have only one element then we are done
if needle == haystack[midpoint] or len(haystack[start:end]) == 1:
return midpoint
@davidejones
davidejones / bubble_sort.py
Last active September 25, 2016 04:05
first attempt at a bubble sort
def bubble_sort(l):
# keep track of if we did any swapping
swapped = False
# loop over all elements in list
for i in xrange(0, len(l), 1):
# look at this item and the next and see if they need to swap
if i != len(l) - 1:
if l[i] > l[i + 1]:
l[i], l[i + 1] = l[i + 1], l[i]
swapped = True
@davidejones
davidejones / inplace_insertion_sort.py
Last active September 14, 2016 04:10
attempt at an insertion sort
def inplace_insertion_sort(unsorted_list, split_index=None):
# if we have no split_index yet we know this is first iteration so lets split it
split_index = 1 if not split_index else split_index
# loop over each item in unsorted list
for uitem in unsorted_list[split_index:len(unsorted_list)]:
# reverse the sorted list to loop over as we compare right to left
for index, sitem in enumerate(reversed(unsorted_list[:split_index])):
if uitem > sitem:
# remove from unsorted so we don't look at it again
unsorted_list.remove(uitem)
@davidejones
davidejones / quick_sort.py
Last active August 31, 2016 16:20
first attempt at a quick sort
def quick_sort(l, start=None, end=None):
# get the length of our working list
list_len = len(l[start:end])
# if this is the first iteration end is length of whole list
end = len(l) if end is None else end
# choose the last item of working list as pivot
pivot = l[end-1]
# set wall left of first element in working list
wall_index = start-1 if start is not None else -1
# set the current element we start with to after wall (first element in working list)
@davidejones
davidejones / fizzbuzz.py
Created August 29, 2016 17:26
trying out a fizzbuzz
def main():
for i in xrange(1, 101):
if i % 3 == 0 and i % 5 == 0:
print('%d FizzBuzz' % i)
elif i % 3 == 0:
print('%d Fizz' % i)
elif i % 5 == 0:
print('%d Buzz' % i)
else:
print(i)