Skip to content

Instantly share code, notes, and snippets.

#include <iostream>
#include <vector>
using namespace std;
inline int max(int a, int b) {
return a > b ? a : b;
}
int trunc(int v) {
if (abs(v) == 1) return abs(v);
@dgodfrey206
dgodfrey206 / maxSubArray.cpp
Last active April 28, 2016 20:53
Calculating the length of the longest contiguous subsequence
#include <iostream>
#include <vector>
using std::max;
// Let A be an array of length N
// Let M(i) be the largest contiguous subsequence ending at index i
// M(0) = 1
// M(i) = max(M(i),M(j)+1) for j<i and A[j]<=A[i]
int maxSubArray(int A[], int N) {
@dgodfrey206
dgodfrey206 / morse.cpp
Created April 18, 2016 00:01
Morse code translator
#include <algorithm>
#include <streambuf>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <utility>
#include <string>
#include <vector>
#include <cctype>
#include <memory>
@dgodfrey206
dgodfrey206 / unary2base10.cpp
Created February 28, 2016 03:31
Converting unary to base 10
#include <iostream>
#include <string>
#include <sstream>
std::string UnaryToBinary(std::string str) {
std::string binary;
binary.reserve(30);
char flag;
for (std::stringstream ss(str); ss >> str; ) {
@dgodfrey206
dgodfrey206 / infix2postfix.cpp
Last active February 6, 2016 18:53
Infix to Postfix (Reverse Polish Notation)
#include <iostream>
#include <cstring>
#include <stack>
uint8_t precedence[256];
bool HasHigherPrecedence(char a, char b) {
return precedence[(uint8_t)a] < precedence[(uint8_t)b];
}
#include <iostream>
#include <utility>
#include <tuple>
namespace detail {
template<class... Args>
struct stream_bump {
std::tuple<Args...> args;
template<class F>
void operator->*(F&& f) && {
@dgodfrey206
dgodfrey206 / rc.cpp
Created June 21, 2015 21:16
Reference collapsing
#include <utility>
template<class T> struct TD;
template<class T> void f(T&& x) { TD<decltype(x)>(); }
// T & = T&
// T && = T&&
// T& & = T&
// T& && = T&
@dgodfrey206
dgodfrey206 / word_inserter.cpp
Last active March 26, 2022 16:40
Word inserter
#include <iostream>
#include <sstream>
#include <algorithm>
template<class charT>
struct word_inserter_impl {
word_inserter_impl(int words, std::basic_string<charT>& str, charT delim)
: str_(str)
, delim_(delim)
, words_(words)
@dgodfrey206
dgodfrey206 / colornode.cpp
Last active March 26, 2022 16:41
HackerRank
#include <string>
#include <iostream>
#include <initializer_list>
enum struct Color {
red,blue,orange,purple,green,black,yellow,invalid=-1
};
Color FromStringToColor(std::string const& str) {
return ((str == "red") ? Color::red :
@dgodfrey206
dgodfrey206 / pair.cpp
Last active March 26, 2022 16:41
HackerRank2
#include <iostream>
#include <string>
#include <cctype>
#include <cassert>
namespace detail {
bool isMatchingPair(char c1, char c2) {
return std::isupper(c1) && std::tolower(c1) == c2;
}
}