Skip to content

Instantly share code, notes, and snippets.

@mb0017
mb0017 / Python-Quicksort.py
Created January 15, 2014 21:46
Quicksort using list comprehensions
#source: http://en.literateprograms.org/Quicksort_(Python)
def qsort1(list):
"""Quicksort using list comprehensions"""
if list == []:
return []
else:
pivot = list[0]
lesser = qsort1([x for x in list[1:] if x < pivot])
greater = qsort1([x for x in list[1:] if x >= pivot])
return lesser + [pivot] + greater
@mb0017
mb0017 / Python-Bubblesort
Last active August 29, 2015 14:02
Python-Bubblesort
"""
A bubble sort is often considered the most inefficient sorting method since
it must exchange items before the final location is known. These “wasted”
exchange operations are very costly. However, because the bubble sort makes
passes through the entire unsorted portion of the list, it has the capability
to do something most sorting algorithms cannot. In particular, if during a
pass there are no exchanges, then we know that the list must be sorted. A
bubble sort can be modified to stop early if it finds that the list has
become sorted. This means that for lists that require just a few passes, a
bubble sort may have an advantage in that it will recognize the sorted list
@mb0017
mb0017 / my vi cheatsheet
Created November 10, 2014 20:55
vi cheatsheet
Cursor movement:
h - move left
j - move down
k - move up
l - move right
w - jump by start of words (punctuation considered words)
W - jump by words (spaces separate words)
e - jump to end of words (punctuation considered words)
E - jump to end of words (no punctuation)
b - jump backward by words (punctuation considered words)
@mb0017
mb0017 / my linux cheatsheet
Last active December 23, 2019 17:25
Linux cheatsheet
#--- install/remove packages
apt-get update # Fetches the list of available updates
apt-get upgrade # Strictly upgrades the current packages
apt-get dist-upgrade # Installs updates (new ones)
apt-get autoclean # This command removes .deb files for packages
# that are no longer installed on your system.
apt-get clean # Remove the installed deb packages
apt-get remove <package> # This command removes an installed package, leaving configuration files intact.
apt-get purge <package> # This command completely removes a package and the associated configuration files.
apt-get autoremove #This command removes packages that were installed by other packages and are no longer needed.
<p>
My programming language of preference is python for the simple reason that I feel I write better code faster with it then I do with other languages. However also has a lot of nice tricks and idioms to do things well. And partly as a reminder to myself to use them, and partly because I thought this might be of general interest I have put together this collection of some of my favourite idioms. I am also putting this on <a href="https://gist.github.com/codefisher/9d7993ddbf404c505128">gist.github.com</a> so that anyone that wants to contribute there own things can, and I will try and keep this post up to date.
</p>
<h2>enumerate</h2>
<p>
A fairly common thing to do is loop over a list while also keeping track of what index we are up to. Now we could use a <code>count</code> variable, but python gives us a nicer syntax for this with the <code>enumerate()</code> function.
<script src="https://gist.github.com/mb0017/f7575869bb2341367032.js?file=enumerate.py"></script>