Youcantcatchme
Youcantcatchme
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.
– At least 4 characters
Assumption:
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
password = input()
result = CheckPassword(password)
if result == 1:
print(1)
else:
print(0)
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;
}
}
```
```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;
}
}
```
```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);
}
}
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);
}
}
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/