Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
dgodfrey206 / jr1.java
Created January 31, 2015 01:11
Java recursion
public int count8(int n) {
int m = n/10;
boolean b = n%10==8;
return n==0 ? 0 : count8(n/10) + (b ? ((b && m%10==8) ? 2 : 1) : 0);
}
@dgodfrey206
dgodfrey206 / jr2.java
Created January 31, 2015 01:18
Java recursion #2
public int countX(String str) {
return str.isEmpty() ? 0 : countX(str.substring(1, str.length())) + ((str.charAt(0)=='x') ? 1:0);
}
@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;
@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 / 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 / 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 / cast.cpp
Last active October 25, 2025 21:39
Explicit type conversion (cast notation)
struct Parent { virtual ~Parent() = default; };
struct Child : private virtual Parent { };
int main()
{
Child c0;
Parent& p0 = static_cast<Parent&>(c0); // FAILS, Parent is inaccessible
Parent& p1 = (Parent&)(c0); // OK ( [expr.cast]/p4.6 ), chooses static_cast
@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 / 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 / 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));
}