Skip to content

Instantly share code, notes, and snippets.

@gabhi
gabhi / gist:9959221
Created April 3, 2014 17:47
Sample test case to test POST method for express js REST API
//Following test case assumes you have express js server running at port 5000 and has api /api/login
var should = require('should');
var assert = require('assert');
var request = require('supertest');
var winston = require('winston');
describe('Routing', function() {
var url = 'http://localhost:5000';
@gabhi
gabhi / gist:9965888
Created April 4, 2014 00:48
slice array elements in forward direction
public static void arraySlicing(int[] inputArr) {
List<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i <= inputArr.length; i++) {
for (int j = i + 1; j <= inputArr.length; j++) {
System.out.println(Arrays.toString(Arrays.copyOfRange(inputArr, i, j)));
}
}
}
@gabhi
gabhi / gist:9985846
Last active August 29, 2015 13:58
reverse linked list
private static ListNode reverseList(ListNode head) {
ListNode current = head;
if (current == null)
return null;
if (current.next == null)
return current;
ListNode nextItem = current.next;
current.next = null;
@gabhi
gabhi / gist:9988442
Created April 5, 2014 07:13
How to create ArrayList (ArrayList<T>) from array (T[])
new ArrayList<Element>(Arrays.asList(array))
@gabhi
gabhi / gist:9988481
Created April 5, 2014 07:18
In Java, how can I test if an Array contains a certain value?
String[] VALUES = new String[] {"AB","BC","CD","AE"};
String value= "AB";
String notExists = "abc";
Arrays.asList(VALUES).contains(value); //TRUE
Arrays.asList(VALUES).contains(notExists); //FALSE
@gabhi
gabhi / gist:10442618
Created April 11, 2014 05:41
linked list implementation
public class LinkedList {
private ListNode firstNode;
private ListNode lastNode;
private int size;
/**
* For the no-args constructor, the data and next will be null (empty list)
*/
public LinkedList() {
@gabhi
gabhi / gist:10443043
Last active August 29, 2015 13:59
String Permutations
permuteString("", "ABCD");
//if endString.lenth <=1 then print
//for each character in endString
//Remove ith character
//permute i, remaining string
public static void permuteString(String beginningString, String endingString) {
if (endingString.length() <= 1)
@gabhi
gabhi / gist:10489541
Created April 11, 2014 18:18
Longest Compound word
private boolean isCompoundWord(String word) {
if (dictionary.contains(word)) return true;
for (int i = 1; i < word.length(); i++) {
String prefix = word.substring(0, i);
if (isCompoundWord(prefix)) {
String remainder = word.substring(i, word.length());
if (remainder.length() == 0) return true;
return isCompoundWord(remainder);
}
}
@gabhi
gabhi / gist:10489702
Created April 11, 2014 18:21
Powerset - subsets of set
public static <T> Set<Set<T>> powerSet(Set<T> originalSet) {
Set<Set<T>> sets = new HashSet<Set<T>>();
if (originalSet.isEmpty()) {
sets.add(new HashSet<T>());
return sets;
}
List<T> list = new ArrayList<T>(originalSet);
T head = list.get(0);
Set<T> rest = new HashSet<T>(list.subList(1, list.size()));
for (Set<T> set : powerSet(rest)) {
@gabhi
gabhi / gist:10489904
Created April 11, 2014 18:24
Right sibling
if (node->parent && node->parent->right) {
return node->parent->right;
} else {
return NULL;
}