mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-10-29 22:17:23 +01:00
feat(solutions): slugify/python/function (#16)
This commit is contained in:
parent
df539a41c0
commit
a04240c0ab
3
challenges/slugify/solutions/python/function/README.md
Normal file
3
challenges/slugify/solutions/python/function/README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# slugify/python/function
|
||||||
|
|
||||||
|
Created by [@Mehln](https://github.com/Mehln) on 14 October 2022.
|
24
challenges/slugify/solutions/python/function/solution.py
Normal file
24
challenges/slugify/solutions/python/function/solution.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
string = input()
|
||||||
|
|
||||||
|
def isalphanum(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
|
||||||
|
|
||||||
|
string = string.strip(' ')
|
||||||
|
string = string.strip('-')
|
||||||
|
answer = ""
|
||||||
|
current = ""
|
||||||
|
|
||||||
|
for character in string:
|
||||||
|
if character == ' ' or (character == '-' and len(current)>0):
|
||||||
|
answer += current
|
||||||
|
answer += '-'
|
||||||
|
current = ""
|
||||||
|
elif isalphanum(character):
|
||||||
|
current += character
|
||||||
|
|
||||||
|
answer += current
|
||||||
|
|
||||||
|
print(answer)
|
Loading…
Reference in New Issue
Block a user