Skip to content

Instantly share code, notes, and snippets.

// Pointers
int a = 3, b = 4;
int c = 1, d = 2;
int* pa = &a;
int e = *pa;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
// The following are required for implementing the Iterable and Iterator interfaces.
import java.lang.Iterable;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class SeekableList<T> {
public class SynchronizedCounter {
private int c = 0;
public synchronized void increment() {
c++;
}
public synchronized void decrement() {
c--;
}
public void updateWordCount(HashMap<String, Integer> counts, String wordToUpdate){
if (counts.containsKey(wordToUpdate)) {
int current_count = counts.get(wordToUpdate);
counts.put(wordToUpdate, current_count++);
} else {
counts.put(wordToUpdate, 1);
}
}
@spitz-dan-l
spitz-dan-l / gist:77968e6177ad483741ad
Created December 9, 2014 21:07
Loading a mysql table into pandas
# If you are using anaconda, then you need to install an extra package which is used behind the scenes.
# Run the following command on the command line:
#
# $ conda install pymysql
#
# say yes when it prompts you.
import pandas
from sqlalchemy import create_engine
@spitz-dan-l
spitz-dan-l / gist:f494674b855eeea9f998
Created November 18, 2014 22:58
One-hot encoding demo
from sklearn.feature_extraction import DictVectorizer
import pandas
def demo1(input_csv, new_csv):
"""Demonstration of one-hot encoding through Scikit-Learn's DictVectorizer object."""
df = pandas.read_csv(input_csv) #read csv into a DataFrame object
#create the one hot encoder
one_hot_encoder = DictVectorizer(sparse=False)
@spitz-dan-l
spitz-dan-l / gist:01a8e7788ffaeec87fd7
Created September 3, 2014 17:19
Pandas panel not adding column
import numpy as np
import pandas as ps
np.random.seed(0)
index=range(3)
columns = list('abc')
panel = ps.Panel.from_dict(
{'A' : ps.DataFrame(np.random.randn(3, 3), index=index, columns=columns),
@spitz-dan-l
spitz-dan-l / gist:c8f69e4a045f6f57f2f0
Last active August 29, 2015 14:04
Python List Monad with Coroutine Copying
# how to get python to look like this haskell?..
# listOfTuples :: [(Int,Char)]
# listOfTuples = do
# n <- [1,2]
# ch <- ['a','b']
# return (n,ch)
# -> [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]
# ..such that we can write a coroutine and apply a decorator that will make
@list_monad
dept_prov_apt_types = DepartmentProviderAppointmentType.where("department_provider_id in (?)", @department_providers.map(&:id))
@apt_types = AppointmentType.where("id in (?)", dept_prov_apt_types.map(&:appointment_type_id)).distinct.order(:name)
def remove_spans_neg(s, spans):
return ''.join(s[i:j] for (_, i), (j, _) in zip([(None,0)]+spans, spans+[(len(s),None)]))