Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
dgodfrey206 / spartan.cpp
Last active March 26, 2022 16:42
HackRank
#include <string>
#include <algorithm>
#include <map>
#include <cassert>
namespace detail {
std::map<char, unsigned int> numerals = { {'I', 1}, {'E', 3}, {'V', 5}, {'X', 10}, {'L', 50} };
unsigned int numeral_rank(char c) {
return numerals[c];
@dgodfrey206
dgodfrey206 / tools.cpp
Last active August 29, 2015 14:21
C++14 Metafunction tools
#include <utility>
#include <tuple>
#include <cassert>
struct type_erasure { };
template<class T>
struct wrapper : type_erasure {
wrapper(T&& w) : w_(std::forward<T>(w)) { }
T&& w_;
@dgodfrey206
dgodfrey206 / ToRBGA.cpp
Last active August 29, 2015 14:21
Converts ARGB to RGBA
#include <iostream>
#include <bitset>
#include <vector>
#include <algorithm>
template<class T>
T Rotate(T i) {
return (i << 1) | (i >> sizeof(i));
//return ((i & (1 << (sizeof(T) - 1)) >> sizeof(T)) | (i << 1));
}
@dgodfrey206
dgodfrey206 / tostring.cpp
Last active March 26, 2022 16:43
ToString for arbitrary types
#include <iostream>
#include <cstring>
#define TO_STRING_FOR_CLASS(X)\
template<class T>\
const char* ToString();\
\
template<>\
const char* ToString<X>() {\
static const char* str = #X;\
@dgodfrey206
dgodfrey206 / etc.cpp
Created April 19, 2015 20:24
More explicit type conversion
#include <new>
struct Parent { };
struct Child : Parent { };
struct Tag { };
int main()
{
const Child c;
@dgodfrey206
dgodfrey206 / cast.cpp
Last active August 29, 2015 14:19
Explicit type conversion (cast notation)
struct Parent { virtual ~Parent(); };
struct Child : private virtual Parent { };
int main()
{
Child aa;
Parent& pp = (Parent&)(aa); // OK ( [expr.cast]/p4.6 ), chooses static_cast
Child& aa = (Child&)(pp) // FAILS, chooses static_cast, but that would fail
// because Parent is a virtual base ( [expr.static.cast]/p2 )
@dgodfrey206
dgodfrey206 / using-bug.cpp
Last active August 29, 2015 14:19
GCC bug with using-declaration
struct Base {
Base(int) {}
};
struct D1 : virtual Base {
using Base::Base;
};
int main() {
static_assert(std::is_constructible<D1, int>::value, ""); // FAILS
@dgodfrey206
dgodfrey206 / j45.java
Last active February 5, 2016 20:19
Java recursion #5
/*
Given a string and a non-empty substring sub, compute recursively the largest substring which starts and ends with sub and return its length. */
public int strDist(String str, String sub) {
if (str.length() < sub.length()) return 0;
if (str.substring(0, sub.length()).equals(sub) &&
str.substring(str.length() - sub.length(), str.length()).equals(sub))
return str.length();
int p1 = strDist(str.substring(1), sub);
int p2 = strDist(str.substring(0, str.length() - 1), sub);
@dgodfrey206
dgodfrey206 / jr4.java
Last active August 29, 2015 14:14
Java recursion #4
/* Given a string and a non-empty substring sub, compute recursively
the number of times that sub appears in the string, without the sub
strings overlapping. */
public int strCount(String str, String sub) {
if (str.length() < sub.length()) return 0;
if (str.substring(0, sub.length()).equals(sub) &&
!str.substring(sub.length(), sub.length()).equals(sub))
{
return 1 + strCount(str.substring(sub.length()), sub);
@dgodfrey206
dgodfrey206 / jr3.java
Last active August 29, 2015 14:14
Java recursion #3
public int countHi(String str) {
if (str.isEmpty()) return 0;
if (str.length()==1)return 0;
return countHi(str.substring(1))+((str.substring(0,2).equals("hi"))?1:0) ;
}
public boolean array220(int[] nums, int index) {
if (index >= nums.length-1) return false;
if (nums.length <= 1) return false;