DEV Community

Discussion on: Daily Challenge #270 - Fix String Case

Collapse
 
agtoever profile image
agtoever • Edited

Python solution. Uses helper function that counts the matching elements in a list. I use that to compare the string to itself converted to upper- and lowercase.

TIO link

def matching(l1, l2):
    # Returns the number of matching corresponding elements of l1 and l2
    return sum(e1 == e2 for e1, e2 in zip(l1, l2))

def solve(s):
    return s.lower() if matching(s, s.upper()) <= matching(s, s.lower()) else s.upper()

cases = ["coDe", "CODe", "coDE", "code", "CODe", "COde", "Code"]
for case in cases:
    print(f'{case} -> {solve(case)}')