Skip to content

Instantly share code, notes, and snippets.

@pichi
pichi / search_setperms.c
Last active August 29, 2015 14:04
Fast finding of all permutations of given set of characters in string
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
// fast int
#if __WORDSIZE == 64
typedef unsigned long int uint_fast_t;
#define UINT_FAST(c) c ## UL
#else
typedef unsigned int uint_fast_t;
%% Power function (X ^ Y) and root function (X ^ (1/Y)) for
%% integers in Erlang
%% by Kenji Rikitake <kenji.rikitake@acm.org> 26-JAN-2010
%% modified by Hynek Vychodil <vychodil.hynek@gmail.com> 2-FEB-2010
%% Distributed under MIT license at the end of the source code.
-module(bignum_root).
-export([power/2, root/2, sqrt/1]).
proplists_get_value(Key, List)->
case lists:keyfind(Key,1,List) of
{K,V} when K =:= Key ->
V;
_->
proplists_get_value_(Key, List)
end.
proplists_get_value_(_,[])->
undefined;
@pichi
pichi / LCP.pm
Created June 19, 2012 09:35
Longest Common Prefix in Erlang
use strict;
use warnings;
package LCP;
sub LCP {
return '' unless @_;
return $_[0] if @_ == 1;
my $i = 0;
my $first = shift;
@pichi
pichi / memory_linear.sql
Created September 7, 2013 06:31
Fastest previous date solution.
drop table if exists products;
drop table if exists prod;
drop table if exists `out`;
SET max_heap_table_size = 1024*1024*1024;
create table products ( id int not null, partner int not null, start date not null) engine=memory ;
load data infile '/tmp/test.data' into table products columns terminated by ',';
create table prod ( r int not null AUTO_INCREMENT primary key, rp int default 1, id int not null, partner int not null, start date ) engine=memory as select id, partner, start from products order by partner, start;
drop table products;
update prod set rp = r-1;
create table `out` engine=memory select p1.id, p1.partner, p1.start, p2.start prev from prod p1 left join prod p2 on p2.r = p1.rp and p2.partner = p1.partner;
@pichi
pichi / polish_numbers.erl
Created November 25, 2013 22:20
Generator of Befunge script for numbers using * and + operations written in Erlang.
-module(polish_numbers).
-export([generate/1]).
generate(N) when is_integer(N), N >= 0 ->
Self = self(), % spawn subprocess to avoid process dictionary pollution by put/get
Pid = spawn_link(fun() -> Self ! {result, self(), gen(N, 999999)} end),
[X || {_,X} <- receive {result, Pid, R} -> R end].
gen(N, _) when N < 10 -> [{1, [N+$0]}];
@pichi
pichi / polish_numbers.c
Last active December 29, 2015 09:28
Generator of Befunge script for numbers using * and + operations written in C. Compile using gcc -lm -O3 -o polish_numbers polish_numbers.c
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
typedef struct {
unsigned char length;
char op;
} best_rec_head;
@pichi
pichi / binary_code.c
Last active December 30, 2015 22:09
Generating "binary code" (see http://stackoverflow.com/q/20422126/49197)
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
enum {
max_k = 6, // it can't be bigger than 6 because it has to fit in 64 bit set
};
typedef uint64_t set;
typedef unsigned char number; // 1 << max_k has to fit
@pichi
pichi / vertical_tree.erl
Last active December 31, 2015 02:09
Module for getting "vertical lines" (see http://stackoverflow.com/q/20521098/49197) It also contain tree pretty printer (far more sophisticated then getting vertical lines BTW)
-module(vertical_tree).
-export([draw/1, get_verical_lines/1]).
-record(node, {
value,
left = nil,
right = nil
}).
@pichi
pichi / Result
Last active March 30, 2016 17:59
Stopwords Benchmark
$ erl -pa eministat/ebin
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V7.3 (abort with ^G)
1> {ok, Bin} = file:read_file("/home/hynek/Downloads/words.txt"), L = string:tokens(binary_to_list(Bin), "\s\r\n"), length(L).
113809
2> length(lists:filter(fun stopwords_clause:is_stopword/1, L)).
122
3> length(lists:filter(fun stopwords_map:is_stopword/1, L)).
122