Skip to content

Instantly share code, notes, and snippets.

View rupalbarman's full-sized avatar

Rupal Barman rupalbarman

View GitHub Profile
@rupalbarman
rupalbarman / rotateArrXor.c
Last active April 9, 2021 17:19
Rotate an Array using XOR operations
/* The code allows rotation of an 1D array (right and left)
using XOR operator
ie. Shifts elements of an Array to left or right directions,
wraps the elements circularly.
*/
#include <stdio.h>
int arr[]={1,2,3,4,5,6,7,8,9,10};
@rupalbarman
rupalbarman / InterCodeGen.cpp
Created December 9, 2016 03:13
Intermediate code generator ( Three address code) simple
//SIMPLE INTERMEDIATE CODE GENERATOR (3 ADDRESS CODE)
//Author: Rupal Barman
//Modified: 5/11/16
// Does not consider the operator precedence, ie. generates the 3 address code from left to right directly.
// INPUT example: t = q * p + g ; (don't forget to end the statement with a ';' or any character, even a space would work)
// OUTPUT example: v=r*j; w=v+h
#include <iostream>
#include <stdio.h>
#include <string.h>
@rupalbarman
rupalbarman / bst.py
Last active February 25, 2017 13:28
Binary Search Tree in Python
'''
Binary Searc Tree Simple
Rupal Barman
[email protected]
This is a demonstration of implementing BST in python.
Since, it's basic. Feel free to add in more features.
'''
class Node(object):
@rupalbarman
rupalbarman / bst.go
Last active February 25, 2017 13:51
Binary Search Tree golang
/*
Binary Search Tree GoLang
Rupal barman
[email protected]
*/
package main
import (
"fmt"
@rupalbarman
rupalbarman / adjacencyMat_to_List.py
Created March 19, 2017 03:19
Utility to convert adjacency matrix into adjacency list in python, for efficient BFS and DFS maybe
# get adjancy matrix
n= int(input()) #number of vertices
am=[]
for i in range(n):
am.append([int(x) for x in input().split()])
print(am) #show matrix
print()
#conver to adjancy list
@rupalbarman
rupalbarman / subset_having_sum.py
Created March 26, 2017 15:05
get subsets of two elements from a list/ array whose sum is given
def get_pair_with_sum(a, need):
s= set()
for i in a:
if (need- i) in s:
print(need-i, i)
else:
s.add(i)
a=[4,2,4,2]
b=[3,6,2,5]
@rupalbarman
rupalbarman / bin_packing_nextfit_simple.py
Created March 27, 2017 14:03
Simple Bin Packing Algorithm code which uses Next-fit technique
# bin packing algo (estimation)
# since it is Np- Hard
# for better methods (first fit and best fit) use self balancing trees
# this one is Next fit
def bins_needed(item_weights, bin_capacity):
remain= bin_capacity
bins=0
for i in item_weights:
if i> bin_capacity:
@rupalbarman
rupalbarman / python-one-liners.py
Created March 29, 2017 05:57
Basic one-liners in python 3 for reference
#generate all subsets of a sequence
f = lambda x: [[y for j, y in enumerate(set(x)) if (i >> j) & 1] for i in range(2**len(set(x)))]
print(f([1,2,3]))
#print((2>>1)&1) #1010-> 0101
# max() using lambda
maxx= lambda x,y: y if y>x else x
print(maxx(23,12))
@rupalbarman
rupalbarman / anagram-palindrome-counter.py
Last active March 29, 2017 17:10
Working on Counters (for reference)
'''
Alice is taking a cryptography class and finding anagrams to be very useful. We consider two strings to be anagrams of each other if the first string's letters can be rearranged to form the second string. In other words, both strings must contain the same exact letters in the same exact frequency For example, bacdc and dcbac are anagrams, but bacdc and dcbad are not.
Alice decides on an encryption scheme involving two large strings where encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. Can you help her find this number?
Given two strings, and , that may or may not be of the same length, determine the minimum number of character deletions required to make and anagrams. Any characters can be deleted from either of the strings.
'''
from collections import Counter
c1 = Counter('baaaaccd')
c2 = Counter('abbaa')
@rupalbarman
rupalbarman / set_difference.cpp
Created April 2, 2017 04:57
set operations in c++
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int a[]= {1, 2, 90, 43, 23};
int b[]= {1, 90, 2, 43, 99};
vector<int> v(10);