Skip to content

Instantly share code, notes, and snippets.

@jgcoded
jgcoded / IntSet Combinations.hs
Last active August 29, 2015 14:21
Defines a function nCombination which generates and prints out all n-combinations as a list. Uses Data.IntSet.
import Control.Monad (unless)
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
-- Generate and print out all combinations up to size n
nCombination :: Int -> IO ()
nCombination n = do
print $ IntSet.toList IntSet.empty
combination' n IntSet.empty $ makeBaseCase n
@jgcoded
jgcoded / Sorts.c
Last active October 5, 2015 03:13
Provides a C implementation of selection, insertion, bubble, merge, and quick sort
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
// Signature for all sorting functions
typedef void (*SortFunc)(int*,int);
void swap(int* a, int* b)
@jgcoded
jgcoded / binarytree.c
Last active August 29, 2015 14:21
Provides a C implementation of a binary tree along with many tree-related functions
#include <stdio.h>
#include <stdlib.h>
struct tree_node
{
int data;
struct tree_node *left, *right;
};
struct tree_node *create_node(int data)
@jgcoded
jgcoded / Universal_Input.hs
Last active October 5, 2015 03:28
Use this snippet to parse any line-delimited user input into a list of a specified type
{-
If you want to read in Strings: getInputLinesWith id n
if you want to read in Ints: getInputLinesWith read n :: IO [Int]
where n is how many lines to take input from
-}
getInputLinesWith :: (String -> a) -> Int -> IO [a]
getInputLinesWith f n = fmap (map f) (replicateM n getLine)
@jgcoded
jgcoded / greedy.cpp
Last active August 29, 2015 14:21
Scheduling and coin change greedy algorithms implemented in c++11
#include <algorithm>
#include <chrono>
#include <vector>
#include <map>
#include <iostream>
#include <tuple>
#include <string>
#include <ctime>
namespace chrono = std::chrono;
@jgcoded
jgcoded / arithmetic.cpp
Created May 22, 2015 23:38
Evaluates elementary arithmetic expressions by converting from infix to post-fix notation
#include <string>
#include <stack>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cctype>
#include <vector>
using namespace std;
#include <iostream>
#include <queue>
#include <vector>
#include <string>
#include <utility>
#include <functional>
#include <cstring>
using namespace std;
@jgcoded
jgcoded / WindowsSharedMemory.cpp
Created June 26, 2015 21:08
Using Windows Shared Memory in Unreal Engine because the Unreal Engine API for shared memory does not work on Windows.
// Fill out your copyright notice in the Description page of Project Settings.
#include "VTBGame.h"
#ifdef WIN32
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <memory>
@jgcoded
jgcoded / djikstra.cpp
Created October 10, 2015 17:49
Modern C++ implementation of Djkstra's algorithm. Runs in O(Elog(V)), supports negative weights, but not negative weight cycles.
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <limits>
#include <functional>
using namespace std;
@jgcoded
jgcoded / traveling_salesman.cpp
Last active January 22, 2024 09:43
Traveling Salesman solution in c++ - dynamic programming solution with O(n^2 * 2^n).
#include <vector>
#include <iostream>
using namespace std;
/**
\brief Given a complete, undirected, weighted graph in the form of an adjacency matrix,
returns the smallest tour that visits all nodes and starts and ends at the same
node. This dynamic programming solution runs in O(n^2 * 2^n).