Skip to content

Additional files similiar to TheAlgorithms/Python #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Others/GuassLengendre.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.lang.Math;
/*
* author: @AKS1996
* Guass Legendre Algorithm
* ref https://en.wikipedia.org/wiki/Gauss–Legendre_algorithm
*
*/

public class GuassLegendre {

public static void main(String[] args) {
for(int i=1;i<=3;++i)
System.out.println(pi(i));

}

static double pi(int l){
/*
* l: No of loops to run
*/

double a = 1,b=Math.pow(2,-0.5),t=0.25,p=1;
for(int i=0;i<l;++i){
double temp[] = update(a,b,t,p);
a = temp[0];
b = temp[1];
t = temp[2];
p = temp[3];
}

return Math.pow(a+b, 2)/(4*t);
}

static double[] update(double a, double b, double t, double p){
double values[] = new double[4];
values[0] = (a+b)/2;
values[1] = Math.sqrt(a*b);
values[2] = t - p*Math.pow(a - values[0],2);
values[3] = 2*p;

return values;
}

}
45 changes: 45 additions & 0 deletions Others/PasswordGen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.Collections;
import java.util.Random;
import java.util.List;
import java.util.ArrayList;

/*
Creates a random password from ASCII letters
Given password length bounds

author: AKS1996
date: 2017-10-25
*/

class PasswordGen {
public static void main(String args[]){
String password = generatePassword(8,16);
System.out.print("Password: " + password);
}

static String generatePassword(int min_length, int max_length){
Random random = new Random();

String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lower = "abcdefghijklmnopqrstuvwxyz";
String numbers = "0123456789";
String specialChars = "!@#$%^&*(){}?";

String allChars = upper+lower+numbers+specialChars;

List<Character> letters = new ArrayList<Character>();
for(char c:allChars.toCharArray())
letters.add(c);

// Inbuilt method to randomly shuffle a elements of a list
Collections.shuffle(letters);
String password = "";

// Note that size of the password is also random
for(int i = random.nextInt(max_length-min_length) + min_length; i>0; --i) {
password += letters.get(random.nextInt(letters.size()));
}

return password;
}
}
59 changes: 59 additions & 0 deletions data_structures/Stacks/BalancedBrackets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*

The nested brackets problem is a problem that determines if a sequence of
brackets are properly nested. A sequence of brackets s is considered properly nested
if any of the following conditions are true:
- s is empty
- s has the form (U) or [U] or {U} where U is a properly nested string
- s has the form VW where V and W are properly nested strings
For example, the string "()()[()]" is properly nested but "[(()]" is not.
The function called is_balanced takes as input a string S which is a sequence of brackets and
returns true if S is nested and false otherwise.

author: akshay sharma
date: 2017-10-17
*/
import java.util.Scanner;
import java.util.Stack;
import java.util.ArrayList;

class BalancedBrackets {

static boolean is_balanced(char[] S) {
Stack<Character> stack = new Stack<>();
String pair = "";
for (int i = 0; i < S.length; ++i) {
if (S[i] == '(' || S[i] == '{' || S[i] == '[') {
stack.push(S[i]);
} else if (stack.size() > 0) {
if (!pair.equals("[]") && !pair.equals("()") && !pair.equals("{}")) {
return false;
}
} else {
return false;
}
}

return stack.isEmpty();
}

static void print(Object a) {
System.out.println(a);
}

public static void main(String args[]) {
try {
Scanner in = new Scanner(System.in);
print("Enter sequence of brackets: ");
String S = in.nextLine();
if (is_balanced(S.toCharArray())) {
print(S + " is balanced");
} else {
print(S + " ain't balanced");
}
in.close();
} catch (Exception e) {
e.toString();
}
}
}