Skip to content

Instantly share code, notes, and snippets.

View beingmartinbmc's full-sized avatar
🏠
Working from home

Ankit Sharma beingmartinbmc

🏠
Working from home
  • Games24x7
  • Bengaluru
  • 07:28 (UTC -12:00)
View GitHub Profile
@beingmartinbmc
beingmartinbmc / Fibonacci.java
Last active February 24, 2019 15:55
Fibonacci using dijkstra's formula. O(log n) using memoisation
private static HashMap<Integer, Integer> lookup = new HashMap<>();
private static int fib(int n){
if(lookup.containsKey(n))
return lookup.get(n);
else{
if(n <= 1)
lookup.put(n, n);
else{
int k = (n & 1) == 1? (n+1)/2 : n/2;
if((n&1) == 1)
@beingmartinbmc
beingmartinbmc / Traversals.java
Created October 19, 2018 16:46
BFS and DFS in Java.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class Traversals {
private ArrayList<Integer> G[];
private boolean visited[];
@beingmartinbmc
beingmartinbmc / BFS.java
Last active October 19, 2018 16:08
Java implementation of BFS as per as CLRS
import java.util.*;
public class BFS{
private Vector<Integer> distance;
private Vector<Integer> parent;
private Vector<String> colour;
private Vector<Integer> g[];
private int vertexCount;
private HashSet<Integer> vertices;
@beingmartinbmc
beingmartinbmc / DFS.java
Created October 19, 2018 16:06
Java implementation of DFS algorithm as per CLRS
import java.util.Collections;
import java.util.HashSet;
import java.util.Vector;
public class DFS {
private Vector<Integer> discoveredTime;
private Vector<Integer> finishedTime;
private Vector<Integer> parent;
private Vector<String> colour;
import java.util.Stack;
public class SortStack {
private static Stack<Integer> sortStack(Stack<Integer> stack){
Stack<Integer> helperStack = new Stack<>();
while(!stack.isEmpty()){
int current = stack.pop();
while(!helperStack.isEmpty() && current < helperStack.peek())
@beingmartinbmc
beingmartinbmc / BalancedParentheses.java
Last active April 21, 2019 06:48
Checks if an expression is balanced.
import java.util.Stack;
public class BalancedParantheses {
private boolean isOpeningSymbol(char a){
return a == '(' || a == '{' || a == '[';
}
private char getClosingSymbol(char a){
if(a == '(') return ')';
if(a == '{') return '}';
@beingmartinbmc
beingmartinbmc / Luhn_check.py
Created September 20, 2018 08:21
Luhn check algorithm to validate credit card numbers. Implemented using basic integer atomic operations.
def luhn(a):
def get_odd(a):
x = 0
while a > 0:
s = a % 10
s = 2*s
if s > 9:
s = (s-1) % 9 + 1
x += s
a //= 100