Skip to content

Commit 1eeb42c

Browse files
author
Deepak Malik
committed
Algorithms
1 parent ad17605 commit 1eeb42c

File tree

10 files changed

+128
-2
lines changed

10 files changed

+128
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.deepak.algorithms.Advanced_Algorithms;
2+
3+
public class Dijkstra_Shortest_Path {
4+
5+
}

src/com/deepak/algorithms/Advanced_Algorithms/Dijkstra.java renamed to src/com/deepak/algorithms/Advanced_Algorithms/Travelling_Salesman.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.deepak.algorithms.Advanced_Algorithms;
22

3-
public class Dijkstra {
3+
public class Travelling_Salesman {
44

55
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.deepak.algorithms.Dynamic_Programming;
2+
3+
public class Knapsack {
4+
5+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.deepak.algorithms.Iterators;
2+
3+
import java.util.Stack;
4+
5+
import com.deepak.algorithms.Library.TreeNode;
6+
7+
public class DFSForTree {
8+
9+
public static void main(String[] args) {
10+
11+
}
12+
13+
public static void doPreOrder(TreeNode<Integer> root) {
14+
Stack<TreeNode<Integer>> stack = new Stack<>();
15+
stack.push(root);
16+
TreeNode<Integer> current = root;
17+
while (!stack.isEmpty() && current != null) {
18+
TreeNode<Integer> itemOnTop = stack.pop();
19+
System.out.println(itemOnTop + " ");
20+
if (itemOnTop.hasLeft()) {
21+
current = itemOnTop.left;
22+
}
23+
}
24+
25+
}
26+
27+
public static void doInOrder(TreeNode<Integer> root) {
28+
29+
}
30+
31+
public static void doPostOrder(TreeNode<Integer> root) {
32+
33+
}
34+
35+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.deepak.algorithms.Iterators;
2+
3+
import java.util.Iterator;
4+
5+
public class InOrderIterator<T> implements Iterator<T> {
6+
7+
8+
@Override
9+
public boolean hasNext() {
10+
return false;
11+
}
12+
13+
@Override
14+
public T next() {
15+
return null;
16+
}
17+
18+
@Override
19+
public void remove() {
20+
throw new UnsupportedOperationException("Remove not supported!!");
21+
}
22+
23+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.deepak.algorithms.Iterators;
2+
3+
public class PostOrderIterator {
4+
5+
6+
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.deepak.algorithms.Recursion;
2+
3+
public class Tower_Of_Hanoi {
4+
5+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.deepak.algorithms.Regex;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
public class RegexForUSPhoneNumber {
7+
8+
public static void main(String[] args) {
9+
System.out.println(matchUSPhoneNumber("425-633-6014"));
10+
System.out.println(matchUSPhoneNumber("425-633-601"));
11+
System.out.println(matchUSPhoneNumber("425-6336014"));
12+
System.out.println(matchUSPhoneNumber("(425)-633-6014"));
13+
System.out.println(matchUSPhoneNumber("42-33-6014"));
14+
System.out.println(matchUSPhoneNumber("-633-6014"));
15+
System.out.println(matchUSPhoneNumber("6014"));
16+
System.out.println(matchUSPhoneNumber("425-633-60145"));
17+
}
18+
19+
/**
20+
* Method to match US phone number
21+
*
22+
* @param input
23+
* @return {@link boolean}
24+
*/
25+
public static boolean matchUSPhoneNumber(String input) {
26+
String regexforPhone = "^\\(?([0-9]{3})\\)?[-.\\s]?([0-9]{3})[-.\\s]?([0-9]{4})$";
27+
Pattern pattern = Pattern.compile(regexforPhone);
28+
Matcher matcher = pattern.matcher(input);
29+
return matcher.matches();
30+
}
31+
32+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Regex Introduction
2+
3+
Regex is a sequence of characters that helps us to find a string or set of strings. Java provides java.util.regex package for pattern matching with regular expressions.
4+
There are 3 classes in this package,
5+
6+
1. Pattern class - invoke compile() method alongwith the regex as param. This returns a pattern back.
7+
2. Matcher class - It interprets the pattern and matches it with given input.
8+
3. PatternSyntaxException class - Will be invoked if there exists a error in pattern.
9+
10+
Capturing groups -
11+
way to treat multiple characters as one single unit. Capturing groups are counted by number of open paranthesis
12+
in the regex.
13+
14+

src/com/deepak/algorithms/Sorting/MergeSort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class MergeSort {
1717
* @param args
1818
*/
1919
public static void main(String[] args) {
20-
int[] valuesToBeSorted = {7, 10, 47, 40, 83, 84, 65, 61, 32, 55, 49, 46, 25, 20, 93, 63, 54, 10};
20+
int[] valuesToBeSorted = {7, 10, 47, 40};
2121
System.out.println("******************* MERGE - SORT *******************");
2222
int[] sortedValues = performMergeSort(valuesToBeSorted, 0, valuesToBeSorted.length - 1);
2323
Arrays.stream(sortedValues).forEach(System.out::println);

0 commit comments

Comments
 (0)