Support
Quality
Security
License
Reuse
kandi has reviewed algs4 and discovered the below as its top functions. This is intended to give you an instant insight into algs4 implemented functionality, and help decide if they suit your requirements.
Our original goal was to cover the 50 algorithms that every programmer should know. We use the word programmer to refer to anyone engaged in trying to accomplish something with the help of a computer, including scientists, engineers, and applications developers, not to mention college students in science, engineering, and computer science. The code is optimized for clarity, portability, and efficiency. While some of our implementations are as fast as (or faster than) their counterparts in java.util, our main goal is to express the core algorithmic ideas in an elegant and simple manner. While we embrace some advanced Java features (such as generics and iterators), we avoid those that interfere with the exposition (such as inheritance and concurrency).
Java Cannot Load Main Class Without Adding .java
$ java -cp ".lift/algs4.jar;." RandomWord < coin.txt
$ java -cp ".lift/algs4.jar:." RandomWord < coin.txt
-----------------------
$ java -cp ".lift/algs4.jar;." RandomWord < coin.txt
$ java -cp ".lift/algs4.jar:." RandomWord < coin.txt
compareTo with objects returns a false while it is true
public class MyBST {
private Object e;
public MyBST(Object e) {
this.e = e;
}
public Object getE(){
return e;
}
@Override
public int hashCode() {
return Objects.hashCode(e);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof MyBST))
return false;
MyBST me = (MyBST) obj;
if (e == null) {
if (me.e != null)
return false;
} else if (!e.equals(me.e))
return false;
return true;
}
}
class MyBST<T extends Comparable<? super T>> implements Comparable<MyBST<T>>{
private T e;
public MyBST(T e){
this.e = e;
}
public T getE(){
return e;
}
@Override
public int compareTo(MyBST<T> o) {
return e.compareTo(o.e);
}
}
class Apple {
String type;
int weight;
}
class MyBST implements Comparable<MyBST> {
private Apple apple;
public MyBST(Apple apple) {
this.apple = apple;
}
@Override
public int compareTo(MyBST e) {
// this could be different depending on how you wanted
// to compare one apple to another. This comparison favors
// type over weight.
// check type - String class implements comparable
int ret = apple.type.compareTo(e.apple.type);
if (ret != 0) {
return ret;
}
// same type so check weight
if (apple.weight < e.apple.weight) {
return -1;
}
if (apple.weight > e.apple.weight) {
return 1;
}
return 0; // equals apples based on criteria
}
}
private Object getE(){
return e;
}
-----------------------
public class MyBST {
private Object e;
public MyBST(Object e) {
this.e = e;
}
public Object getE(){
return e;
}
@Override
public int hashCode() {
return Objects.hashCode(e);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof MyBST))
return false;
MyBST me = (MyBST) obj;
if (e == null) {
if (me.e != null)
return false;
} else if (!e.equals(me.e))
return false;
return true;
}
}
class MyBST<T extends Comparable<? super T>> implements Comparable<MyBST<T>>{
private T e;
public MyBST(T e){
this.e = e;
}
public T getE(){
return e;
}
@Override
public int compareTo(MyBST<T> o) {
return e.compareTo(o.e);
}
}
class Apple {
String type;
int weight;
}
class MyBST implements Comparable<MyBST> {
private Apple apple;
public MyBST(Apple apple) {
this.apple = apple;
}
@Override
public int compareTo(MyBST e) {
// this could be different depending on how you wanted
// to compare one apple to another. This comparison favors
// type over weight.
// check type - String class implements comparable
int ret = apple.type.compareTo(e.apple.type);
if (ret != 0) {
return ret;
}
// same type so check weight
if (apple.weight < e.apple.weight) {
return -1;
}
if (apple.weight > e.apple.weight) {
return 1;
}
return 0; // equals apples based on criteria
}
}
private Object getE(){
return e;
}
-----------------------
public class MyBST {
private Object e;
public MyBST(Object e) {
this.e = e;
}
public Object getE(){
return e;
}
@Override
public int hashCode() {
return Objects.hashCode(e);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof MyBST))
return false;
MyBST me = (MyBST) obj;
if (e == null) {
if (me.e != null)
return false;
} else if (!e.equals(me.e))
return false;
return true;
}
}
class MyBST<T extends Comparable<? super T>> implements Comparable<MyBST<T>>{
private T e;
public MyBST(T e){
this.e = e;
}
public T getE(){
return e;
}
@Override
public int compareTo(MyBST<T> o) {
return e.compareTo(o.e);
}
}
class Apple {
String type;
int weight;
}
class MyBST implements Comparable<MyBST> {
private Apple apple;
public MyBST(Apple apple) {
this.apple = apple;
}
@Override
public int compareTo(MyBST e) {
// this could be different depending on how you wanted
// to compare one apple to another. This comparison favors
// type over weight.
// check type - String class implements comparable
int ret = apple.type.compareTo(e.apple.type);
if (ret != 0) {
return ret;
}
// same type so check weight
if (apple.weight < e.apple.weight) {
return -1;
}
if (apple.weight > e.apple.weight) {
return 1;
}
return 0; // equals apples based on criteria
}
}
private Object getE(){
return e;
}
-----------------------
public class MyBST {
private Object e;
public MyBST(Object e) {
this.e = e;
}
public Object getE(){
return e;
}
@Override
public int hashCode() {
return Objects.hashCode(e);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof MyBST))
return false;
MyBST me = (MyBST) obj;
if (e == null) {
if (me.e != null)
return false;
} else if (!e.equals(me.e))
return false;
return true;
}
}
class MyBST<T extends Comparable<? super T>> implements Comparable<MyBST<T>>{
private T e;
public MyBST(T e){
this.e = e;
}
public T getE(){
return e;
}
@Override
public int compareTo(MyBST<T> o) {
return e.compareTo(o.e);
}
}
class Apple {
String type;
int weight;
}
class MyBST implements Comparable<MyBST> {
private Apple apple;
public MyBST(Apple apple) {
this.apple = apple;
}
@Override
public int compareTo(MyBST e) {
// this could be different depending on how you wanted
// to compare one apple to another. This comparison favors
// type over weight.
// check type - String class implements comparable
int ret = apple.type.compareTo(e.apple.type);
if (ret != 0) {
return ret;
}
// same type so check weight
if (apple.weight < e.apple.weight) {
return -1;
}
if (apple.weight > e.apple.weight) {
return 1;
}
return 0; // equals apples based on criteria
}
}
private Object getE(){
return e;
}
How to save permutation in a Set Java
...
for (int i = 0; i < tes.toArray().length; i++) {
Integer key = it.next();
bst1.put(key, 0);
}
Set<Stack<Integer>> combos = combos(tes, stack, tes.size()); //there you have set with all Stacks
}
}
public static Set<Stack<Integer>> combos(Set<Integer> items, Stack<Integer> stack, int size) {
Set<Stack<Integer>> set = new HashSet<>();
if(stack.size() == size) {
System.out.println(stack.to);
set.add((Stack) stack.clone());
}
Integer[] itemz = items.toArray(new Integer[0]);
for(Integer i : itemz) {
stack.push(i);
items.remove(i);
set.addAll(combos(items, stack, size));
items.add(stack.pop());
}
return set;
}
How to show top to bottom leaks in my path
public class AssignmentTwo {
public static void main(String[] args) {
int n = 500;
double p = 0.60;
StdDraw.enableDoubleBuffering();
boolean[][] open = Percolation.random(n, p);
StdDraw.clear();
StdDraw.setPenColor(StdDraw.PINK);
Percolation.show(open, false);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.show();
StdDraw.disableDoubleBuffering();
boolean[][] full = Percolation.flow(open);
Percolation.show(full, true);
}
}
How to count number of edges between red and white nodes in a graph?
private int countEdges(Graph G, int[] rednodes) {
int count = 0;
Arrays.sort(rednodes);
for ( int rednode : rednodes ) {
for ( int adj : G.adj(rednode) ) {
if ( Arrays.binarySearch(rednodes, adj) < 0 ) {
count++;
}
}
}
return count;
}
How to use read file from URI using C# in PowerShell line by line?
# Set $f to point to the target file on the web
$f = 'https://algs4.cs.princeton.edu/15uf/tinyUF.txt'
# $wc to represent the WebClient
$wc = [System.Net.WebClient]::new()
# Create a stream object, using the .OpenRead method on $f
$stream = $wc.OpenRead($f)
# Create a reader of the stream using System.IO.StreamReader
$reader = [System.IO.StreamReader]($stream)
# ForEach-Object didn't play nice, so using while until it displays the last line of the file
while (-not($reader.EndOfStream)) {
$line = $reader.ReadLine();
$line
}
# Close the StreamReader
$reader.Close()
# Dispose of the WebClient
$wc.Dispose()
Ruby Heapsort: `sink': undefined method `>' for nil:NilClass
if !a[j + 1].nil? # check if there is a right child
while n > 1
swap(a, 1, n)
n -= 1
sink(a, 1, n)
end
def sink(a, k, n)
while 2 * k <= n
j = 2 * k # child
if j + 1 <= n # check if there is a right child
j += 1 if j > 1 && less(a, j, j + 1) # if left child less than right child
end
break if !less(a, k, j) # If parent greater than child break
swap(a, k, j)
k = j
end
end
-----------------------
if !a[j + 1].nil? # check if there is a right child
while n > 1
swap(a, 1, n)
n -= 1
sink(a, 1, n)
end
def sink(a, k, n)
while 2 * k <= n
j = 2 * k # child
if j + 1 <= n # check if there is a right child
j += 1 if j > 1 && less(a, j, j + 1) # if left child less than right child
end
break if !less(a, k, j) # If parent greater than child break
swap(a, k, j)
k = j
end
end
-----------------------
if !a[j + 1].nil? # check if there is a right child
while n > 1
swap(a, 1, n)
n -= 1
sink(a, 1, n)
end
def sink(a, k, n)
while 2 * k <= n
j = 2 * k # child
if j + 1 <= n # check if there is a right child
j += 1 if j > 1 && less(a, j, j + 1) # if left child less than right child
end
break if !less(a, k, j) # If parent greater than child break
swap(a, k, j)
k = j
end
end
parameterized test constructor of junit java error message: Test class should have exactly one public zero-argument constructor
public SolverTest(Board board, int expectedNumberOfMoves) {
this.board = board;
this.expectedNumberOfMoves = expectedNumberOfMoves;
}
public SolverTest() {
}
@RunWith(Parameterized.class)
public class SolverTest {
final static File folder = new File("C:\\Users\\Azizam\\IdeaProjects\\EightPuzzle\\src\\ModifiedTests");
final static String destFolder = "C:\\Users\\Azizam\\IdeaProjects\\EightPuzzle\\src\\testresults";
final static ArrayList<Object[][]> filesList = new ArrayList<>();
final Object currentBoard = new Object();
@Parameterized.Parameters
public static Iterable<Object[][]> data() {
String path = "";
int counter = 0;
for (final File fileEntry : folder.listFiles()) {
//System.out.println("processing file: " + fileEntry.getName())
counter++;
if (counter == 20) break;
path = destFolder + fileEntry;
In in = new In(fileEntry.getAbsolutePath());
int n = in.readInt();
int moves = in.readInt();
int[][] tiles = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
tiles[i][j] = in.readInt();
Board b = new Board(tiles);
Object[][] fileList = new Object[][]{{b, moves}};
filesList.add(fileList);
}
return filesList;
}
@Parameterized.Parameter(0)
public Board board;
@Parameterized.Parameter(1)
public int expectedNumberOfMoves;
public SolverTest() {
}
@Test
public void test() {
assertEquals(expectedNumberOfMoves, new Solver(board).moves());
}
}
-----------------------
public SolverTest(Board board, int expectedNumberOfMoves) {
this.board = board;
this.expectedNumberOfMoves = expectedNumberOfMoves;
}
public SolverTest() {
}
@RunWith(Parameterized.class)
public class SolverTest {
final static File folder = new File("C:\\Users\\Azizam\\IdeaProjects\\EightPuzzle\\src\\ModifiedTests");
final static String destFolder = "C:\\Users\\Azizam\\IdeaProjects\\EightPuzzle\\src\\testresults";
final static ArrayList<Object[][]> filesList = new ArrayList<>();
final Object currentBoard = new Object();
@Parameterized.Parameters
public static Iterable<Object[][]> data() {
String path = "";
int counter = 0;
for (final File fileEntry : folder.listFiles()) {
//System.out.println("processing file: " + fileEntry.getName())
counter++;
if (counter == 20) break;
path = destFolder + fileEntry;
In in = new In(fileEntry.getAbsolutePath());
int n = in.readInt();
int moves = in.readInt();
int[][] tiles = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
tiles[i][j] = in.readInt();
Board b = new Board(tiles);
Object[][] fileList = new Object[][]{{b, moves}};
filesList.add(fileList);
}
return filesList;
}
@Parameterized.Parameter(0)
public Board board;
@Parameterized.Parameter(1)
public int expectedNumberOfMoves;
public SolverTest() {
}
@Test
public void test() {
assertEquals(expectedNumberOfMoves, new Solver(board).moves());
}
}
-----------------------
public SolverTest(Board board, int expectedNumberOfMoves) {
this.board = board;
this.expectedNumberOfMoves = expectedNumberOfMoves;
}
public SolverTest() {
}
@RunWith(Parameterized.class)
public class SolverTest {
final static File folder = new File("C:\\Users\\Azizam\\IdeaProjects\\EightPuzzle\\src\\ModifiedTests");
final static String destFolder = "C:\\Users\\Azizam\\IdeaProjects\\EightPuzzle\\src\\testresults";
final static ArrayList<Object[][]> filesList = new ArrayList<>();
final Object currentBoard = new Object();
@Parameterized.Parameters
public static Iterable<Object[][]> data() {
String path = "";
int counter = 0;
for (final File fileEntry : folder.listFiles()) {
//System.out.println("processing file: " + fileEntry.getName())
counter++;
if (counter == 20) break;
path = destFolder + fileEntry;
In in = new In(fileEntry.getAbsolutePath());
int n = in.readInt();
int moves = in.readInt();
int[][] tiles = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
tiles[i][j] = in.readInt();
Board b = new Board(tiles);
Object[][] fileList = new Object[][]{{b, moves}};
filesList.add(fileList);
}
return filesList;
}
@Parameterized.Parameter(0)
public Board board;
@Parameterized.Parameter(1)
public int expectedNumberOfMoves;
public SolverTest() {
}
@Test
public void test() {
assertEquals(expectedNumberOfMoves, new Solver(board).moves());
}
}
`java myClass` not finding a .class file when `java myClass.java` does? (OpenJDK)
// Linux or MacOS
java -cp ".:foo.jar" MyClass
// Windows
java -cp ".;foo.jar" MyClass
Resizing array when elements are more than 1/2 of his size
public void resize(int m) {
int[] temp = new int[m]; // <-- this is the new double-size of m
for(int i=0; i<hashTable.length; i++) {
temp[i] = hashTable[i];
}
hashTable = new int[m];
for(int i=0; i<temp.length; i++) { // <-- here we go too far
hashing(temp[i]);
}
}
public void resize(int m) {
int[] oldHash = hashTable;
hashTable = new int[m];
for(int i=0; i<oldHash.length; i++) {
if (oldHash[i] != 0) { // <-- don't hash empty slots
hashing(oldHash[i]);
}
}
}
-----------------------
public void resize(int m) {
int[] temp = new int[m]; // <-- this is the new double-size of m
for(int i=0; i<hashTable.length; i++) {
temp[i] = hashTable[i];
}
hashTable = new int[m];
for(int i=0; i<temp.length; i++) { // <-- here we go too far
hashing(temp[i]);
}
}
public void resize(int m) {
int[] oldHash = hashTable;
hashTable = new int[m];
for(int i=0; i<oldHash.length; i++) {
if (oldHash[i] != 0) { // <-- don't hash empty slots
hashing(oldHash[i]);
}
}
}
QUESTION
Java Cannot Load Main Class Without Adding .java
Asked 2021-Oct-24 at 16:08I'm trying to run a Java class file which I've compiled, but it keeps telling me that it cannot find or load the main class.
I have a directory which looks like this:
My goal is to run the RandomWord.java file, which takes in a file input and relies on the algs4.jar file found in the .lift sub-directory.
From what I've seen, to compile, I need to run the following (assuming I'm cd'ed into the hello directory):
$ javac -cp .lift/algs4.jar RandomWord.java
This should create my RandomWord.class file.
Now, to run it with a txt file, for example with coin.txt, I believe I should be running the following:
$ java -cp .lift/algs4.jar RandomWord < coin.txt
However, this results in an error stating: Error: Could not find or load main class RandomWord.
I've done a ton of experimenting with this but I have no idea what's going on, but I've found that running the following works:
$ java -cp .lift/algs4.jar RandomWord.java < coin.txt
However, I believe when a .class file is created, I shouldn't be writing .java in my command line, so I'm really confused.
ANSWER
Answered 2021-Oct-24 at 16:08Windows:
$ java -cp ".lift/algs4.jar;." RandomWord < coin.txt
Linux/Mac
$ java -cp ".lift/algs4.jar:." RandomWord < coin.txt
Current dir must be added to classpath to allow java to find classes there.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Explore Related Topics
Save this library and start creating your kit