Skip to content

Instantly share code, notes, and snippets.

View lobster1234's full-sized avatar
🎯
Focusing

Manish Pandit lobster1234

🎯
Focusing
View GitHub Profile
@lobster1234
lobster1234 / ArraySumWithMap.java
Created March 9, 2013 03:56
Check if a collection contains 2 numbers that add up to the input. This uses a hashmap so that the computational complexity is constant.
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class ArraySumWithMap {
private Map<Integer, Integer> numbers = new HashMap<Integer, Integer>();
/**
@lobster1234
lobster1234 / WaitNotifyExample..java
Created March 6, 2013 08:05
A very simple wait/notify example
public class WaitNotifyExample {
public static void main(String[] args) {
Integer lock = new Integer(0);
Thread waiter = new Thread(new Waiter(lock));
waiter.start();
Thread notifier = new Thread(new Notifier(lock));
notifier.start();
}
@lobster1234
lobster1234 / ArraySum.java
Last active December 14, 2015 12:48
Test if 2 numbers contained in an array add up to a given number. The time complexity is n(n-1)/2. Space complexity is constant.
public class ArraySum {
private int[] array = new int[]{1,2,3,4,5,6,7,8,9};
/**
* Do any 2 add up to this number
* @param number
* @return
*/
public boolean canAdd(int number){
@lobster1234
lobster1234 / ProducerConsumerInefficient.java
Last active December 14, 2015 12:39
Producer Consumer - Inefficient (sleep) implementation. Prone to deadlocks.
import java.util.Stack;
public class Bucket {
private Stack<Long> stack = new Stack<Long>();
private int MAX_SIZE = 20;
public Long consume(){
@lobster1234
lobster1234 / ConcurrentModificationExceptionGenerator.java
Created March 4, 2013 23:41
ConcurrentModificationException - How to get one.
public class Map {
public static void main(String[] args) {
HashMap<Integer,Long> map = new HashMap<Integer,Long>();
for(int i = 0;i<100;i++){
map.put(i,System.nanoTime());
}
Iterator<Integer> it = map.keySet().iterator();
while(it.hasNext()){
map.remove(it.next());
@lobster1234
lobster1234 / LinkedList.java
Last active October 12, 2021 22:24
A generic, singly LinkedList Implementation
public class LinkedList<T> {
private LinkedListNode<T> first = null;
/**
* Insert at the front of the list
* @param node
*/
public void insert(LinkedListNode<T> node) {
node.setNext(first);
@lobster1234
lobster1234 / ReverseString.scala
Last active February 21, 2018 22:37
Reverse a String - Scala
def reverseString(s:String) = (for(i<-s.length-1 to 0 by -1) yield s(i)).mkString
//Java Version here:
public class Test {
public final String reverse(String s) {
StringBuffer b = new StringBuffer(s.length());
for(int i = s.length()-1; i >=0; i--){
b.append(s.charAt(i));
@lobster1234
lobster1234 / PalindromeCheck.java
Last active December 14, 2015 05:39
Test for a Palindrome...
public class PalindromeCheck {
public final boolean isPalindrome(String s) {
boolean isPalindrome = true;
for (int i = 0; i < s.length(); i++) {
if (!(s.charAt(i) == s.charAt(s.length() - i - 1))) {
isPalindrome = false;
break;
}
}
@lobster1234
lobster1234 / TrafficManager.java
Created September 17, 2012 20:35
Oauth2 Authorization with Google APIs - A quick verification script
package com.ign.traffic;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
@lobster1234
lobster1234 / redis
Created February 16, 2012 22:44
Startup Script for Redis
#! /bin/sh
### BEGIN INIT INFO
# Provides: redis-server
# Required-Start: $syslog
# Required-Stop: $syslog
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: redis-server - Persistent key-value db