Skip to content

Instantly share code, notes, and snippets.

View rupalbarman's full-sized avatar

Rupal Barman rupalbarman

View GitHub Profile
@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 / 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 / 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 / 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 / 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]
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 / 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
@rupalbarman
rupalbarman / mat_layer_rot.py
Created May 30, 2017 05:18
matrix rotation (layer wise) anti clockwise by 90 degrees
a= [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]
]
for _ in a:
print(*_)
print()
ab= [x[::-1] for x in zip(*a)]
@rupalbarman
rupalbarman / mergeunique.cpp
Created June 1, 2017 10:07
Merge two arrays (sorted) with no repetitions of values
#include <iostream>
using namespace std;
void merge_em_all(int *a, int *b, int *c, int x, int y, int z) {
int i=0, j=0, k= 0;
for(i=0, j=0; i<x && j<y; ) {
if(a[i]< b[j]){
@rupalbarman
rupalbarman / bsearchqsort.cpp
Last active June 1, 2017 15:56
binary search and qsort in c++ (bsearch, qsort)
#include <iostream>
#include <string.h>
using namespace std;
int compare(const void *a, const void *b){
return (*(int *)a- *(int*)b);
}
int str_compare(const void *a, const void *b) {