Skip to content

Instantly share code, notes, and snippets.

@Barakat
Barakat / Main.java
Created July 20, 2015 01:13
Java Stream#flatMap minimal example.
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<List<Integer>> nested = Arrays.asList(
Arrays.asList(1, 2, 3),
Arrays.asList(4, 5, 6),
Arrays.asList(7, 8, 9)
@Barakat
Barakat / prime.py
Last active March 30, 2021 19:55
Prime-counting function and x/ln(x) plot
#!python
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
def pi(x):
prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541]
assert x <= prime[-1]
i = 0
while prime[i] < x:
@Barakat
Barakat / ConsistentHash.java
Created August 30, 2015 01:49
Simple Java consistent hashing implementation, do not use in production
import java.util.Map;
import java.util.TreeMap;
import java.util.SortedMap;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.math.BigInteger;
import java.io.UnsupportedEncodingException;
public class ConsistentHash {
private static int MAX_REPLICA = 100;
@Barakat
Barakat / caesar.py
Last active September 3, 2015 03:44
Caesar cipher implementation in Python
def caesar_enc(plain, key):
plain = plain.encode('utf-8')
cipher = bytearray(plain)
for i, c in enumerate(plain):
cipher[i] = (c + key) & 0xff
return bytes(cipher)
def caesar_dec(cipher, key):
plain = bytearray(len(cipher)) # at most, len(plain) <= len(cipher)
for i, c in enumerate(cipher):
@Barakat
Barakat / DebugMessageCallback.cpp
Created July 10, 2016 06:00
An implementation of glDebugMessageCallbackARB callback
#include <gl/glew.h>
#include <string>
void GLAPIENTRY DebugMessageCallback(GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam)
@Barakat
Barakat / matrix.c
Created December 15, 2016 10:39
Cache optimized serial matrix multiplication
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <assert.h>
#include <stdbool.h>
typedef struct {
size_t rows;
size_t columns;
@Barakat
Barakat / ean13-checksum.py
Created February 27, 2017 10:50
Compute the checksum of EAN-13 barcodes
def compute_ean13_checksum(barcode):
assert len(barcode) == 12 and barcode.isdigit()
return (10 - sum(int(i) * j for i, j in zip(barcode, (1, 3) * 6)) % 10) % 10
@Barakat
Barakat / mini.kt
Last active June 6, 2017 00:34
Mini-programming language in pure-Kotlin
import java.io.ByteArrayInputStream
import java.io.InputStream
import java.io.PrintStream
import java.util.*
import java.util.concurrent.Callable
enum class Token constructor(val symbol: String) {
IF("if"),
ELSE("else"),
@Barakat
Barakat / CMakeLists.txt
Last active October 14, 2023 22:36
Minimal Objective-C++ SDL2 + Metal example
# CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(example)
set(CMAKE_CXX_STANDARD 11)
find_package(SDL2 REQUIRED)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_COMPILER_IS_CLANGCXX 1)
endif ()
@Barakat
Barakat / ascii_upper_encoder.py
Created June 27, 2018 19:30
Encode binary data into uppercase-only ASCII
ASCII_A_CODE = ord('A')
def ascii_upper_encode(data):
code = ''
for byte in data:
code += chr((byte >> 4) + ASCII_A_CODE)
code += chr((byte & 0xf) + ASCII_A_CODE)
return code
def ascii_upper_decode(code):