Skip to content

Instantly share code, notes, and snippets.

@ryancooper
ryancooper / CTCI 7.7
Last active August 29, 2015 14:08
CTCI 7.7 Fin d the kth number such that the only prime factors are 3, 5, 7
class Solution{
public:
int findKthNum(int k)
{
set<int> bst;
bst.insert(1);
int curr;
for (int i = 0; i < k; ++i)
{
curr = *bst.begin();
@ryancooper
ryancooper / BinaryCode
Created October 1, 2014 08:27
TopCoder SRM 144 DIV 1 300-points
class BinaryCode {
int charToNum(char c) {
return c - '0';
}
String solve(String message, char[] P, char first) {
boolean isValid = true;
int N = P.length;
P[0] = first;
P[1] = (char) ('0' + charToNum(message.charAt(0)) - charToNum(P[0]));
moves = [int('011',base=8), int('0111', base=8), int('01110010', base=8), int('0100100',base=8), int('0101000',base=8), int('010001000',base=8), int('010100000',base=8), int('011000000',base=8)]
finalStates = [0, int('1'*8, base=8), int('2'*8, base=8), int('3'*8, base=8)]
TERMINAL = int('4'*8, base=8)
MASK = int('3'*8, base=8)
def code(c):
# Definition for a binary tree node
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
self.parent = None
def successor(node):
# return the tree node which is the successor of this node
@ryancooper
ryancooper / gist:fd37ad761c15da4c95d4
Created July 2, 2014 05:28
Stack sort in python(using system stack)
def stackInsert(stack, elem):
if len(stack) == 0 or stack[len(stack)-1] <= elem:
stack.append(elem)
else:
topElem = stack.pop()
stackInsert(stack, elem)
stack.append(topElem)
def stackSort(stack):
if len(stack) == 0:
def Hanoi(num, start, target, aux):
stack = []
t = (num, start, target, aux)
while t[0] != 0 or len(stack) > 0:
if t[0] != 0:
stack.append(t)
t = (t[0]-1, t[1], t[3], t[2])
else:
t = stack.pop()
print 'move the '+str(t[0])+'th plate from '+str(t[1])+' to '+str(t[2])