Skip to content

Commit b71360c

Browse files
author
AKS1996
committed
Additional files similiar to TheAlgorrithms/Python
1 parent 01e2555 commit b71360c

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

Others/nested_brackets.java

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 nested_brackets {
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+
// pair = (stack.lastElement() + S[i]);
30+
if (!pair.equals("[]") && !pair.equals("()") && !pair.equals("{}")) {
31+
return false;
32+
}
33+
} else {
34+
return false;
35+
}
36+
}
37+
38+
return stack.isEmpty();
39+
}
40+
41+
static void print(Object a) {
42+
System.out.println(a);
43+
}
44+
45+
public static void main(String args[]) {
46+
try {
47+
Scanner in = new Scanner(System.in);
48+
print("Enter sequence of brackets: ");
49+
String S = in.nextLine();
50+
if (is_balanced(S.toCharArray())) {
51+
print(S + " is balanced");
52+
} else {
53+
print(S + " ain't balanced");
54+
}
55+
in.close();
56+
} catch (Exception e) {
57+
e.toString();
58+
}
59+
}
60+
}

Others/password_gen.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.Collections;
2+
import java.util.Random;
3+
import java.util.List;
4+
import java.util.ArrayList;
5+
/*
6+
Creates a random password from ASCII letters
7+
author: akshay sharma
8+
date: 2017-10-17
9+
*/
10+
class password_gen {
11+
public static void main(String args[]){
12+
Random random = new Random();
13+
List<Character> letters = new ArrayList<>();
14+
for(char c:"ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray())
15+
letters.add(c);
16+
17+
for(char c:"ABCDEFGHIJKLMNOPQRSTUVWXYZ".toLowerCase().toCharArray())
18+
letters.add(c);
19+
20+
for(char c:"0123456789".toCharArray())
21+
letters.add(c);
22+
23+
for(char c:"!@#$%^&*(){}?".toCharArray())
24+
letters.add(c);
25+
26+
27+
Collections.shuffle(letters);
28+
29+
int min_length = 8;
30+
int max_length = 16;
31+
String password = "";
32+
33+
for(int i= random.nextInt(max_length-min_length) + min_length; i>0; --i) {
34+
password += letters.get(random.nextInt(letters.size()));
35+
}
36+
System.out.print("Password: " + password);
37+
System.out.print("[ If you are thinking of using this passsword, You better save it. ]");
38+
}
39+
}

0 commit comments

Comments
 (0)