密码复杂度 编写一个函数,参数为一串明文密码字符串,返回值为字符串长度以及字符串里面的大写字母、小写字母,和数字的个数总共四个数字
时间: 2024-02-15 22:03:58 浏览: 31
好的,下面是一个简单的实现:
```python
def password_complexity(password):
length = len(password)
upper_count = 0
lower_count = 0
digit_count = 0
for char in password:
if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
elif char.isdigit():
digit_count += 1
return f"Length: {length}, Upper: {upper_count}, Lower: {lower_count}, Digit: {digit_count}"
```
你可以这样调用:
```python
password = "Abc123"
result = password_complexity(password)
print(result)
```
输出结果应该是:
```
Length: 6, Upper: 1, Lower: 2, Digit: 3
```
相关问题
编写一个java字符串排序的demo,时间复杂度为o(1)
抱歉,基于比较的字符串排序算法的时间复杂度最佳情况下也需要 O(nlogn) 的时间复杂度,没有办法做到 o(1) 的时间复杂度。
如果您说的是空间复杂度为 O(1),那么可以使用基数排序算法来对字符串数组进行排序,其空间复杂度为 O(k+n),其中 k 表示字符串中的字符集大小,n 表示字符串数组的长度。
下面是使用基数排序算法对字符串数组进行排序的 Java 代码示例:
```java
import java.util.Arrays;
public class StringSort {
public static void main(String[] args) {
String[] arr = {"apple", "banana", "orange", "grape", "pear"};
// 获取最长字符串的长度
int maxLength = 0;
for (String str : arr) {
maxLength = Math.max(maxLength, str.length());
}
// 对每个字符进行排序
for (int i = maxLength - 1; i >= 0; i--) {
int[] count = new int[256]; // ASCII 码表的字符集大小为 256
String[] temp = new String[arr.length];
for (String str : arr) {
if (i < str.length()) {
count[str.charAt(i)]++;
} else {
count[0]++; // 空字符占一个位置
}
}
for (int j = 1; j < count.length; j++) {
count[j] += count[j - 1];
}
for (int j = arr.length - 1; j >= 0; j--) {
if (i < arr[j].length()) {
temp[--count[arr[j].charAt(i)]] = arr[j];
} else {
temp[--count[0]] = arr[j];
}
}
System.arraycopy(temp, 0, arr, 0, arr.length);
}
// 输出排序结果
System.out.println(Arrays.toString(arr));
}
}
```
输出结果为:
```
[apple, banana, grape, orange, pear]
```
编写一个java字符串倒序排序的demo,时间复杂度为 O(nlogn)
下面是使用快速排序算法对字符串数组进行倒序排序的 Java 代码示例:
```java
import java.util.Arrays;
public class StringSort {
public static void main(String[] args) {
String[] arr = {"apple", "banana", "orange", "grape", "pear"};
// 快速排序
quickSort(arr, 0, arr.length - 1);
// 输出排序结果
System.out.println(Arrays.toString(arr));
}
public static void quickSort(String[] arr, int left, int right) {
if (left < right) {
int pivot = partition(arr, left, right);
quickSort(arr, left, pivot - 1);
quickSort(arr, pivot + 1, right);
}
}
public static int partition(String[] arr, int left, int right) {
String pivot = arr[right];
int i = left - 1;
for (int j = left; j < right; j++) {
if (arr[j].compareTo(pivot) > 0) {
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, right);
return i + 1;
}
public static void swap(String[] arr, int i, int j) {
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
```
输出结果为:
```
[pear, orange, grape, banana, apple]
```