Skip to content

Instantly share code, notes, and snippets.

View rupalbarman's full-sized avatar

Rupal Barman rupalbarman

View GitHub Profile
@rupalbarman
rupalbarman / JDBCTest.java
Created May 30, 2017 03:53
JDBC Java test example
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.PreparedStatement;
/*
NOTES
-JDBC API is available in two packages, java.sql and javax.sql
import java.util.ArrayList;
class Employee {
private String name;
private int empID;
private int yearsOfService;
private int salary;
private ArrayList<Certificate> certificates;
public Employee(String name, int empID, int yearsOfService, int salary) {
@rupalbarman
rupalbarman / three_sum.py
Created April 30, 2017 02:52
Three sum problem in python. Find three numbers from an array whose sum is x
'''
three sum, to find a+b+c== x in an array
'''
a= [-1,0,1,2,-1,-4] #the array/ list
x= 0 #the sum we need
s= set()
res= [] #holds groups of 3 numbers satisfying the condition
for i in range(len(a)-1):
ax= a[i]
@rupalbarman
rupalbarman / py_advanced.py
Created April 10, 2017 15:51
Collections of python snippets (Matrix rotation, Prime Numbers)
import itertools
def pretty(a):
for i in a:
print(i)
print()
original= [[1,2], [3,4]]
pretty(original)
pretty(original[::-1])
@rupalbarman
rupalbarman / sorter.go
Created April 5, 2017 02:48
Sorting methods in GoLang
package main
import (
"fmt"
"sort"
)
func main() {
a:= []int{1,2,3,9,5,6}
fmt.Println(a)
@rupalbarman
rupalbarman / code_snip.go
Created April 3, 2017 15:59
Helpful code snippets in GoLang for competitive coding
package main
import (
"fmt"
"index/suffixarray"
)
func main() {
//readn_arr()
suffixarray_usage()
@rupalbarman
rupalbarman / huffman_encode.py
Created April 3, 2017 12:56
Huffman encoding of strings but without TREES!
from collections import Counter
s= input()
c= Counter(s)
cc= sorted(c.items(), key= lambda x: x[1], reverse= True)
huff_map= {}
a, b= '0', '1'
for x in range(len(cc)-1):
huff_map[cc[x][0]]= a*x + b #assigns 0, 01, 001 to n-1 chars
huff_map[cc[x+1][0]]= a* (len(cc)-1) #assigns 000..0 to nth char
print(huff_map) #prints the huffman mapping
@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);
@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 / 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))