Skip to content

Commit 47f695e

Browse files
authored
Merge branch 'master' into JoshJ-Problems
2 parents 4b404bd + 63bb7f6 commit 47f695e

23 files changed

+864
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.codefortomorrow.advanced.chapter13.examples;
2+
3+
public class BinarySearch {
4+
5+
// Java implementation of recursive Binary Search
6+
// Returns index of x if it is present in arr[l..
7+
// r], else return -1
8+
public static int binarySearch(int arr[], int l, int r, int x) {
9+
if (r >= l) {
10+
int mid = l + (r - l) / 2;
11+
12+
// If the element is present at the
13+
// middle itself
14+
if (arr[mid] == x) return mid;
15+
16+
// If element is smaller than mid, then
17+
// it can only be present in left subarray
18+
if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x);
19+
20+
// Else the element can only be present
21+
// in right subarray
22+
return binarySearch(arr, mid + 1, r, x);
23+
}
24+
25+
// We reach here when element is not present
26+
// in array
27+
return -1;
28+
}
29+
30+
// Driver method to test above
31+
public static void main(String args[]) {
32+
int arr[] = { 2, 3, 4, 10, 40 };
33+
int n = arr.length;
34+
int x = 10;
35+
int result = binarySearch(arr, 0, n - 1, x);
36+
if (result == -1) System.out.println(
37+
"Element not present"
38+
); else System.out.println("Element found at index " + result);
39+
}
40+
/* This code is contributed by Rajat Mishra
41+
https://www.geeksforgeeks.org/binary-search/ */
42+
43+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.codefortomorrow.advanced.chapter13.examples;
2+
3+
public class Fibonacci {
4+
5+
public static int fibonacciRecursion(int nthNumber) {
6+
//use recursion
7+
if (nthNumber == 0) {
8+
return 0;
9+
} else if (nthNumber == 1) {
10+
return 1;
11+
}
12+
return (
13+
fibonacciRecursion(nthNumber - 1) +
14+
fibonacciRecursion(nthNumber - 2)
15+
);
16+
}
17+
18+
public static int fibonacciLoop(int nthNumber) {
19+
//use loop
20+
int previouspreviousNumber, previousNumber = 0, currentNumber = 1;
21+
for (int i = 1; i < nthNumber; i++) {
22+
previouspreviousNumber = previousNumber;
23+
previousNumber = currentNumber;
24+
currentNumber = previouspreviousNumber + previousNumber;
25+
}
26+
return currentNumber;
27+
}
28+
// Credit: https://dev.to/khalilsaboor/fibonacci-recursion-vs-iteration--474l
29+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.codefortomorrow.advanced.chapter13.practice;
2+
3+
public class RecurMergeSort {
4+
5+
public static void recurMergeSort(Comparable[] array, int start, int end) {}
6+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.codefortomorrow.advanced.chapter13.solutions;
2+
3+
public class RecurMergeSort {
4+
5+
public static void recurMergeSort(Comparable[] array, int start, int end) {
6+
if (start >= end) {
7+
return;
8+
}
9+
int middle = (start + end) / 2;
10+
recurMergeSort(array, start, middle);
11+
recurMergeSort(array, middle + 1, end);
12+
int i = start;
13+
int j = middle + 1;
14+
Comparable[] sortedArray = new Comparable[end - start + 1];
15+
int k = 0;
16+
while (i <= middle && j <= end) {
17+
if (array[i].compareTo(array[j]) > 0) {
18+
sortedArray[k] = array[i];
19+
i++;
20+
} else {
21+
sortedArray[k] = array[j];
22+
j++;
23+
}
24+
k++;
25+
}
26+
while (i <= middle) {
27+
sortedArray[k] = array[i];
28+
i++;
29+
k++;
30+
}
31+
while (j <= end) {
32+
sortedArray[k] = array[j];
33+
j++;
34+
k++;
35+
}
36+
k = 0;
37+
for (int l = start; l <= end; l++) {
38+
array[l] = sortedArray[k];
39+
k++;
40+
}
41+
}
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.codefortomorrow.advanced.chapter14.examples;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
/**
8+
* Source: https://beginnersbook.com/2014/01/how-to-read-file-in-java-using-bufferedreader/
9+
*/
10+
public class BufferedReaderExample {
11+
12+
public static void main(String[] args) {
13+
BufferedReader br = null;
14+
BufferedReader br2 = null;
15+
16+
try {
17+
br = new BufferedReader(new FileReader("B:\\myfile.txt"));
18+
19+
//One way of reading the file
20+
System.out.println("Reading the file using readLine() method:");
21+
String contentLine = br.readLine();
22+
while (contentLine != null) {
23+
System.out.println(contentLine);
24+
contentLine = br.readLine();
25+
}
26+
27+
br2 = new BufferedReader(new FileReader("B:\\myfile2.txt"));
28+
29+
//Second way of reading the file
30+
System.out.println("Reading the file using read() method:");
31+
int num = 0;
32+
char ch;
33+
while ((num = br2.read()) != -1) {
34+
ch = (char) num;
35+
System.out.print(ch);
36+
}
37+
} catch (IOException ioe) {
38+
ioe.printStackTrace();
39+
}
40+
}
41+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.codefortomorrow.advanced.chapter14.practice.account;
2+
3+
public class InvalidAgeException {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.codefortomorrow.advanced.chapter14.practice.account;
2+
3+
public class InvalidPasswordException {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.codefortomorrow.advanced.chapter14.practice.account;
2+
3+
public class InvalidUsernameException {}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.codefortomorrow.advanced.chapter14.practice.account;
2+
3+
import java.util.Scanner;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) {
8+
Scanner sc = new Scanner(System.in);
9+
10+
System.out.println("Welcome to Account Creation!");
11+
while (true) {
12+
System.out.print("Enter username (4 to 10 characters): ");
13+
String username = sc.next();
14+
System.out.print("Enter age: ");
15+
int age = sc.nextInt();
16+
System.out.print("Enter password (4 to 10 characters): ");
17+
String password = sc.next();
18+
System.out.print("Confirm password (4 to 10 characters): ");
19+
String confirmPassword = sc.next();
20+
21+
// TODO: try and catch to handle exceptions
22+
createAccount(username, password, age, confirmPassword);
23+
System.out.println("Account created successfully!");
24+
}
25+
}
26+
27+
public static void createAccount(
28+
String username,
29+
String password,
30+
int age,
31+
String confirmPassword
32+
)
33+
throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException {
34+
// TODO: complete
35+
}
36+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.codefortomorrow.advanced.chapter14.practice.account;
2+
3+
public class PasswordMismatchException {}

0 commit comments

Comments
 (0)