Skip to content

Instantly share code, notes, and snippets.

@andreburgaud
andreburgaud / cyber_savvy.py
Last active July 7, 2024 20:39
Infinite Campus We Are Cyber Savvy T-shirt
import sys
tshirt = [
0b01001001, 0b01101110, 0b01100110, 0b01101001,
0b01101110, 0b01101001, 0b01110100, 0b01100101,
0b00100000, 0b01000011, 0b01100001, 0b01101101,
0b01110000, 0b01110101, 0b01110011, 0b00100000,
0b01001001, 0b00100000,
0b01110111, 0b01110010,
0b01101001, 0b01110100,
@andreburgaud
andreburgaud / list_comp_misc.py
Last active August 29, 2015 14:08
List comprehensions in Python. Translation from book "Programming Haskell" examples. Erik Meijer Functional Programming class on Edx.
pairs = lambda xs: zip(xs, xs[1:])
print pairs([1,2,3,4,5,6]) # [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
sorted = lambda xs: all([x<=y for (x, y) in pairs(xs)])
print sorted([1,2,3,4,5,6,7]) # True
print sorted([1,2,3,4,5,9,7]) # False
@andreburgaud
andreburgaud / webcrypto.js
Created November 16, 2015 04:29
Basic example of Web Crypto (keygen/encrypt/decrypt) using symmetric encryption with AES Gallois Counter Mode algorithm.
// Resources:
// https://github.com/diafygi/webcrypto-examples
// https://developer.mozilla.org/en-US/docs/Web/API/CryptoKey
// http://blog.engelke.com/tag/webcrypto/
// Tested in Firefox Developer Edition
function strToArrayBuffer(str) {
var buf = new ArrayBuffer(str.length * 2);
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
@andreburgaud
andreburgaud / first.erl
Last active June 23, 2017 05:16
FutureLearn - Functional Programming in Erlang 1.9
-module(first).
-export([double/1,treble/1,square/1,mult/2,area/3]).
mult(X, Y) ->
X*Y.
double(X) ->
mult(2, X).
% Area of a triangle (Heron's formula)
@andreburgaud
andreburgaud / patterns.erl
Last active June 25, 2017 16:19
FutureLearn - Functional Programming in Erlang 1.15 - Erlang pattern matching examples with hand-crafted tests
-module(patterns).
-export([all_tests/0]).
% all_tests invokes all tests (xor, maxThree, howManyEquals)
all_tests() ->
all_xor_tests(),
test_max_three(),
test_how_many().
% all_xor_tests invokes all XOR tests.
@andreburgaud
andreburgaud / fib.erl
Last active June 25, 2017 20:26
FutureLearn - Functional Programming in Erlang 1.19 - Recursion
-module(fib).
-export([fib/1]).
% Fibonacci direct recursion implementation
fib(0) ->
0;
fib(1) ->
1;
fib(N) when N>0 ->
fib(N-1) + fib(N-2).
@andreburgaud
andreburgaud / recursion.erl
Created June 25, 2017 22:04
FutureLearn - Functional Programming in Erlang 1.21 - Tail Recursion
-module(recursion).
-export([test/0,fib/1,loop/1,sum/1,max/1,perfect/1]).
% Typical loop in Erlang
loop(N) when N>0 ->
io:format("~p~n", [N]),
loop(N-1);
loop(_) -> % implies N = 0
io:format("bye~n").
@andreburgaud
andreburgaud / product.erl
Created June 27, 2017 04:14
FutureLearn - Functional Programming in Erlang 2.6 - Functions over lists
-module(product).
-export([all_tests/0,test_sum/0,test_prod/0,test_maximum/0,sum/1,sumT/1,prod/1,
prodT/1,maximum/1,maximumT/1]).
%% sum implementation with direct recursion (per the video)
sum([]) -> 0;
sum([X|Xs]) -> X + sum(Xs).
%% sum implementation with tail recursion (per the video)
sumT(Xs) -> sumT(Xs, 0).
@andreburgaud
andreburgaud / transform.erl
Last active June 28, 2017 05:17
FutureLearn - Functional Programming in Erlang 2.9 - Constructing lists with recursive functions
-module(transform).
-export([double/1,evens/1,median/1,modes/1,sort/1,counters/2]).
%% ----------------------------------------------------------------------------
%% Double
double([]) -> [];
double([X|Xs]) -> [2*X | double(Xs)].
%% ----------------------------------------------------------------------------
%% Evens
@andreburgaud
andreburgaud / take.erl
Created June 28, 2017 15:16
FutureLearn - Functional Programming in Erlang 2.11 - Where do I begin?
-module(more_lists).
-export([reverse/1,take/2,take2/2,test_take/0,test_take2/0]).
%% Take with direct recursion
-spec take(integer(), [T]) -> [T].
take(_, []) -> []; % handles case where N > length(Xs)
take(0, _) -> [];
take(N, [X|Xs]) when N > 0 -> [X | take(N-1, Xs)].
test_take() ->