This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example program | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
#include <functional> | |
#include <cassert> | |
#include <iomanip> | |
#include <numeric> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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:: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <variant> | |
#include <string> | |
#include <vector> | |
#include <exception> | |
#include <functional> | |
#include <iostream> | |
using namespace std; | |
void numToStringNum(variant<int, string>& x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); | |
}); |