Skip to content

Instantly share code, notes, and snippets.

@tuket
tuket / multi_sort.cpp
Created September 30, 2017 16:53
multi_sort.cpp
// Example program
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cassert>
#include <iomanip>
#include <numeric>
@tuket
tuket / euclides.py
Created October 26, 2017 19:33
simple python implementation of the extended euclidean algorithm
def euclides(a, b):
S = []
while a % b != 0:
S.append(a // b)
a, b = b, a % b
x1, x2 = 1, -S.pop()
while len(S):
q = S.pop()
x1, x2 = x2, x1 - x2*q
return (b, x1, x2)
@tuket
tuket / SimpleAuthServer.py
Created November 6, 2017 11:53 — forked from fxsjy/SimpleAuthServer.py
SimpleAuthServer: A SimpleHTTPServer with authentication
#!/usr/bin/env python
"""
Very simple HTTP server in python.
Usage::
./dummy-web-server.py [<port>]
Send a GET request::
curl http://localhost
Send a HEAD request::
curl -I http://localhost
Send a POST request::
@tuket
tuket / powmod.cpp
Created November 9, 2017 22:09
compute power modulo n
#include <iostream>
#include <CppUTest/CommandLineTestRunner.h>
#include <CppUTest/TestHarness.h>
#include <numeric>
using namespace std;
// (x^p)%n
unsigned powMod(unsigned x, unsigned p, unsigned n)
{
@tuket
tuket / solve_hanoi.cpp
Created January 18, 2018 23:37
hanoi towers
#include <iostream>
// there are 3 sticks: 0, 1, 2
void move(int x, int from, int to)
{
if(x == 1) printf("%d->%d\n", from, to);
else
{
int other = 3-from-to;
move(x-1, from, other);
@tuket
tuket / 000_INDEX.md
Last active March 22, 2018 22:19
type_traits example
@tuket
tuket / shift_rotate.cpp
Created June 2, 2018 21:24
Shift Rotate
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <chrono>
using namespace std;
template <typename T>
void vecSwap(vector<T>& v, int i1, int i2, int n) {
@tuket
tuket / variant_numbers.cpp
Created July 13, 2018 15:54
variant_numbers.cpp
#include <variant>
#include <string>
#include <vector>
#include <exception>
#include <functional>
#include <iostream>
using namespace std;
void numToStringNum(variant<int, string>& x)
@tuket
tuket / fastDiv.cpp
Created July 20, 2019 09:11
Integer division can be decomposed in a bitshift and a multiplication
/*
When I was looking at the disassembly of my compiler, I noticed that it decided to, instead of
dividing by 3, multiply by 2863311531.
At first I was puzzled by this, but it wasn't as complicated as I thought.
So it turns out that an integer division can be decomposed in two operatations:
1) A bit shift: that's just deviding by a power of 2
2) A multiplication that overflows
The compiler dicides to do this because division is an expensive operation for CPUs
So here I wrote some code that computes the right pair of values for the given divider
*/
@tuket
tuket / paint.zig
Created August 13, 2019 09:31
paint in zig using SDL2
// compile in ubuntu:
// $ zig build-exe paint.zig --library SDL2 --library SDL2main --library c -isystem "/usr/include" --library-path "/usr/lib/x86_64-linux-gnu"
const std = @import("std");
const warn = std.debug.warn;
const fmt = std.fmt;
const c = @cImport({
@cInclude("SDL2/SDL.h");
});