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
  • 00:52 (UTC -12:00)
View GitHub Profile
@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
@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 '}';
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 / 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;
@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 / 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 / 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 / BST.c
Created November 24, 2018 19:41
Binary search tree implementation
#include "stdio.h"
#include "stdlib.h"
#define pf printf
#define sf scanf
typedef struct tree
{
int data;
struct tree *left,*right;
}node,*nodeptr;
nodeptr tree=NULL;
@beingmartinbmc
beingmartinbmc / Stack.py
Created December 11, 2018 04:53
Implementation of stack in Python
__author__ = 'Ankit Sharma'
class Stack:
""" This is an easy implementation of a stack """
def __init__(self):
self.stack = []
def push(self, some_value):
@beingmartinbmc
beingmartinbmc / NQueen.py
Last active December 14, 2018 10:58
N-Queen problem implementation based on @Tushar_Roy video
n = (int(input('Enter number of queens\n')))
board = [[0] * n for i in range(n)]
def is_safe(row, col):
for i in range(n):
if board[row][i] == 1 or board[i][col] == 1:
return False
for i in range(n):
for j in range(n):