Skip to content

Instantly share code, notes, and snippets.

View lawliet89's full-sized avatar
🧑‍🤝‍🧑
He/him

Yong Wen Chua lawliet89

🧑‍🤝‍🧑
He/him
  • Singapore
  • 15:29 (UTC +08:00)
View GitHub Profile
@lawliet89
lawliet89 / mergesort.cpp
Created November 12, 2012 22:54
C++ Mergesort with in place merging
// http://en.literateprograms.org/Merge_sort_(C_Plus_Plus)
#include <algorithm>
#include <iterator>
#include <iostream>
// Note: InputIterator must allow the minus operator -- i.e. RandomAccess
// The end element is AFTER the last element
// output refers to the iterator for the first output item
// You can mix and match different input and output iterators (i.e. vectors, arrays etc.)
template <typename InputIterator, typename OutputIterator> void mergesort(InputIterator start, InputIterator end, OutputIterator output){
@lawliet89
lawliet89 / palindrome.cpp
Created November 12, 2012 23:22
The Next Palindrome (SPOJ)
// https://www.spoj.pl/problems/PALIN/
// Concept: http://www.algorithmist.com/index.php/SPOJ_PALIN
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool lessThan(vector<int> obj1, vector<int> obj2);
void helper(const vector<int> &input, vector<int> &output, int stage=0);
@lawliet89
lawliet89 / prime.cpp
Created November 12, 2012 23:24
Prime Numbers (SPOJ)
// https://www.spoj.pl/problems/PRIME1/
//http://www.swageroo.com/wordpress/spoj-problem-2-prime-generator-prime1/
#include <iostream>
using namespace std;
int main(){
int count;
cin >> count;
while(count--){
@lawliet89
lawliet89 / 3m.cpp
Created November 13, 2012 00:11
Mean, Median, Mode
// Mean medium and mode
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <map>
using namespace std;
// This is a variant of merge sort that does mode counts and sums all the items.
template <typename InputIterator, typename OutputIterator, typename ValueType, typename SumType> void mergesort(InputIterator start, InputIterator end, OutputIterator output, SumType &sum, map<ValueType, int> &mode){
@lawliet89
lawliet89 / RadixSort.cpp
Created November 18, 2012 18:03
C++ Generic Radix Sort
// Because I'm lazy and I used a C++11 syntax to initialise a vector, you have to use G++ to compile this,
// Compile with something like g++ -pedantic -std=c++0x RadixSort.cpp
// Or for the more pedantic g++ -pedantic -std=c++0x -Wall -Wextra -Werror=return-type -Wno-reorder RadixSort.cpp
// Reference http://en.wikipedia.org/wiki/Radix_sort
#include <iostream>
#include <vector>
#include <algorithm> // Required
#include <iterator> // Required
#include <queue> // Required
using namespace std;
@lawliet89
lawliet89 / quicksort.cpp
Last active October 12, 2015 23:58
C++ Generic Quicksort
// Quicksort - see makefile for compiling options with G++.
// Will not compile in VS2012 due to initialiser syntax in main() for std::vector
#include <iostream>
#include <vector>
#include <algorithm> // Required
#include <iterator> //Required
#include <stdlib.h> // Required
#include <time.h> // Required
// Function Prototypes
@lawliet89
lawliet89 / Strip.py
Last active December 17, 2015 04:58
Strip list/dictionaries of empty items recursively
def strip(content):
"""Strip Empty Variables"""
if (type(content) is list):
return strip_list(content)
elif (type(content) is dict):
return strip_dictionary(content)
elif (content):
return content
else:
return None
@lawliet89
lawliet89 / concat.py
Created May 10, 2013 12:41
Python concatenate list of string
def list_to_string(lst, delim=''):
"""Concatenate a list of strings to one single string. Specify a delimiter to append to end of each fragment"""
return reduce((lambda a, b: a + delim + b), lst)
@lawliet89
lawliet89 / gist:5728344
Created June 7, 2013 10:14
Git Regular Expression search (grep) for missing trailing newline
git grep -I -E "[^\n]\'"
@lawliet89
lawliet89 / gist:5899943
Created July 1, 2013 10:55
Git grep for trailing whitespace
git grep -I -E '^.+[ ]+$'