Skip to content

Instantly share code, notes, and snippets.

View anupsavvy's full-sized avatar

Anup anupsavvy

  • Northwestern University
  • Chicago
View GitHub Profile
@anupsavvy
anupsavvy / sdword2vec.py
Created October 9, 2015 16:39
Load science direct articles and create doc2vec vectors for each one of them.
#coding:utf-8
# Author: Anup Sawant
# Purpose: Doc2vec vectors of Science Direct Articles
# Created: 9/21/2015
import sys
import os
import pandas as pd
from pandas import Series, DataFrame
@anupsavvy
anupsavvy / 19.json
Last active August 29, 2015 13:57
Stacked bar-charts on time scale.
[
[
{
"time": "0",
"y": 0
},
{
"time": "1",
"y": 0
},
@anupsavvy
anupsavvy / gist:6983050
Created October 14, 2013 22:08
1) Find the maximum sum possible from picking a contiguous subsequence of an array. [-1, 5, 6, -2, 20, -50, 4] What is the largest sum of contiguous elements available in this list?
public int largestSum(int[] A){
if(A.length == 0) return 0;
if(A.length == 1) return A[0];
int max = Integer.MIN_VALUE;
int curr = 0;
for(int i = 0; i < A.length; i++){
@anupsavvy
anupsavvy / Search.java
Created February 20, 2013 19:42
Given a sorted array of integers array and an integer key, return the index of the first instance of key in the array. If key is not present in array, you should return -1. For example, given the array [-10, -2, 1, 5, 5, 8, 20] and key 5, you should return the index 3. Your solution should be a binary search, that is, it should run in O(log n) t…
public class Search{
private int binarySearch(int arr[], int key){
if(arr == null)
throw new RuntimeException("Null array found");
int l = 0; // lower limit
int h = arr.length - 1; // upper limit
int m;
@anupsavvy
anupsavvy / DataSource.java
Last active December 14, 2015 00:19
Singleton Class
public class DataSource {
private Connection connection = null;
private static DataSource datasource = null;
// private constructor
private DataSource(){
connection = getConnection();
}
// method to get an instance.
@anupsavvy
anupsavvy / Puzzle.java
Created July 29, 2011 04:01
Write a method to sort an array of strings so that all the anagrams are next to each other.
public class Puzzle{
public Puzzle(){
//TODO
}
public static String[] sortStrings(String[] arr){
if(arr == null || arr.length == 0 || arr.length == 1)
return arr;
@anupsavvy
anupsavvy / Puzzle.java
Created July 27, 2011 04:55
Write a method to decide if two strings are anagrams are not.
public class Puzzle{
public static boolean findAnagrams(String s1, String s2){
if(s1 == null && s2!=null)
return false;
if(s1!=null && s2==null)
return false;
if(s1==null && s2==null)
return true;
if(s1.length()!=s2.length())
@anupsavvy
anupsavvy / Puzzle.java
Created July 27, 2011 03:49
Write a method to shuffle a deck of cards. It must be a perfect shuffle – in other words, each 52! permutations of the deck has to be equally likely. Assume that you are given a random number generator which is perfect.
public class Puzzle {
public static int[] shuffleCards(int[] cards){
if(cards == null || cards.length==1 || cards.length==0){
return cards;
}
int cardNumber,hold;
for(int i =0; i < cards.length; i++){
cardNumber = (int)(Math.random()*(cards.length-i)) + 1;
hold = cards[cardNumber];
cards[cardNumber] = cards[i];
@anupsavvy
anupsavvy / Search.java
Created July 26, 2011 04:30
Write a program for finding the kth - smallest element in the array x[0..n-1] in O(n) expected time. Your algorithm may permute the elements of x.
package com.operations.search;
public class Search{
private int[] arr=null;
private int k=0;
public Search(int[] arr){
this.arr = arr;
if(this.arr == null)
throw new SearchException("Cannot accept null array");
@anupsavvy
anupsavvy / Sort.java
Created July 25, 2011 23:45
Simple Quicksort ( this is not a randomized version ).
public class Sort{
private int[] arr = null;
public Sort(int[] arr) throws SortException{
this.arr = arr;
if(this.arr == null)
throw new SortException("Null array found");
}