【每日一题1216】检查大小写

:woman_mage:请编写一个函数,接收两个字符串ab,请判断他们是否是字母且大小写是否一致。如果一致则返回1,不一致则返回0。如果其中任意一个不是字母,则返回-1。

示例:
输入:“a” 和 “g”,返回:1。
输入:“A” 和 “G”, 返回:1。
输入:“a” 和 “G”, 返回:0。
输入:“A” 和 “?”, 返回:-1。

题目难度:简单
题目来源:Check same case

def solution(a: str, b: str) -> int:
    # your code 

assert solution("a", "g") == 1
assert solution("A", "G") == 1
assert solution("a", "G") == 0
assert solution("0", "?") == -1
def solution(a: str, b: str) -> int:
    if a.isalpha() and b.isalpha():
        if (a.isupper() and b.isupper()) or (a.islower() and b.islower()):
            return 1
        else:
            return 0
    else:
        return -1

assert solution("a", "g") == 1
assert solution("A", "G") == 1
assert solution("a", "G") == 0
assert solution("0", "?") == -1
def solution(a, b): 
    return a.isupper() == b.isupper() if a.isalpha() and b.isalpha() else -1

assert solution("a", "g") == 1
assert solution("A", "G") == 1
assert solution("a", "G") == 0
assert solution("0", "?") == -1
3 Likes
import re
def solution(a: str, b: str) -> int:
    res_lower_a = re.search('[a-z]', a)
    res_lower_b = re.search('[a-z]', b)
    res_up_a = re.search('[A-Z]', a)
    res_up_b = re.search('[A-Z]', b)
    # 全大写或者全小写
    if (res_lower_a and res_lower_b) or (res_up_a and res_up_b):
        return 1
    # 是字母但是大小写不一致
    elif (res_lower_a and res_up_b) or (res_up_a and res_lower_b):
        return 0
    else:
        return -1

assert solution("a", "g") == 1
assert solution("A", "G") == 1
assert solution("a", "G") == 0
assert solution("0", "?") == -1
def solution(a: str, b: str) -> int:
    return -1 if not (a.isalpha() or b.isalpha()) else 1 if (
                (a.islower() and b.islower()) or (a.isupper() and b.isupper())) else 0


assert solution("a", "g") == 1
assert solution("A", "G") == 1
assert solution("a", "G") == 0
assert solution("0", "?") == -1
def solution(a: str, b: str) -> int:
    if a.isalpha() and b.isalpha():
        if a.isupper() and b.isupper():
            return 1
        elif a.islower() and b.islower():
            return 1
        else:
            return 0
    else:
        return -1        
        

assert solution("a", "g") == 1
assert solution("A", "G") == 1
assert solution("a", "G") == 0
assert solution("0", "?") == -1
def solution(a: str, b: str) -> int:
    # your code
    a_1 = a.isupper()
    b_1 = b.isupper()
    a_2 = a.islower()
    b_2 = b.islower()
    if a_1 == b_1 is True or a_2 == b_2 is True:
        return 1
    elif (a_1 is True or a_2 is True) and (b_1 is True or b_2 is True):
        return 0
    else:
        return -1


assert solution("a", "g") == 1
assert solution("A", "G") == 1
assert solution("a", "G") == 0
assert solution("0", "?") == -1
def solution(a:str, b:str) ->int:
    result = ((1 if (a.isupper() and b.isupper()) or (a.islower() and b.islower()) else 0) 
              if a.isalpha() and b.isalpha() else -1)
    return result
assert solution("a", "g") == 1
assert solution("A", "G") == 1
assert solution("a", "G") == 0
assert solution("A", "?") == -1
def check_s(ss:str):
    # 匹配除了字母以外的任意字符
    my_re=re.compile(r'[^A-Za-z]')
    res=re.findall(my_re,ss)
    # print(res)
    if len(res):
        # print('字符串存在任意一个不是字母')
        return -1
    else:
        if ss.isupper():
            # print('字符串都是大写字母')
            return 11
        elif ss.islower():
            # print('字符串都是小写字母')
            return 12
        else:
            # print('字符串包含大小写字母')
            return 0
def solution(a:str,b:str):
    if check_s(a) ==check_s(b) == 11:
        return 1
    elif check_s(a) ==check_s(b) == 12:
        return 1
    elif check_s(a)==-1 or check_s(b)==-1:
        return -1
    else:
        return 0
assert solution("a","g")==1
assert solution("A","G")==1
assert solution("a","G")==0
assert solution("0","?")==-1
    public int solution(char a,char b){
        int i;
        int j;
        // 判断第一个字符的大小写
        if (Character.isLowerCase(a)){
            i = 0;
        }else if (Character.isUpperCase(a)){
            i = 1;
        }else {
            i = 3;
        }

        // 判断第二个字符的大小写
        if (Character.isLowerCase(b)){
            j = 0;
        }else if (Character.isUpperCase(b)){
            j = 1;
        }else {
            j = 3;
        }

        if (i == j){
            return 1;
        }else if (i != 3 && j != 3){
            return 0;
        }else {
            return -1;
        }
    }
import re


def solution(a: str, b: str) -> int:
    # 对入参拼接成一个字符串
    a_b = str(a) + str(b)
    # 生成正则表达式
    # 表达式1:匹配大写字母
    compile_upper = re.compile(r"[A-Z]")
    # 表达式2:匹配小写字母
    compile_lower = re.compile(r"[a-z]")
    # 表达式3:匹配大小写字母
    compile_letter = re.compile(r"[a-zA-Z]")

    # findall:在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果有多个匹配模式,则返回元组列表,如果没有找到匹配的,则返回空列表
    # 通过判断匹配到的列表长度,判断是否同为大小写
    if len(compile_letter.findall(a_b)) == 2:
        if len(compile_upper.findall(a_b)) > 1 or len(compile_lower.findall(a_b)) > 1:
            return 1
        else:
            return 0
    else:
        return -1


assert solution("a", "g") == 1
assert solution("A", "G") == 1
assert solution("a", "G") == 0
assert solution("0", "?") == -1

def solution(a: str, b: str) -> int:
    return (int(a.islower() ^ b.islower())+1)%2 if a.isalpha() and b.isalpha() else -1
def solution(a: str, b: str) -> int:
    # 判断a和b的返回 a.isalpha, b.isalpha
    # 如果 string 至少有一个字符并且所有字符都是字母则返回 True,否则返回 False
    if a.isalpha() or b.isalpha():
        # a.isupper, b.isupper 如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False
        if a.isupper() == b.isupper():
            return 1
        else:
            return 0
    else:
        return -1


assert solution("a", "g") == 1
assert solution("A", "G") == 1
assert solution("a", "G") == 0
assert solution("0", "?") == -1
def solution(a: str, b: str) -> int:
    if a.isalpha() and b.isalpha():
        if (a.islower() and b.islower()) or (a.isupper() and b.isupper()):
            return 1
        else:
            return 0
    else:
        return -1

assert solution("a", "g") == 1
assert solution("A", "G") == 1
assert solution("a", "G") == 0
assert solution("0", "?") == -1
关闭