-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dijkstra algorithm implementation need to be done properly
- Loading branch information
1 parent
68f47fd
commit d95608d
Showing
8 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import edu.princeton.cs.algs4.StdIn; | ||
|
||
import java.util.Stack; | ||
|
||
public class Evaluate142 { | ||
public static void main(String[] args) { | ||
Stack<String> ops = new Stack<>(); | ||
Stack<Double> vals = new Stack<>(); | ||
while (!StdIn.isEmpty()) { | ||
String s = StdIn.readString(); | ||
if (s.equals("(")) { | ||
|
||
} else if (s.equals("+")) { | ||
ops.push(s); | ||
} else if (s.equals("-")) { | ||
ops.push(s); | ||
} else if (s.equals("*")) { | ||
ops.push(s); | ||
} else if (s.equals("/")) { | ||
ops.push(s); | ||
} else if (s.equals("sqrt")) { | ||
ops.push(s); | ||
} else if (s.equals(")")) { | ||
String op = ops.pop(); | ||
double v = vals.pop(); | ||
if (op.equals("+")) { | ||
v = vals.pop() + v; | ||
} else if (op.equals("-")) { | ||
v = vals.pop() - v; | ||
} else if (op.equals("*")) { | ||
v = vals.pop() * v; | ||
} else if (op.equals("/")) { | ||
v = vals.pop() / v; | ||
} else if (op.equals("sqrt")) { | ||
v = Math.sqrt(v); | ||
} | ||
vals.push(v); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import edu.princeton.cs.algs4.Date; | ||
|
||
public class MemoryManagement104 { | ||
public static void main(String[] args) { | ||
Date a = new Date(12, 31, 1999); | ||
System.out.println(a); | ||
Date b = new Date(12,31,2001); | ||
System.out.println(b); | ||
b = a; | ||
System.out.println(a); | ||
System.out.println(b); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import edu.princeton.cs.algs4.StdIn; | ||
import edu.princeton.cs.algs4.StdOut; | ||
|
||
import java.util.Stack; | ||
|
||
public class Reverse { | ||
public static void main(String[] args) { | ||
Stack<Integer> stack; | ||
stack = new Stack<>(); | ||
while (!StdIn.isEmpty()) | ||
stack.push(StdIn.readInt()); | ||
while (!stack.isEmpty()) | ||
StdOut.println(stack.pop()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import edu.princeton.cs.algs4.Bag; | ||
import edu.princeton.cs.algs4.StdIn; | ||
import edu.princeton.cs.algs4.StdOut; | ||
|
||
public class Stats138 { | ||
public static void main(String[] args) { | ||
Bag<Double> numbers = new Bag<>(); | ||
while (!StdIn.isEmpty()) { | ||
numbers.add(StdIn.readDouble()); | ||
} | ||
int N = numbers.size(); | ||
|
||
double sum = 0.0; | ||
for (double x : numbers) { | ||
sum += x; | ||
} | ||
double mean = sum/N; | ||
|
||
sum = 0.0; | ||
for (double x:numbers){ | ||
sum+= (x-mean) * (x-mean); | ||
} | ||
double std = Math.sqrt(sum/(N-1)); | ||
StdOut.printf("Mean: %.2f\n", mean); | ||
StdOut.printf("Std dev: %.2f\n", std); | ||
} | ||
} |