Skip to content

Commit 8048737

Browse files
committed
initial week 1 homework
1 parent 466541b commit 8048737

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+14634
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.algs4.hw1;
2+
3+
/******************************************************************************
4+
* Compilation: javac InteractivePercolationVisualizer.java
5+
* Execution: java InteractivePercolationVisualizer n
6+
* Dependencies: PercolationVisualizer.java Percolation.java
7+
* StdDraw.java StdOut.java
8+
*
9+
* This program takes the grid size n as a command-line argument.
10+
* Then, the user repeatedly clicks sites to open with the mouse.
11+
* After each site is opened, it draws full sites in light blue,
12+
* open sites (that aren't full) in white, and blocked sites in black.
13+
*
14+
******************************************************************************/
15+
16+
import edu.princeton.cs.algs4.StdDraw;
17+
import edu.princeton.cs.algs4.StdOut;
18+
19+
public class InteractivePercolationVisualizer {
20+
21+
public static void main(String[] args) {
22+
// n-by-n percolation system (read from command-line, default = 10)
23+
int n = 10;
24+
if (args.length == 1) n = Integer.parseInt(args[0]);
25+
26+
// repeatedly open site specified my mouse click and draw resulting system
27+
StdOut.println(n);
28+
29+
StdDraw.enableDoubleBuffering();
30+
Percolation perc = new Percolation(n);
31+
PercolationVisualizer.draw(perc, n);
32+
StdDraw.show();
33+
34+
while (true) {
35+
36+
// detected mouse click
37+
if (StdDraw.mousePressed()) {
38+
39+
// screen coordinates
40+
double x = StdDraw.mouseX();
41+
double y = StdDraw.mouseY();
42+
43+
// convert to row i, column j
44+
int i = (int) (n - Math.floor(y));
45+
int j = (int) (1 + Math.floor(x));
46+
47+
// open site (i, j) provided it's in bounds
48+
if (i >= 1 && i <= n && j >= 1 && j <= n) {
49+
if (!perc.isOpen(i, j)) {
50+
StdOut.println(i + " " + j);
51+
}
52+
perc.open(i, j);
53+
}
54+
55+
// draw n-by-n percolation system
56+
PercolationVisualizer.draw(perc, n);
57+
StdDraw.show();
58+
}
59+
60+
StdDraw.pause(20);
61+
}
62+
}
63+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.algs4.hw1;
2+
3+
public class Percolation
4+
{
5+
private int[] arr;
6+
public Percolation(int n)
7+
{
8+
for(int i = 0; i < n; i++)
9+
{
10+
arr[i] = i;
11+
}
12+
}
13+
14+
public boolean isOpen(int i, int j)
15+
{
16+
return false;
17+
}
18+
19+
public boolean isFull(int i, int j)
20+
{
21+
return false;
22+
}
23+
24+
public void open(int i, int j)
25+
{
26+
27+
}
28+
29+
public boolean percolates()
30+
{
31+
return false;
32+
}
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.algs4.hw1;
2+
3+
public class PercolationStats
4+
{
5+
// perform trials independent experiments on an n-by-n grid
6+
public PercolationStats(int n, int trials)
7+
{
8+
9+
}
10+
11+
// sample mean of percolation threshold
12+
public double mean()
13+
{
14+
return Double.NaN;
15+
}
16+
17+
// sample standard deviation of percolation threshold
18+
public double stddev()
19+
{
20+
return Double.NaN;
21+
}
22+
23+
// low endpoint of 95% confidence interval
24+
public double confidenceLo()
25+
{
26+
return Double.NaN;
27+
}
28+
29+
// high endpoint of 95% confidence interval
30+
public double confidenceHi()
31+
{
32+
return Double.NaN;
33+
}
34+
35+
// test client (described below)
36+
public static void main(String[] args)
37+
{
38+
39+
}
40+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.algs4.hw1;
2+
3+
/******************************************************************************
4+
* Compilation: javac PercolationVisualizer.java
5+
* Execution: java PercolationVisualizer input.txt
6+
* Dependencies: Percolation.java
7+
*
8+
* This program takes the name of a file as a command-line argument.
9+
* From that file, it
10+
*
11+
* - Reads the grid size n of the percolation system.
12+
* - Creates an n-by-n grid of sites (intially all blocked)
13+
* - Reads in a sequence of sites (row i, column j) to open.
14+
*
15+
* After each site is opened, it draws full sites in light blue,
16+
* open sites (that aren't full) in white, and blocked sites in black,
17+
* with with site (1, 1) in the upper left-hand corner.
18+
*
19+
******************************************************************************/
20+
21+
import java.awt.Font;
22+
23+
import edu.princeton.cs.algs4.In;
24+
import edu.princeton.cs.algs4.StdDraw;
25+
26+
public class PercolationVisualizer {
27+
28+
// delay in miliseconds (controls animation speed)
29+
private static final int DELAY = 100;
30+
31+
// draw n-by-n percolation system
32+
public static void draw(Percolation perc, int n) {
33+
StdDraw.clear();
34+
StdDraw.setPenColor(StdDraw.BLACK);
35+
StdDraw.setXscale(-0.05*n, 1.05*n);
36+
StdDraw.setYscale(-0.05*n, 1.05*n); // leave a border to write text
37+
StdDraw.filledSquare(n/2.0, n/2.0, n/2.0);
38+
39+
// draw n-by-n grid
40+
int opened = 0;
41+
for (int row = 1; row <= n; row++) {
42+
for (int col = 1; col <= n; col++) {
43+
if (perc.isFull(row, col)) {
44+
StdDraw.setPenColor(StdDraw.BOOK_LIGHT_BLUE);
45+
opened++;
46+
}
47+
else if (perc.isOpen(row, col)) {
48+
StdDraw.setPenColor(StdDraw.WHITE);
49+
opened++;
50+
}
51+
else
52+
StdDraw.setPenColor(StdDraw.BLACK);
53+
StdDraw.filledSquare(col - 0.5, n - row + 0.5, 0.45);
54+
}
55+
}
56+
57+
// write status text
58+
StdDraw.setFont(new Font("SansSerif", Font.PLAIN, 12));
59+
StdDraw.setPenColor(StdDraw.BLACK);
60+
StdDraw.text(0.25*n, -0.025*n, opened + " open sites");
61+
if (perc.percolates()) StdDraw.text(0.75*n, -0.025*n, "percolates");
62+
else StdDraw.text(0.75*n, -0.025*n, "does not percolate");
63+
64+
}
65+
66+
public static void main(String[] args) {
67+
In in = new In(args[0]); // input file
68+
int n = in.readInt(); // n-by-n percolation system
69+
70+
// turn on animation mode
71+
StdDraw.enableDoubleBuffering();
72+
73+
// repeatedly read in sites to open and draw resulting system
74+
Percolation perc = new Percolation(n);
75+
draw(perc, n);
76+
StdDraw.show();
77+
StdDraw.pause(DELAY);
78+
while (!in.isEmpty()) {
79+
int i = in.readInt();
80+
int j = in.readInt();
81+
perc.open(i, j);
82+
draw(perc, n);
83+
StdDraw.show();
84+
StdDraw.pause(DELAY);
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy