Skip to content

Instantly share code, notes, and snippets.

@amadamala
amadamala / HashTable.java
Last active June 28, 2022 03:55
HashTable implementation in Java
public class HashTable {
private static int INITIAL_SIZE = 16;
private HashEntry[] entries = new HashEntry[INITIAL_SIZE];
public void put(String key, String value) {
int hash = getHash(key);
final HashEntry hashEntry = new HashEntry(key, value);
if(entries[hash] == null) {
entries[hash] = hashEntry;
}
// If there is already an entry at current hash
@amadamala
amadamala / VendorUtils.java
Created June 7, 2012 19:41
String concatination using var args.
public static String concat(String arg, String... args) {
StringBuilder builder = new StringBuilder(arg);
for(String s:args){
builder.append(s);
}
return builder.toString();
}
@amadamala
amadamala / hack.sh
Created April 1, 2012 06:38 — forked from erikh/hack.sh
OSX For Hackers
#!/usr/bin/env sh
##
# This is script with usefull tips taken from:
# https://github.com/mathiasbynens/dotfiles/blob/master/.osx
#
# install it:
# curl -sL https://raw.github.com/gist/2108403/hack.sh | sh
#
@amadamala
amadamala / WordTotal100.java
Created December 28, 2011 10:50
Java code to get all words with character sum = 100 in a dictionary
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class WordTotal100 {
public static void main(String[] args) {
// read big.txt file
try {
String fileStr = readFileAsString("words.txt");
String[] words = fileStr.split("\\r?\\n");
@amadamala
amadamala / gist:1010771
Created June 6, 2011 18:25
Google from Terminal
# Add following snippet to your ".bash_profile" to google from the terminal.
# google from Terminal
google() {
python -c "import sys, webbrowser, urllib; webbrowser.open('http://www.google.com/search?' + urllib.urlencode({'q': ' '.join(sys.argv[1:]) }))" $@
}
# Usage: $ google recursion
@amadamala
amadamala / gist:976237
Created May 17, 2011 10:15
Viewing man pages as pdf
# Add the following code to your ~/.bash_profile.
pman()
{
PMANFILE="/tmp/pman-${1}.pdf"
if [ ! -e $PMANFILE ]; then # only create if it doesn't already exist
man -t "${1}" | pstopdf -i -o "$PMANFILE"
fi
if [ -e $PMANFILE ]; then # in case create failed
open -a /Applications/Preview.app/ "$PMANFILE"
fi
@amadamala
amadamala / collatz_sequences.py
Created April 29, 2011 14:11
Collatz sequence
N = 1001
def collatz_sequences(n):
while n > 1:
print n
if (n % 2) != 0:
n = (3 * n) + 1
else:
n = n / 2
print n
@amadamala
amadamala / DeckOfCards.java
Created March 21, 2011 17:54
Design a class to represent a deck of cards, and implement a shuffle method for the deck of cards.
/* Design a class to represent a deck of cards, and implement a
shuffle method for the deck of cards. http://goo.gl/T4Bk9 */
import java.util.Arrays;
import java.util.Random;
public class DeckOfCards {
public static String deck = "23456789TJQKA";
public static int numOfShuffles = 13;
public static void main(String[] args) {
import java.util.Arrays;
public class SumOfThreads implements Runnable {
static int sum[] = new int[3];
static int idx = 0;
int n1, n2;
SumOfThreads(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
@amadamala
amadamala / Power2.java
Created March 17, 2011 06:27
Read this problem somewhere on the internet. Java solution is here.
import java.util.Arrays;
public class Power2 {
static int resultLen = 5000;
public static void main(String[] args) {
Power2 power = new Power2();
String s = power.power(10000);
System.out.println(s);
}
String power(int n) {