break-the-ice-with-python icon indicating copy to clipboard operation
break-the-ice-with-python copied to clipboard

Q14 Improved Solution

Open Euan-J-Austin opened this issue 1 year ago • 0 comments

Each letter and digit we use has a corresponding unique Unicode integer value. We can get this value using Python's built-in ord() function. Knowing which integers represent upper- and lower-case letters, which represent the digits 0 through 9 we can then use the len() function and list comprehension to determine the number of letters and digits. My solution avoids unwieldy if else conditions etc.

Thoughts?

def count_ld(x): return print(f'LETTERS {len([v for v in x if ord(v) in range(65, 91) or ord(v) in range(97,123)])}\nDIGIT {len([v for v in x if ord(v) in range(48,54)])}')

count_ld(input("ENTER: "))

Euan-J-Austin avatar Nov 30 '23 22:11 Euan-J-Austin