# HG changeset patch # User Eris Caffee # Date 1431795775 18000 # Node ID 17ee997bf1241cea4099a80394c2baaa645e88ed Initial commit diff -r 000000000000 -r 17ee997bf124 .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Sat May 16 12:02:55 2015 -0500 @@ -0,0 +1,4 @@ +algs4/algs4-data/.* +algs4/algs4-code/.* +.*~$ +.*\.class$ diff -r 000000000000 -r 17ee997bf124 algs4-c++/1.1.14/lg.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/algs4-c++/1.1.14/lg.cpp Sat May 16 12:02:55 2015 -0500 @@ -0,0 +1,21 @@ +// Write a static method lg() that takes an int value N as argument and returns the largest int not larger than the base-2 logarithm of N. Do not use Math. + +// g++ lg.cpp -o lg + +#include +#include +#include + +int main( int argc, char **argv ) { + + while ( ! std::cin.eof() ) { + int i; + std::cin >> i; + int lg = 0; + while ( (1 << lg) <= i ) { + lg++; + } + lg--; + std::cout << "lg(" << i << ") >= " << lg << " actual value is " << log2(i) << std::endl; + } + } diff -r 000000000000 -r 17ee997bf124 algs4-c++/1.1.9/int_to_binary.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/algs4-c++/1.1.9/int_to_binary.cpp Sat May 16 12:02:55 2015 -0500 @@ -0,0 +1,16 @@ +// g++ int_to_binary.cpp int_to_binary + +#include +#include + +int main( int argc, char **argv ) { + int num; + while ( ! std::cin.eof() ) { + std::cin >> num; + + for ( int i = (INT_MAX - INT_MAX/2); i > 0; i /= 2 ) { + std::cout << ( ( num & i ) ? 1 : 0 ); + } + std::cout << std::endl; + } + } diff -r 000000000000 -r 17ee997bf124 algs4-c++/BinarySearch/BinarySearch.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/algs4-c++/BinarySearch/BinarySearch.cpp Sat May 16 12:02:55 2015 -0500 @@ -0,0 +1,58 @@ +// g++ -D COMPILE_MAIN BinarySearch.cpp -o BinarySearch + +#include +#include +#include +#include + +namespace BinarySearch { + template + long rank( T key, std::vector a ) { + long lo = 0; + long hi = a.size() - 1; + while ( lo <= hi ) { + long mid = lo + ( hi - lo ) /2 ; + if ( a[mid] < key ) + lo = mid + 1; + else if ( a[mid] > key ) + hi = mid - 1; + else + return mid; + } + return -1; + } + }; + +//////////////////////////////////////////////////////////////////////////////// + +#ifdef COMPILE_MAIN + +int main( int argc, char **argv ) { + std::vector whitelist; + + std::fstream whitelist_file( argv[1] ); + while ( whitelist_file.good() ) { + int i; + whitelist_file >> i; + whitelist.push_back(i); + } + whitelist_file.close(); + + std::sort( whitelist.begin(), whitelist.end() ); + + std::cout << "Sorted whitelist" << std::endl; + for ( std::vector::iterator it = whitelist.begin(); it < whitelist.end(); ++it ) { + std::cout << *it << std::endl; + } + + while ( ! std::cin.eof() ) { + int key; + std::cin >> key; + if ( BinarySearch::rank( key, whitelist ) > -1 ) { + std::cout << key << " is in the whitelist" << std::endl; + } + } + + } + +#endif diff -r 000000000000 -r 17ee997bf124 algs4/1.1.19/Fibonacci.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/algs4/1.1.19/Fibonacci.java Sat May 16 12:02:55 2015 -0500 @@ -0,0 +1,12 @@ +public class Fibonacci { + public static long F( int N ) { + if ( N == 0 ) return 0; + if ( N == 1 ) return 1; + return F( N-1 ) + F( N-2 ); + } + + public static void main( String[] args ) { + for ( int N = 0; N < 100; N++ ) + StdOut.println( N + " " + F(N) ); + } + } \ No newline at end of file diff -r 000000000000 -r 17ee997bf124 algs4/1.1.19/Fibonacci_Memoized.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/algs4/1.1.19/Fibonacci_Memoized.java Sat May 16 12:02:55 2015 -0500 @@ -0,0 +1,16 @@ +public class Fibonacci_Memoized { + static long[] fibs = new long[100]; + + public static long F( int N ) { + if ( N == 0 ) return 0; + if ( N == 1 ) return 1; + if ( fibs[N] == 0 ) + fibs[N] = F( N-1 ) + F( N-2 ); + return fibs[N]; + } + + public static void main( String[] args ) { + for ( int N = 0; N < 100; N++ ) + StdOut.println( N + " " + F(N) ); + } + } \ No newline at end of file diff -r 000000000000 -r 17ee997bf124 algs4/1.1.20/LnFact.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/algs4/1.1.20/LnFact.java Sat May 16 12:02:55 2015 -0500 @@ -0,0 +1,25 @@ +public class LnFact { + public static double ln_fact ( int n ) { + if ( n < 1 ) + return -1; + if ( n == 1 ) + return 0; + return Math.log( n ) + ln_fact( n - 1 ); + } + + // public static double fact ( int n ) { + // if ( n < 1 ) + // return 0; + // if ( n == 1 ) + // return 1; + // return n * fact( n - 1 ); + // } + + public static void main( String[] args ) { + while ( ! StdIn.isEmpty() ) { + int n = StdIn.readInt(); + StdOut.println( ln_fact( n ) ); +// StdOut.println( Math.log( fact( n ) ) ); + } + } + } \ No newline at end of file diff -r 000000000000 -r 17ee997bf124 algs4/BinarySearch/BinarySearch.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/algs4/BinarySearch/BinarySearch.java Sat May 16 12:02:55 2015 -0500 @@ -0,0 +1,31 @@ +import java.util.Arrays; + +public class BinarySearch { + public static int rank( int key, int[] a ) { + int lo = 0; + int hi = a.length - 1; + while ( lo <= hi) { + int mid = lo + ( hi - lo ) / 2; + if ( key < a[mid] ) + hi = mid - 1; + else if ( key > a[mid] ) + lo = mid + 1; + else + return mid; + } + return -1; + } + + public static void main( String[] args ) { + int[] whitelist = In.readInts( args[0] ); + + Arrays.sort( whitelist ); + + while ( ! StdIn.isEmpty() ) { + int key = StdIn.readInt(); + if ( rank( key, whitelist ) >= 0 ) + StdOut.println( key ); + } + } + + } \ No newline at end of file diff -r 000000000000 -r 17ee997bf124 algs4/algs4-data.zip Binary file algs4/algs4-data.zip has changed diff -r 000000000000 -r 17ee997bf124 algs4/algs4.jar Binary file algs4/algs4.jar has changed diff -r 000000000000 -r 17ee997bf124 algs4/bin/drjava --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/algs4/bin/drjava Sat May 16 12:02:55 2015 -0500 @@ -0,0 +1,9 @@ +#!/bin/bash + +# ************************************************* +# Wrapper for using DrJava +# Last edited: September 18, 2011 +# ************************************************* + +jar=~/algs4/drjava.jar +java -jar ${jar} diff -r 000000000000 -r 17ee997bf124 algs4/bin/java-algs4 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/algs4/bin/java-algs4 Sat May 16 12:02:55 2015 -0500 @@ -0,0 +1,16 @@ +#!/bin/bash + +# ************************************************* +# java-algs4 +# Hayk Martirosyan +# ------------------- +# Wrapper for java that includes algs4 libraries. +# ************************************************* + +# This must match the install directory +INSTALL=~/algs4 + +# Sets the path to the classpath libraries +jars=(.:${INSTALL}/stdlib.jar:${INSTALL}/algs4.jar) + +java -cp "$jars" "$@" diff -r 000000000000 -r 17ee997bf124 algs4/bin/javac-algs4 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/algs4/bin/javac-algs4 Sat May 16 12:02:55 2015 -0500 @@ -0,0 +1,16 @@ +#!/bin/bash + +# ************************************************* +# javac-algs4 +# Hayk Martirosyan +# ------------------- +# Wrapper for javac that includes algs4 libraries. +# ************************************************* + +# This must match the install directory +INSTALL=~/algs4 + +# Sets the path to the classpath libraries +jars=(.:${INSTALL}/stdlib.jar:${INSTALL}/algs4.jar) + +javac -cp "$jars" -g -encoding UTF-8 "$@" diff -r 000000000000 -r 17ee997bf124 algs4/drjava.jar Binary file algs4/drjava.jar has changed diff -r 000000000000 -r 17ee997bf124 algs4/hello/HelloWorld.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/algs4/hello/HelloWorld.java Sat May 16 12:02:55 2015 -0500 @@ -0,0 +1,5 @@ +public class HelloWorld { + public static void main(String[] args) { + System.out.println("Hello, World"); + } + } diff -r 000000000000 -r 17ee997bf124 algs4/stdlib.jar Binary file algs4/stdlib.jar has changed