Code Self Study Forum

Algorithms 4th Edition (Sedgewick / Wayne)

This section of the forum is for people who are working through Algorithms 4th Edition and/or the related Coursera courses (part 1 and part 2).

I’m reading the book at a very slow pace.

Hey, Josh. I’m planning to work through this book in the new year. I might sign up for the Coursera course as well.

Eric

1 Like

Great, we could share tips here and/or try to solve the puzzles in different languages.

If we need to hide algorithm implementations from the book, there is a “hide details” feature if you click the gear icon when writing a post. Here’s an example:

Click here to show the text
def greeting():
    """Implement hello world."""
    return('saluton mondo')

if __name__ == '__main__':
    print(greeting())

Great! Thanks for sharing that. I think it will be very helpful.

@eayoungs - The percolation exercise is here.

I didn’t write anything below than they didn’t mention in the assignment, but just translated their description into methods that don’t work yet. I don’t know Java, so this was just trying to get code to compile.

PercolationStats.java
import java.util.*;
import edu.princeton.cs.algs4.StdOut;

public class PercolationStats {

    public PercolationStats(int n, int trials)    // perform trials independent experiments on an n-by-n grid
    {
    }

    // sample mean of percolation threshold
    public static double mean(double a, double b)
    {
        // TODO: this is not the correct calculation.
        double result = a * b;
        return result;
    }

    public double stddev()                        // sample standard deviation of percolation threshold
    {
        return 0.0;
    }

    public double confidenceLo()                  // low  endpoint of 95% confidence interval
    {
        return 0.0;
    }

    public double confidenceHi()                  // high endpoint of 95% confidence interval
    {
        return 0.0;
    }

    public static void main(String[] args)        // test client (described below)
    {

        double n = Double.parseDouble(args[0]);
        double t = Double.parseDouble(args[1]);
        double main_result = mean(n, t);

        StdOut.println("The result is: " + main_result);

    }
}
Percolation.java
public class Percolation {

    /**
     * Throw a java.lang.IllegalArgumentException if any argument to open(), isOpen(), or isFull() is
     * outside its prescribed range. The constructor should throw a java.lang.IllegalArgumentException if n ≤ 0.
     *
     *
     * Performance requirements.  The constructor should take time proportional to n2; all methods should take constant
     * time plus a constant number of calls to the union–find methods union(), find(), connected(), and count().
     */

    public Percolation(int n)
    {
        // create n-by-n grid, with all sites blocked
    }

    public void open(int row, int col)
    {
        // open site (row, col) if it is not open already
    }

    public boolean isOpen(int row, int col)
    {
        // is site (row, col) open?
    }

    public boolean isFull(int row, int col)
    {
        // is site (row, col) full?
    }

    public int numberOfOpenSites()
    {
        // number of open sites
    }

    public boolean percolates()
    {
        // does the system percolate?
    }

    public static void main(String[] args)
    {
        // test client (optional)
    }

}

I may revisit the assignment later but will probably focus more on trying to get through the book than the course.

Oh, I thought maybe you translated it to Python. No worries if you don’t get back to it for a while, this will be a good reference for me. Thanks!

Not that assignment, but it would be easy to translate into Python classes.

Just FYI; if anyone else is working on the first assignment, I just got a passing submission. I’m new to Java so mine is by no means an expert opinion but I’d be happy to discuss!

1 Like

I’d be interested in seeing how it works.

Sure; I just don’t want to run afoul of the Coursera code of conduct.

1 Like

I’ve found some of the work translated into Scala; I’m hoping to go through that code and would be interested in discussing the implementation and it’s differences from Java to Scala.

1 Like

Thanks. There are some other interesting courses listed there too.

I found some versions of the problems in other languages.

JavaScript

Python

Java