Skip to content

Instantly share code, notes, and snippets.

@nbyouri
nbyouri / file_stats_from_har.awk
Created November 25, 2017 14:51
Awk script to extract file statistic from a har file
function FileStats(file) {
nb_js = 0
nb_img = 0
nb_css = 0
nb_others = 0 # also html/text
nb_lines = 0
cmd = sprintf("%s %s", "jq '(.log.entries[]|[ .request.url])|@csv'", file)
while (cmd|getline) {
if (/\.png|\.jpg|\.gif/) {
nb_img++
@nbyouri
nbyouri / quickselect.java
Created August 20, 2017 17:14
Median in O(n)
public static int median(Vector a, int lo, int hi) {
if (lo == hi) {
return a.get(lo);
}
int n = a.size() / 2;
for (;;) {
int pivotIndex = randomPivot(lo, hi);
pivotIndex = partition(a, lo, hi, pivotIndex);
@nbyouri
nbyouri / huffman.java
Created August 19, 2017 09:16
huffman code
package Révision;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
@nbyouri
nbyouri / powermethod.jl
Last active August 23, 2017 20:20
Power Method for PageRank
#############################################
# PageRank algorithm using the power method #
# youri mouton 18/08/2017 #
#############################################
# Adjacency matrix
A = [ 0 2 0;
0 0 2;
1 1 0];
@nbyouri
nbyouri / matmul.dfy
Created May 27, 2017 08:48
squared matrix multiplication in dafny
method matmul(a: array2<int>, b: array2<int>) returns (c : array2<int>)
requires a != null
requires a.Length0 > 0 && a.Length1 > 0
requires a.Length0 == a.Length1 // que pour matrices carrées
requires b != null
requires b.Length0 > 0 && b.Length1 > 0
requires b.Length0 == b.Length1 // que pour matrices carrées
requires a.Length0 == b.Length0
requires a.Length1 == b.Length1
ensures fresh(c)
package Calcu;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;
@nbyouri
nbyouri / pagerank.m
Last active January 13, 2017 09:00
PageRank in matlab
% Reviewed par Marco Saerens le 13/01/2017
% matrice des degres sortants
n = size(A)
D = diag(sum(A, 2))
% matrice de probabilites de transition
P = D^-1 * A
% ajout d'un lien faible entre tous les liens
alpha = 0.85
% vecteur de 1 de taille n
@nbyouri
nbyouri / levenshtein.jl
Last active August 11, 2017 08:39
Tool to calculate edit-distance (levenshtein number) of two strings
# Compute the edit-distance of two strings, also called the Levenstein number.
function lev(x, y)
# edit-distance array
xl = length(x) + 1
yl = length(y) + 1
m = zeros(Int, xl, yl)
# init first line and column
for i = 1 : yl
m[1, i] = i - 1
@nbyouri
nbyouri / pagerank.jl
Last active January 4, 2017 14:23
explication pagerank
# PageRank
# Le graphe en exemple est celui de la Q4 de l'exam 09/2015:
#
# 1 -----→ 4
# ↑ \ ↑
# | `---↘ |
# 2 -----→ 3
#
# Matrice d'adjacence:
A = [ 0 1 1 0;
@nbyouri
nbyouri / counting.jl
Created January 2, 2017 10:56
Small Julia utilities for counting
# C(n,r)
function C(n::Int, r::Int)
return (factorial(n)) / (factorial(r)*factorial(n - r))
end
# P(n,r)
function P(n::Int, r::Int)
return (factorial(n) / factorial(n -r))
end
# Sum of a function in a j .. n range
function ∑(j::Int, n::Int, f::Function)