1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00
programming-challenges/challenges/slugify/solutions/python/function/solution.py

26 lines
691 B
Python
Raw Normal View History

string = input()
2022-12-30 00:55:21 +01:00
def is_alphanumeric(character: str) -> bool:
is_lowercase_letter = ord(character) >= ord('a') and ord(character) <= ord('z')
is_upper_letter = ord(character) >= ord('A') and ord(character) <= ord('Z')
is_digit = ord(character) >= ord('0') and ord(character) <= ord('9')
return is_upper_letter or is_lowercase_letter or is_digit
2022-12-30 00:55:21 +01:00
string = string.strip(' ').strip('-')
answer = ""
current = ""
for character in string:
2022-12-30 00:55:21 +01:00
if character == ' ' or (character == '-' and len(current) > 0):
answer += current
answer += '-'
current = ""
2022-12-30 00:55:21 +01:00
elif is_alphanumeric(character):
current += character
answer += current
print(answer)