Skip to content

Commit

Permalink
algoJava: c1 s3
Browse files Browse the repository at this point in the history
  • Loading branch information
AliNisarAhmed committed Apr 7, 2024
1 parent 180273c commit 6a3d379
Show file tree
Hide file tree
Showing 46 changed files with 2,937 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Java/AlgoJava/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>AlgoJava</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
<filteredResources>
<filter>
<id>1711739015881</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
3 changes: 3 additions & 0 deletions Java/AlgoJava/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/reneargento/algorithms-sedgewick-wayne/tree/master

https://algs4.cs.princeton.edu/code/
30 changes: 30 additions & 0 deletions Java/AlgoJava/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ali.algojava</groupId>
<artifactId>AlgoJava</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>AlgoJava</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.kevin-wayne</groupId>
<artifactId>algs4</artifactId>
<version>master-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
13 changes: 13 additions & 0 deletions Java/AlgoJava/src/main/java/com/ali/algojava/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.ali.algojava;

import edu.princeton.cs.algs4.*;

/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
StdOut.print(Average.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.ali.algojava.chapter1.section2;

import edu.princeton.cs.algs4.Counter;
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import java.util.Arrays;

// 1.2.9

public class BinarySearch {
public static void main(String[] args) {
Counter counter = new Counter("keys");
int[] whitelist = In.readInts(args[0]);
Arrays.sort(whitelist);
while (!StdIn.isEmpty()) {
int key = StdIn.readInt();
if (rank(key, whitelist, counter) == -1) {
StdOut.println(key);
}
}
StdOut.println(counter);
}

public static int rank(int key, int[] a, Counter counter) {
int lo = 0;
int hi = a.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
counter.increment();
if (key < a[mid]) {
hi = mid - 1;
} else if (key > a[mid]) {
lo = mid + 1;
} else {
return mid;
}
}

return -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.ali.algojava.chapter1.section2;

import edu.princeton.cs.algs4.StdOut;

// 1.2.6

public class CircularRotationString {
public static void main(String[] args) {
String s1 = "ACTGACG";
String s2 = "TGACGAC";

StdOut.print(isContained(s1, s2));
}

private static boolean isContained(String s1, String s2) {
return (s1.length() == s2.length()) && (s1 + s1).contains(s2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.ali.algojava.chapter1.section2;

public class Counter {
private final String name;
private int count;

public Counter(String id) {
name = id;
}

public void increment() {
count++;
}

public int tally() {
return count;
}

public String toString() {
return count + " " + name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.ali.algojava.chapter1.section2;

import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;

public class Flips {

public static void main(String[] args) {
int T = Integer.parseInt(args[0]);

Counter heads = new Counter("heads");
Counter tails = new Counter("tails");

for (int t = 0; t < T; t++) {
if (StdRandom.bernoulli(0.5)) {
heads.increment();
} else {
tails.increment();
}
}

StdOut.println(heads);
StdOut.println(tails);

int d = heads.tally() - tails.tally();
StdOut.println("delta: " + Math.abs(d));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.ali.algojava.chapter1.section2;

import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import java.util.Arrays;

// 1.2.1

public class PrintClosestDistance {

public static void main(String[] args) {
int N = Integer.parseInt(args[0]);

Point2D[] points = generatePoints(N);

printSmallestDistance(points);
}

private static Point2D[] generatePoints(int N) {
StdDraw.setCanvasSize(1024, 512);
StdDraw.setPenRadius(.015);
StdDraw.setXscale(0, 1);
StdDraw.setYscale(0, 1);

Point2D[] points = new Point2D[N];

for (int i = 0; i < N; i++) {
points[i] = new Point2D(StdRandom.uniform(0.0, 1.0), StdRandom.uniform(0.0, 1.0));
StdDraw.point(points[i].x(), points[i].y());
}

Arrays.sort(points, Point2D.X_ORDER);

return points;
}

private static void printSmallestDistance(Point2D[] points) {
double result = Double.MAX_VALUE;
Point2D point1 = null;
Point2D point2 = null;
for (int i = 0; i < points.length - 1; i++) {
Point2D p1 = points[i];
Point2D p2 = points[i + 1];
double d = p1.distanceTo(p2);
if (d < result) {
result = d;
point1 = p1;
point2 = p2;
}
}

StdOut.println(point1);
StdOut.println(point2);
StdOut.println(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.ali.algojava.chapter1.section2;

import edu.princeton.cs.algs4.Interval1D;
import edu.princeton.cs.algs4.Interval2D;
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import java.util.stream.IntStream;

public class PrintOverlap2DInterval {
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
double min = Double.parseDouble(args[1]);
double max = Double.parseDouble(args[2]);

Interval2D[] intervals = generateIntervals(N, min, max);

drawIntervals(intervals, min, max);
printOverlappingOrContaining(intervals);
}

private static Interval2D[] generateIntervals(int N, double min, double max) {
return IntStream.range(0, N)
.mapToObj(i -> new Interval2D(generate1DInterval(min, max),
generate1DInterval(min, max)))
.toArray(Interval2D[]::new);
}

private static Interval1D generate1DInterval(double min, double max) {
double d1 = StdRandom.uniform(min, max);
double d2 = StdRandom.uniform(min, max);
if (d1 > d2) {
return new Interval1D(d2, d1);
}
return new Interval1D(d1, d2);
}

private static void drawIntervals(Interval2D[] intervals, double min,
double max) {
StdDraw.setCanvasSize(1024, 512);
StdDraw.setPenRadius(.015);
StdDraw.setXscale(min, max);
StdDraw.setYscale(min, max);
for (Interval2D interval : intervals) {
interval.draw();
}
}

private static void printOverlappingOrContaining(Interval2D[] intervals) {
for (int i = 0; i < intervals.length - 1; i++) {
if (intervals[i].intersects(intervals[i + 1])) {
StdOut.print(intervals[i]);
StdOut.print(" intersects with ");
StdOut.println(intervals[i + 1]);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.ali.algojava.chapter1.section2;

import edu.princeton.cs.algs4.Interval1D;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import java.util.Arrays;

// 1.2.2

public class PrintOverlapInterval {

public static void main(String[] args) {
int N = Integer.parseInt(args[0]);

Interval1D[] intervals = new Interval1D[N];

for (int i = 0; i < N; i++) {
StdOut.print("Enter min: ");
double min = StdIn.readDouble();

StdOut.print("Enter max: ");
double max = StdIn.readDouble();

intervals[i] = new Interval1D(min, max);
}

Arrays.sort(intervals, Interval1D.MIN_ENDPOINT_ORDER);

for (int i = 0; i < intervals.length - 1; i++) {
Interval1D interval1 = intervals[i];
Interval1D interval2 = intervals[i + 1];

if (interval1.intersects(interval2)) {
StdOut.print(interval1);
StdOut.print(" intersects with: ");
StdOut.print(interval2);
}
}
}
}
Loading

0 comments on commit 6a3d379

Please sign in to comment.