Skip to content

Instantly share code, notes, and snippets.

@walac
walac / NextPermutation.nb
Created August 22, 2012 00:07
An ugly "hurry up" implementation of NextPermutation algorithm in Mathematica
Vk[p_List] :=
Module[{r =
DeleteCases[
MapIndexed[If[#1[[1]] < #1[[2]], Length[p] - First[#2]] &,
Reverse@Partition[p, 2, 1]], Null]},
If[Length[r] > 0, First[r], Null]]
Vl[p_List, k_Integer] :=
Module[{r =
DeleteCases[
@walac
walac / name.sh
Last active May 28, 2016 02:25
A simple script to make the .avi and .srt file names of my favorite series equal.
#!/bin/bash
if [ $# -gt 0 ]; then
basepath="$1"
else
basepath=
fi
for i in "${basepath}"*.mkv; do
e=$(echo "${basepath}"$i | sed -e 's/.*[Ss][0-9]\+[Ee]\([0-9]\+\).*/\1/')
@walac
walac / mergesort.py
Last active December 11, 2015 08:29
A (kind of) functional implementation of the merge sort algorithm in Python.
def merge(a, b):
if not a:
return b
if not b:
return a
return a[:1] + merge(a[1:], b) if a[0] < b[0] else b[:1] + merge(a, b[1:])
def mergesort(a):
len_a = len(a)
if len_a == 1:
@walac
walac / test-ffi.cc
Last active December 20, 2015 05:59
A crazy libffi test.
#include <ffi.h>
#include <iostream>
using namespace std;
class IInterface {
public:
virtual void fn1(int a, int b) = 0;
virtual void fn2(int a, int b) = 0;
~IInterface() {}
@walac
walac / msubstr.c
Last active December 26, 2015 17:09
MSUBSTR spoj problem
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LEN 3000
#define AUX_LEN (MAX_LEN * 2 + 1)
int fn[AUX_LEN];
char aux[AUX_LEN+1];
int p[AUX_LEN];
@walac
walac / problem.cc
Last active December 28, 2015 05:29
Some random stuff.
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <unordered_map>
using namespace std;
namespace {
@walac
walac / test-openusb.py
Last active January 4, 2016 22:09
Test of the openusb backend for PyUSB
from usb.core import find
from usb.backend import openusb
import sys
if len(sys.argv) < 2:
print ('Usage: python test-openusb.py idvendor:idproduct (in hex)')
exit(1)
vendor, product = list(map(lambda x: int(x, 16), sys.argv[1].split(':')))
@walac
walac / clear-halt-test.c
Created May 22, 2014 19:56
USB endpoint stall test for libusb-win32
#include <usb.h>
#include <stdio.h>
int main(int argc, char **argv)
{
struct usb_bus *bus;
struct usb_device *dev = NULL;
usb_dev_handle *handle;
char dummy_data[5];
int ret;
@walac
walac / check_ids.awk
Last active August 29, 2015 14:05
Dump the breakpad symbols for fxos phones shared objects under /system/lib and check if they match to the generated by the buildsymbols command
BEGIN {
CRASHSYMBOLS = "~/work/B2G/objdir-debug/dist/crashreporter-symbols/"
}
{
if (system("test -d "CRASHSYMBOLS $2) == 0) {
if (system("test -d "CRASHSYMBOLS $2"/"$1) == 0) {
print $2" ok"
} else {
"ls " CRASHSYMBOLS $2"/" | getline id
print "The ids for "$2" don't match: "$1 " "id
@walac
walac / heap.py
Created December 29, 2014 19:18
A simple heap class for dijkstra's algorithm
#!/usr/bin/python
import operator
def parent(i):
return i/2
def left(i):
return i*2 + 1