This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def compose(*funcs): | |
"""Composes (or chains) two or more functions into a new function. | |
E.g. with "f = compose(len, str, int)", f('123')" is the same as | |
len(str(int('123'))).""" | |
if len(funcs) < 2: | |
raise Exception("too few arguments given") | |
elif len(funcs) == 2: | |
(a, b) = funcs | |
def call(*args, **kwargs): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bit operations to determine whether a month has 31 days. | |
# Just for fun, inspired by an example in the Coursera course | |
# 'Hardware Security', https://www.coursera.org/course/hardwaresec | |
def has31days(month): | |
return (month & 1) ^ ((month & 8) >> 3) | |
for i in range(1, 13): | |
print(i, has31days(i)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I wrote this bookmarklet because Amazon has ended Linux support for downloading MP3s, | |
// and now I have to download songs separately through Amazon Cloud Player. | |
// This bookmarklet helps a bit with this, but it's far from perfect. | |
// When a list of songs is shown in Amazon Cloud Player, it clicks the song checkbox and | |
// the download button separately for each song, while waiting a few seconds in between songs. | |
// I encountered the issue that I cannot download more than a few songs simultaneously; | |
// the rest will just not show a download window. Not sure whether my browser or the server | |
// is doing this, but you might want to set the value of songdelayms high enough. | |
// The script is not really that useful, but it was fun to play around with this. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// foobar2k plugin that mixes a multi-channel stream down to mono, | |
// and then up to multiple channels again, but with all channels but one empty. | |
// this can be used to play several different tracks simultaneously over a | |
// multi-channel audio output. | |
// | |
// pre-built version can be found here: | |
// https://dl.dropboxusercontent.com/u/267889/foo_down_up_mix_dsp.dll | |
// | |
// to build locally, just add this source file into the SDK sample plugin. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Ruby script to scrape termine.orf.at for FM4 music events and | |
# turn the result into an RSS feed. | |
# | |
# Usage: ruby fm4-musik.rb <result-file> <data-file> [-digest] | |
# | |
# The result file is where the feed xml will be written to, | |
# and the data file stores some state necessary to track | |
# the events that were already posted. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In C/C++, the idiom (sizeof(arr) / sizeof(*arr)) is often used to get the length of an array. | |
// But this will return incorrect results if arr is not actually an array, but a pointer. | |
// The following function template provides a type-safe way to get the length of an array, | |
// by "destructuring" the type and reading the array length from there. | |
#include <cstddef> // for size_t | |
template <typename T, size_t N> | |
constexpr size_t sizeof_array(T (&)[N]) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import fileinput | |
import re | |
# pattern to match lines of hex byte values | |
PATTERN = re.compile("^[a-zA-Z0-9]{1,2}( [a-zA-Z0-9]{1,2})*$") | |
# margin between bytes and text | |
MARGIN = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct nullptr_t | |
{ | |
template <typename T> | |
operator T*() { return static_cast<T*>(0); } | |
}; | |
nullptr_t nullptr; | |
#include <iostream> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# challenge #8 | |
# 'find the largest palindrome made from the product of two 3-digit numbers' | |
# see also http://projecteuler.net/index.php?section=problems&id=4 | |
# simple one-line solution, seems fast enough | |
# => [906609, 993, 913] | |
999.downto(100).map { |a| 999.downto(100).map { |b| [a*b, a, b] }.select { |arr| s = arr.first.to_s; s == s.reverse } }.flatten(1).sort { |a, b| a.first <=> b.first }.reverse.uniq.first | |
# more performant solution for n-digit numbers | |
# 4 => [99000099, 9999, 9901] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set nocompatible | |
syntax on | |
filetype plugin on | |
filetype plugin indent on | |
" by itself, jellybeans doesnt enable 256 colors | |
set t_Co=256 | |
set background=dark |
OlderNewer