Here are some more tips for anyone who might have encountered these problems.
Edit: I just found that most of the information in this post is actually at the bottom of this page. I should have scrolled down below the list of programs.
Finding the Supplemental Files
I’m reading 1.4 and 1.5 in the textbook.
Update: You can download the data files here:
http://algs4.cs.princeton.edu/code/algs4-data.zip
If anyone is interested in that problem, it’s on page 173 of the textbook. The goal is to write a program that reads through the list of 1 million integers and find triples that sum to 0. I’ll post his example of an inefficient solution below.
Running Java Code without an IDE
Intellij was too much for me to deal with. I finally figured out how to edit the Java code with vim. These instructions should work for any code editor, include vscode.
Create a text file, ThreeSum.java
with the following contents (see p. 173 of the textbook). All I’ve added are the two import statements on the top. I’ll explain those after the code snippet.
// From the sample code, `In` and `StdOut` were not built-in, so I
// imported them from the book's library.
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
public class ThreeSum {
public static int count(int[] a) {
// count triples that sum to 0
int n = a.length;
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
if (a[i] + a[j] + a[k] == 0) {
count++;
}
}
}
}
return count;
}
public static void main(String[] args) {
In in = new In(args[0]);
int[] a = in.readAllInts();
StdOut.println(count(a));
}
}
I downloaded the course’s required algs4.jar
file from this page and put it in a directory at ~/algs4/
. Then I followed the idea from this post to add it to my CLASSPATH
in my ~/.zshenv
file. (It might be ~/.bashrc
or ~/.bash_profile
depending on your shell and OS. Leave a comment below if you don’t know which file to use.)
It looks like this in my .zshenv
file:
export CLASSPATH=$HOME/algs4/algs4.jar:$CLASSPATH
Close the terminals and reopen them for the changes to take effect (or run source
on the file to reload it: source ~/.zshenv
).
Then the program should compile:
$ javac ThreeSum.java
A file named ThreeSum.class
will be produced.
The program should then be runnable on the input file:
$ java ThreeSum ../data/1Mints.txt
I put the data directory above the directory that holds my Java code because I have other directories for solving the algorithm exercises in other languages. That command is being run from the java
directory, and the data files are above that in data
:
.
├── c_cpp
├── data
├── elixir
│ ├── lib
│ │ └── algorithms
│ └── test
├── images
│ └── union_find
├── java
├── javascript
│ ├── test
│ └── union_find
└── python
(I’m also using the Python book and the C/C++ Udemy course as resources.)
I’m not familiar with Java imports, and there might be a better way to do it, but this method works for now.
If anyone knows of better ways to do these things, leave a comment below. 