Skip to content

Instantly share code, notes, and snippets.

View theSundayProgrammer's full-sized avatar

Joseph Mariadassou theSundayProgrammer

View GitHub Profile
@theSundayProgrammer
theSundayProgrammer / allcomo.cpp
Created June 7, 2023 06:06
list all combinations
#include <iostream>
#include <tuple>
#include <vector>
#include <fstream>
#include <algorithm>
#include <functional>
using namespace std;
void print(const vector<int>& nums, ostream& fout)
@theSundayProgrammer
theSundayProgrammer / combo-test.lua
Created October 20, 2022 23:26
generate all combinations and output result using coroutines
local combo = require "combo"
function print_table(T)
local str=""
for i,v in ipairs(T) do
str = str .. string.format("%d,",v)
end
print(str)
end
for p in combo.combo_gen({1,2,3,4,5,6,7}, 1) do
@theSundayProgrammer
theSundayProgrammer / mysin.cc
Last active September 26, 2022 00:39
Lua extension module in C++
/* Modified version of extend.c in luajit source code
* GCC compile command is as follows
* g++ -I $HOME/.local/include/ -fPIC -shared -o mysin.so extend.cc
* Notice that it is NOT linked with the lua library
* because in Linux, executables can export functions and
* linking is done on load
*/
#include <math.h>
#include <luajit/lua.hpp>
static
@theSundayProgrammer
theSundayProgrammer / lua_myobject.cpp
Last active September 24, 2022 11:54 — forked from Youka/lua_myobject.cpp
Example of Lua in C++ and userdata objects
// Lua C API
#include <luajit/lua.hpp>
// C++ input/output streams
#include <iostream>
// MyObject as C++ class
class MyObject{
private:
double x;
public:
@theSundayProgrammer
theSundayProgrammer / Tictactoe.js
Created May 8, 2021 07:40
The model part of the MVC code for a web page that plays tic tac toe.
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
@theSundayProgrammer
theSundayProgrammer / asynch_sock.lua
Last active November 17, 2020 02:05
Asynchronous IO using Lua Coroutines
#!/usr/local/openresty/luajit/bin/luajit
--this is the code as described in
-- Chapter 26, "Programming Lua" by Roberto Ierusalimschy
--except for the function dispatch2 which is a replacement
--for dispatch. It uses the return value of select to access
--only the ready sockets. There is a small change to download to save
--the file as well
local socket = require "socket"
local function receive (connection)
@theSundayProgrammer
theSundayProgrammer / App.cpp
Last active March 23, 2020 06:22
Problem 15.5 from Cormen's book: longest palindromic sequence
/**
For example if the input is "charachter" the output shout be "carac"
*/
#include <iostream>
#include <string>
class CostArray{
int *cost;
int width;
public:
CostArray(int m, int n):width(n){
@theSundayProgrammer
theSundayProgrammer / vimABC.tex
Created July 15, 2019 02:07
Some handy Vim Commands
\documentclass{beamer}
\begin{document}
\title{ABC of Vim}
\author{Joe Mariadassou}
\date{\today}
\frame{\titlepage}
\frame{\frametitle{Table of contents}\tableofcontents}
@theSundayProgrammer
theSundayProgrammer / bounds.ml
Created March 12, 2019 10:54
Ocaml Array module does not have upper or lower bound functions
let lower_bound cmp arr x =
let rec aux lo hi =
if lo = hi then lo
else let mid = lo + (hi-lo)/2 in
if cmp arr.(mid) x < 0
then aux (mid + 1) hi
else aux lo (mid)
in aux 0 (Array.length arr)
let upper_bound cmp arr x =
@theSundayProgrammer
theSundayProgrammer / IntToString
Last active March 3, 2019 02:26
Convert an integer to string. Example: 2347 is two thousand three hundred and forty seven
#include <string>
#include <vector>
using std::wstring;
const std::vector< wstring> first20 {
L"zero", L"one", L"two", L"three", L"four", L"five", L"six", L"seven", L"eight", L"nine", L"ten",
L"eleven", L"twelve", L"thirteen", L"fourteen", L"fifteen", L"sixteen", L"seventeen", L"eighteen", L"nineteen" };
const std::vector< wstring> tees{ L"zero", L"ten", L"twenty",L"thirty", L"forty", L"fifty", L"sixty", L"seventy", L"eighty", L"ninety" };
wstring int_to_str(unsigned int n)
{
if (n < 20)