CodeWars:Valid Spacing
Challenge Name:
Valid Spacing
Challenge Description:
Write a python function called valid_spacing()
or validSpacing()
which checks if a string has valid spacing. The function should return either True
or False
.
Syntax for valid spacing is one space between words, and no leading or trailing spaces.Some examples of what the function should return.
Challenge link:
https://www.codewars.com/kata/5f77d62851f6bc0033616bd8/train/python
Examples:
'Hello world' = True
' Hello world' = False
'Hello world ' = False
'Hello world' = False
'Hello' = True
# Even though there are no spaces, it is still valid because none are needed
'Helloworld' = True
# True because we are not checking for the validity of words.
'Helloworld ' = False
' ' = False
'' = True
Code:
def valid_spacing(s):
if s.startswith(' ') or s.endswith(' '):
return False
return ' ' not in s
Explanation:
Check if s starts with a space or s ends with space ,if a space is found 'False' is returned.
Finally if double space if found return False else return True
If any suggestions and any mistakes please comment !!
0 Comments
Post a Comment