You are given a function.
int CheckPassword(char str[], int n);
The function accepts string str of size n as an argument. Implement the function
which returns 1 if given string str is valid password else 0.
str is a valid password if it satisfies the below conditions.
– At least 4 characters
– At least one numeric digit
– At Least one Capital Letter
– Must not have space or slash (/)
– Starting character must not be a number
Assumption:
Input string will not be empty.
def CheckPassword(password):
n = len(password)
if n < 4:
return 0
if password[0].isdigit():
return 0
has_digit = False
has_uppercase = False
for char in password:
if char.isdigit():
has_digit = True
elif char.isupper():
has_uppercase = True
if has_digit and has_uppercase:
break
if ' ' in password or '/' in password:
return 0
return 1 if has_digit and has_uppercase else 0
password = input()
result = CheckPassword(password)
if result == 1:
print(1)
else:
print(0)
Here are five sorting algorithms with code examples in Java:
1. **Insertion Sort**: This algorithm sorts an array by comparing each element with
the previous elements and inserting it in the correct position[1].
```java
public static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
```
2. **Selection Sort**: This algorithm sorts an array by repeatedly finding the
minimum element from the unsorted part of the array and putting it at the
beginning[1].
```java
public static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
```
3. **Bubble Sort**: This algorithm sorts an array by repeatedly swapping adjacent
elements if they are in the wrong order[1].
```java
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
```
4. **Merge Sort**: This algorithm sorts an array by dividing it into two halves,
sorting each half, and then merging the two halves[1].
```java
public static void mergeSort(int[] arr, int l, int r) {
if (l < r) {
int m = (l + r) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
public static void merge(int[] arr, int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int[] L = new int[n1];
int[] R = new int[n2];
for (int i = 0; i < n1; i++) {
L[i] = arr[l + i];
}
for (int j = 0; j < n2; j++) {
R[j] = arr[m + 1 + j];
}
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
```
5. **Quick Sort**: This algorithm sorts an array by partitioning it into two parts,
sorting each part recursively, and then combining the two parts[1].
```java
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
public static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
```
These are just a few examples of sorting algorithms in Java. There are many more
algorithms and variations that can be used depending on the specific requirements
of the program.
Citations:
[1] https://betterprogramming.pub/5-basic-sorting-algorithms-you-must-know-
9ef5b1f3949c
[2] https://www.toptal.com/developers/sorting-algorithms
[3] https://www.freecodecamp.org/news/sorting-algorithms-explained-with-examples-
in-python-java-and-c/
[4] https://en.wikipedia.org/wiki/Sorting_algorithm
[5] https://www.programiz.com/dsa/sorting-algorithm
[6] https://www.geeksforgeeks.org/sorting-algorithms/