Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
kartikkukreja / NextHigherNumberContainingEqualSetBits.cpp
Last active January 30, 2016 04:46
Next higher number containing equal set bits
#include <iostream>
#include <bitset>
using namespace std;
// used char for illustration, use int in practice
typedef unsigned char uint;
const unsigned int SIZE = sizeof(uint) * 8;
void printBitRepresentation(uint x) {
cout << bitset<SIZE>(x) << endl;
@kartikkukreja
kartikkukreja / MonteCarloSearch.py
Created December 6, 2015 12:35
Monte Carlo Search
MAX_DEPTH = 5 # the fixed maximum depth for game tree expansion
MAX_PROBES = 10 # the fixed maximum number of MCS probes for a non-terminal state
import random
def value(game, state, role, depth):
"""
returns the estimated value of the given state for the given role by
expanding the search tree until MAX_DEPTH has been reached and then
using MCS for non-terminal states' value estimation.
This method can be used to select the best action for the given role by
@kartikkukreja
kartikkukreja / LinearCombinationHeuristic.py
Created November 22, 2015 12:35
Linear Combination Heuristic
class LinearCombinationHeuristic:
def __init__(self, game, role, weights, MAX_POSSIBLE_ACTIONS, MAX_POSSIBLE_STATES):
self.game = game
self.role = role
self.weights = weights
self.MAX_POSSIBLE_ACTIONS = MAX_POSSIBLE_ACTIONS
self.MAX_POSSIBLE_STATES = MAX_POSSIBLE_STATES
self.heuristics = [actionMobility, stateMobility, actionFocus, stateFocus, goalValue]
assert(len(self.heuristics) == len(self.weights))
@kartikkukreja
kartikkukreja / GameInterface.py
Last active December 6, 2015 12:28
Game Interface
class Game:
def getStartState(self):
# return the start state
pass
def getNextState(self, currentState, jointMove):
# return the state achieved by playing jointMove in currentState
pass
def getRoles(self):
@kartikkukreja
kartikkukreja / twoSumBst.py
Last active September 15, 2015 03:59
2Sum problem in BST
def twoSum(bst, k):
if bst is None: return None
bstStart = inorderFromStart(bst)
bstEnd = inorderFromEnd(bst)
start, end = bstStart.next(), bstEnd.next()
while start != end:
if start.data + end.data == k: return (start.data, end.data)
elif start.data + end.data > k: end = bstEnd.next()
else: start = bstStart.next()
return None
@kartikkukreja
kartikkukreja / InorderFromEnd.py
Last active September 15, 2015 04:00
Inorder traversal from end
def inorderFromEnd(bst):
stack = [bst]
while bst.right is not None:
bst = bst.right
stack.append(bst)
while stack:
top = stack.pop()
if top.left is not None:
bst = top.left
stack.append(bst)
@kartikkukreja
kartikkukreja / InorderFromStart.py
Last active September 15, 2015 04:00
Inorder traversal from start
def inorderFromStart(bst):
stack = [bst]
while bst.left is not None:
bst = bst.left
stack.append(bst)
while stack:
top = stack.pop()
if top.right is not None:
bst = top.right
stack.append(bst)
@kartikkukreja
kartikkukreja / BSTNode.py
Last active September 15, 2015 04:00
BST Node
class BST:
def __init__(self, data, left, right):
self.data = data
self.left = left
self.right = right
@kartikkukreja
kartikkukreja / 2SumSortedArray.py
Last active September 15, 2015 04:02
2Sum in sorted array
def twoSumSortedArray(A, k):
"""
Given a sorted integer array A, return two elements from it which sum to k
"""
start, end = 0, len(A) - 1
while start != end:
if A[start] + A[end] == k: return (A[start], A[end])
elif A[start] + A[end] > k: end -= 1
else: start += 1
return None
@kartikkukreja
kartikkukreja / Expectimax Search.py
Created August 8, 2015 17:47
Expectimax Search
def value(problem, state):
if problem.isTerminalState(state): return problem.getTerminalUtility(state)
if state is a MAX node: return maxValue(problem, state)
if state is a MIN node: return minValue(problem, state)
if state is an EXP node: return expValue(problem, state)
def expValue(problem, state):
v = 0
for successor in problem.getSuccessors(state):
v += problem.getProbability(state, successor) * value(problem, successor)