-1

https://codewars.com/kata/5dd462a573ee6d0014ce715b

Write a function that will check if two given characters are the same case.

  • If either of the characters is not a letter, return -1
  • If both characters are the same case, return 1
  • If both characters are letters, but not the same case, return 0

Examples

'a' and 'g' returns 1
'A' and 'C' returns 1
'b' and 'G' returns 0
'B' and 'g' returns 0
'0' and '?' returns -1

char1 = 'G'
char2 = 'a'

def is_same_case(char1,char2):
    if char1.isupper() and char2.isupper or char1.islower() and char2.islower:
        return 1
    elif char1.isupper() and char2.islower or char1.islower() and char2.isupper:
        return 0
    else:
        return -1

print(is_same_case(char1,char2))
print(char1.isupper())
print(char2.isupper())

It returns 1 instead of 0.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
GrainsGames
  • 89
  • 1
  • 5

1 Answers1

2

You can utilize the isalpha() method to test cases that aren't letters, and then perform a comparison of the isupper() method, and convert the boolean to an integer

def is_same_case(char1, char2):
    if not (char1.isalpha() and char2.isalpha()):
        return -1
    return int(char1.isupper() == char2.isupper())
oskros
  • 3,101
  • 2
  • 9
  • 28