Skip to content

Commit 63bba80

Browse files
author
Christian Bender
authored
Merge pull request #218 from AKS1996/master
Additional files similiar to TheAlgorithms/Python
2 parents 0aae345 + 3abf1f5 commit 63bba80

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

Others/GuassLengendre.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.lang.Math;
2+
/*
3+
* author: @AKS1996
4+
* Guass Legendre Algorithm
5+
* ref https://en.wikipedia.org/wiki/Gauss–Legendre_algorithm
6+
*
7+
*/
8+
9+
public class GuassLegendre {
10+
11+
public static void main(String[] args) {
12+
for(int i=1;i<=3;++i)
13+
System.out.println(pi(i));
14+
15+
}
16+
17+
static double pi(int l){
18+
/*
19+
* l: No of loops to run
20+
*/
21+
22+
double a = 1,b=Math.pow(2,-0.5),t=0.25,p=1;
23+
for(int i=0;i<l;++i){
24+
double temp[] = update(a,b,t,p);
25+
a = temp[0];
26+
b = temp[1];
27+
t = temp[2];
28+
p = temp[3];
29+
}
30+
31+
return Math.pow(a+b, 2)/(4*t);
32+
}
33+
34+
static double[] update(double a, double b, double t, double p){
35+
double values[] = new double[4];
36+
values[0] = (a+b)/2;
37+
values[1] = Math.sqrt(a*b);
38+
values[2] = t - p*Math.pow(a - values[0],2);
39+
values[3] = 2*p;
40+
41+
return values;
42+
}
43+
44+
}

Others/PasswordGen.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.Collections;
2+
import java.util.Random;
3+
import java.util.List;
4+
import java.util.ArrayList;
5+
6+
/*
7+
Creates a random password from ASCII letters
8+
Given password length bounds
9+
10+
author: AKS1996
11+
date: 2017-10-25
12+
*/
13+
14+
class PasswordGen {
15+
public static void main(String args[]){
16+
String password = generatePassword(8,16);
17+
System.out.print("Password: " + password);
18+
}
19+
20+
static String generatePassword(int min_length, int max_length){
21+
Random random = new Random();
22+
23+
String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
24+
String lower = "abcdefghijklmnopqrstuvwxyz";
25+
String numbers = "0123456789";
26+
String specialChars = "!@#$%^&*(){}?";
27+
28+
String allChars = upper+lower+numbers+specialChars;
29+
30+
List<Character> letters = new ArrayList<Character>();
31+
for(char c:allChars.toCharArray())
32+
letters.add(c);
33+
34+
// Inbuilt method to randomly shuffle a elements of a list
35+
Collections.shuffle(letters);
36+
String password = "";
37+
38+
// Note that size of the password is also random
39+
for(int i = random.nextInt(max_length-min_length) + min_length; i>0; --i) {
40+
password += letters.get(random.nextInt(letters.size()));
41+
}
42+
43+
return password;
44+
}
45+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
3+
The nested brackets problem is a problem that determines if a sequence of
4+
brackets are properly nested. A sequence of brackets s is considered properly nested
5+
if any of the following conditions are true:
6+
- s is empty
7+
- s has the form (U) or [U] or {U} where U is a properly nested string
8+
- s has the form VW where V and W are properly nested strings
9+
For example, the string "()()[()]" is properly nested but "[(()]" is not.
10+
The function called is_balanced takes as input a string S which is a sequence of brackets and
11+
returns true if S is nested and false otherwise.
12+
13+
author: akshay sharma
14+
date: 2017-10-17
15+
*/
16+
import java.util.Scanner;
17+
import java.util.Stack;
18+
import java.util.ArrayList;
19+
20+
class BalancedBrackets {
21+
22+
static boolean is_balanced(char[] S) {
23+
Stack<Character> stack = new Stack<>();
24+
String pair = "";
25+
for (int i = 0; i < S.length; ++i) {
26+
if (S[i] == '(' || S[i] == '{' || S[i] == '[') {
27+
stack.push(S[i]);
28+
} else if (stack.size() > 0) {
29+
if (!pair.equals("[]") && !pair.equals("()") && !pair.equals("{}")) {
30+
return false;
31+
}
32+
} else {
33+
return false;
34+
}
35+
}
36+
37+
return stack.isEmpty();
38+
}
39+
40+
static void print(Object a) {
41+
System.out.println(a);
42+
}
43+
44+
public static void main(String args[]) {
45+
try {
46+
Scanner in = new Scanner(System.in);
47+
print("Enter sequence of brackets: ");
48+
String S = in.nextLine();
49+
if (is_balanced(S.toCharArray())) {
50+
print(S + " is balanced");
51+
} else {
52+
print(S + " ain't balanced");
53+
}
54+
in.close();
55+
} catch (Exception e) {
56+
e.toString();
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)