Skip to content

Instantly share code, notes, and snippets.

View LemonPi's full-sized avatar

Johnson Zhong LemonPi

View GitHub Profile
@LemonPi
LemonPi / conseq_sort.h
Created December 7, 2014 03:48
Sorting based on frequency of elements into descending order
#include <map>
#include <algorithm> // fill_n, trivially replaceable
// sorts in place based on frequency
template <typename Iter>
void freq_sort(Iter begin, Iter end) {
using T = typename Iter::value_type;
if (begin == end) return;
T current {*begin};
@LemonPi
LemonPi / twodinterface.h
Last active August 29, 2015 14:11
2D array interface
#include <iostream>
template <typename TwoD>
void print2D(const TwoD& mat, size_t m, size_t n) {
for (size_t row = 0; row < m; ++row) {
for (size_t col = 0; col < n; ++col)
std::cout << mat[row][col] << ' ';
std::cout << std::endl;
}
}
@LemonPi
LemonPi / g11.bat
Created April 17, 2015 04:33
Short bat script for compiling c++
@g++ -IC:\Users\Johnson\Documents\mylibs -o %1 %1.cpp %2 %3 %4 %5 %6 %7 %8 %9 -std=c++11 2> log.txt && echo no error
@LemonPi
LemonPi / roots.py
Created October 12, 2013 01:10
Newton's method for finding nth root of number r to p decimal places correct
def newton(n, r, p = 0):
"""
(int(n), num(r), int(p)) -> float
Return the nth root of a positive number r displaying p correct decimals.
"""
x = 1 # start with guess = 1
while True:
fx = x**n - r # function of this form solves all roots
@LemonPi
LemonPi / rnf.py
Created November 16, 2013 17:56
Reduced normal form in python
# Naive implementation of reduce normal form with some helper functions
from fractions import *
# helper functions
def row_sub(reduceby, reduceto, index):
"""
Take row to reduce and row to reduce by with the index of the element to
reduce to 0. Assume reduce by has value 1 in the index of reduceto.
"""
row_merged = []
@LemonPi
LemonPi / binderprototype.scad
Created December 7, 2013 18:29
OpenScad model / animation for an elastic spring shield
module ring(inner_radius, thickness, height)
{
outer_radius = inner_radius + thickness;
padding = 0.1;
difference()
{
cylinder(r=outer_radius,h=height,center=true);
cylinder(r=inner_radius,h=height+padding,center=true);
@LemonPi
LemonPi / pukemore.js
Created December 16, 2013 21:41
Import this if you want to be cool
var timer = setInterval(transform,50);
var degreez = 0;
var allSet = false;
var bellybutton;
function transform(){
if (!allSet) {
var transOrigin = "50% 50%";
document.body.parentElement.style.height = "100%";
document.body.style.minHeight = "100%";
document.body.style.transformOrigin = transOrigin;
- Autoformatting with clang-format and CppStyle addon for eclipse
- an IDE (seriously, vim and sublime is nice and all, but for any large production project IDEs will save a lot of effort and time)
@LemonPi
LemonPi / typeddict.py
Last active November 9, 2016 10:39
python dictionary with restricted key type, given at compile time
# -*- coding: utf-8 -*-
class TypedDict(dict):
"""Dictionary with keys restricted to a specific type, given at creation time"""
def __init__(self, key_type):
print("initializing with", key_type)
self.key_type = key_type
def __getstate__(self):
return self.key_type, dict(self)
@LemonPi
LemonPi / g11
Last active February 23, 2017 14:24
Quick c++11 compile script for bash; put in user path (e.g. /usr/bin/)
filename=$(basename "$1")
outfile="${filename%.*}"
{ g++ -o $outfile $@ -std=c++11 2>&1; } | tee log.txt