import string
def solution(s: str)-> list:
up, low, num, cha = 0, 0, 0, 0
for i in s:
if i in string.ascii_uppercase:
up +=1
elif i in string.ascii_lowercase:
low +=1
elif i in string.digits:
num +=1
elif i in string.punctuation: #如果 空格 换行\t \n等不算的话,如果算的话就用else
cha +=1
return [up, low, num, cha]
assert solution("*'&ABCDabcde12345") == [4,5,5,3]
assert solution("bgA5<1d-tOwUZTS8yQ") == [7,6,3,2]
assert solution("@mw>0=QD-iAx!rp9TaG?o&M%l$34L.nbft") == [7,13,4,10]
def solution(s: str)-> list:
a,b,c,d = 0,0,0,0
for i in s:
if i.isupper():
a += 1
elif i.islower():
b += 1
elif i.isdigit():
c += 1
else:
d += 1
return [a,b,c,d]
import re
u, l, n, c = len(re.findall('[A-Z]',s)),\
len(re.findall('[a-z]',s)),\
len(re.findall('\d',s)),\
len(re.findall('[^a-zA-Z0-9]',s))
return [u, l, n, c]
return [len(re.findall(item, s)) for item in ('[A-Z]', '[a-z]', '\d', '[^a-zA-Z0-9]')]
def solution(s: str)-> list:
result = [0]*4
for i in s:
if i.isupper():
result[0]+=1
elif i.islower():
result[1] += 1
elif i.isdecimal():
result[2] += 1
else:
result[3] += 1
return result
方法2
import re
def solution(s: str)-> list:
patts= [re.compile(r'[A-Z]*'),re.compile(r'[a-z]*'),re.compile(r'\d*',re.ASCII),re.compile(r'[^A-Za-z0-9]*']
return [len(''.join(i.findall(s))) for i in patts]
/**
* 函数
**/
public class Kata {
public static int[] Solve(String word) {
//enjoy coding
int[] list = new int[4];
for (int i = 0; i < word.length(); i++) {
int w = word.charAt(i) - 0;
if (w > 64 && w < 91) {
list[0] += 1;
} else if (w > 96 && w < 123) {
list[1] += 1;
} else if (w > 47 && w < 58) {
list[2] += 1;
}else {
list[3]+=1;
}
}
return list;
}
}
public class Kata {
public static int[] Solve(String word) {
int upperCase = (int) word.chars().filter(value -> Character.isUpperCase(value)).count();
int lowerCase = (int) word.chars().filter(value -> Character.isLowerCase(value)).count();
int number = (int) word.chars().filter(value -> Character.isDigit(value)).count();
int specialChar = word.length() - (upperCase + lowerCase + number);
return new int[]{upperCase, lowerCase, number, specialChar};
}
}
public class Kata {
public static int[] Solve(String word) {
return new int[]{
word.replaceAll("[^\\p{Upper}]", "").length(),
word.replaceAll("[^\\p{Lower}]", "").length(),
word.replaceAll("[^\\p{Digit}]", "").length(),
word.replaceAll("[\\p{Alnum}]", "").length(),
};
}
}
第四种
import java.util.Arrays;
public class Kata {
public static int[] Solve(String word) {
int[] sol = new int[4];
String[] arr = word.split("");
for (String x : arr){
if (x.matches(".*[A-Z].*")){
sol[0]++;
}
else if(x.matches(".*[a-z].*")){
sol[1]++;
}
else if(x.matches(".*[0-9].*")){
sol[2]++;
}
else if(x.matches(".*[!-@].*")){
sol[3]++;
}
}
return sol;
}
}
def solution(s: str)-> list:
# your code here
a, b, c, d =0, 0, 0, 0
for i in s:
if i.isupper():
a +=1
elif i.islower():
b +=1
elif i.isdigit():
c +=1
else:
d +=1
return [a, b, c, d]
assert solution("*'&ABCDabcde12345") == [4,5,5,3]
assert solution("bgA5<1d-tOwUZTS8yQ") == [7,6,3,2]
assert solution("@mw>0=QD-iAx!rp9TaG?o&M%l$34L.nbft") == [7,13,4,10]
def solution(s: str)-> list:
a,b,c,d = 0,0,0,0
for i in s:
if i.isupper():
a += 1
elif i.islower():
b += 1
elif i.isdigit():
c += 1
else:
d += 1
li = [a, b, c, d]
return li
assert solution("*'&ABCDabcde12345") == [4,5,5,3]
assert solution("bgA5<1d-tOwUZTS8yQ") == [7,6,3,2]
assert solution("@mw>0=QD-iAx!rp9TaG?o&M%l$34L.nbft") == [7,13,4,10]