Skip to content

Instantly share code, notes, and snippets.

View paullewallencom's full-sized avatar
🎯
Focusing

Paul Lewallen paullewallencom

🎯
Focusing
View GitHub Profile
@paullewallencom
paullewallencom / Cat.java
Created July 24, 2018 22:44
Concatenates text files and outputs the result to a file.
public class Cat
{
private Cat() { }
public static void main( String[] args )
{
Out out = new Out( args[ args.length - 1 ] );
for ( int i = 0; i < args.length - 1; i++ )
{
@paullewallencom
paullewallencom / Knuth.java
Created July 24, 2018 23:20
Reads a list of strings & prints them in random order.
public class Knuth
{
private Knuth() { }
public static void shuffle( Object[] a )
{
int n = a.length;
for ( int i = 0; i < n; i++ )
{
@paullewallencom
paullewallencom / In.java
Created July 25, 2018 00:23
Reads Data of Various Types
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.Socket;
// import java.net.HttpURLConnection;
import java.net.URLConnection;
import java.util.ArrayList;
@paullewallencom
paullewallencom / Out.java
Created July 25, 2018 00:51
Writes Data of Various Types
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Locale;
public class Out
{
@paullewallencom
paullewallencom / StdIn.java
Created July 26, 2018 00:42
Reads Data of Various Types from Standard Input
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.regex.Pattern;
public final class StdIn
{
private static final String CHARSET_NAME = "UTF-8";
@paullewallencom
paullewallencom / StdOut.java
Last active July 26, 2018 00:58
Writes Data of Various Types to Standard Output
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
public final class StdOut
{
private static final String CHARSET_NAME = "UTF-8";
private static final Locale LOCALE = Locale.US;
private static PrintWriter out;
@paullewallencom
paullewallencom / UF.java
Created July 26, 2018 03:17
Weighted Quick-Union
public class UF
{
private int[] parent; // parent[i] = parent of i
private byte[] rank; // rank[i] = rank of subtree rooted at i (never more than 31)
private int count; // number of components
public UF( int n )
{
if ( n < 0 ) throw new IllegalArgumentException();
@paullewallencom
paullewallencom / QuickUnionUF.java
Created July 26, 2018 03:27
Quick-Union Algorithm
public class QuickUnionUF
{
private int[] parent; // parent[i] = parent of i
private int count; // number of components
public QuickUnionUF( int n )
{
parent = new int[ n ];
count = n;
@paullewallencom
paullewallencom / QuickFindUF.java
Created July 26, 2018 03:44
Quick-Find Algorithm
public class QuickFindUF
{
private int[] id; // id[i] = component identifier of i
private int count; // number of components
public QuickFindUF( int n )
{
count = n;
id = new int[ n ];
@paullewallencom
paullewallencom / WeightedQuickUnionUF.java
Created July 26, 2018 03:54
Weighted Quick-Union No Path Compression
public class WeightedQuickUnionUF
{
private int[] parent; // parent[i] = parent of i
private int[] size; // size[i] = number of sites in subtree rooted at i
private int count; // number of components
public WeightedQuickUnionUF( int n )
{
count = n;
parent = new int[ n ];