Reputation: 39
I'm in desperate need of help, I've been coding for a little bit now but have been stuck for a while with a problem.
My function has to check if a given word consists of special characters and if the length of the word without the special characters is bigger than 3.
The special characters are: ()1234567890!?_@#$%^&*.,'
The function has to return True if there are no special characters in the word and the length of the word without the special characters is bigger than 3.
The one exception here however is that the special characters are allowed to be in the end of a word.
My idea was to turn the initial word and special characters into a list, then check if the last character is "special". (If i remove all the special characters from the end I wont have to worry about them anymore, since they are allowed anyway.) Then check again if the word consists of specials, if it does > return False. If it does not contain special characters and len(list) is > 3 then return True and else: return False (Due to the fact that that len(list) is <= 3.)
Looking over my code I just can't see why my tests are failing.
def check_word(word: str) -> bool:
symbols = "()1234567890!?_@#$%^&*.,'"
symbols_list = list(symbols)
word_list = list(word)
while word_list[-1] in symbols_list:
del word_list[-1]
for char in word_list:
if char in symbols_list:
return False
elif char not in symbols_list and len(word_list) > 3:
return True
else:
return False
The tests that keep failing are:
print(check_word("te1234et1234")) # False (Mine returns True)
print(check_word("mar132gus")) # False (Mine returns True)
Everything else seems to be in order:
print(check_word("jaanus123")) # True
print(check_word("123sander")) # False
print(check_word("123joonas123")) # False
Is there any reason why these tests are failing? What am I missing?
//Edit2 So as a user kindly pointed out, I went ahead and used the debugger on my code and the problem seems to be that when I step into my initial "for" loop it checks only if the first character in my list is a special character, not if there is a special character in the whole list, could anyone lead me to the right track on how to make the loop go through the whole list to look for specials?
Upvotes: 0
Views: 62
Reputation: 1943
Try this
def check_word(word):
symbols = list("()1234567890!?_@#$%^&*.,'")
word_list = list(word)
while word_list[-1] in symbols:
word_list.pop()
print word_list
temp="".join(word_list)
actuallength=0
for i in temp:
if i in symbols:
return False
else:
actuallength+=1
if (actuallength >3):
return True
else:
return False
Output
>>> check_word("jaanus123")
['j', 'a', 'a', 'n', 'u', 's']
True
>>> check_word("123sander")
['1', '2', '3', 's', 'a', 'n', 'd', 'e', 'r']
False
>>> check_word("123joonas123")
['1', '2', '3', 'j', 'o', 'o', 'n', 'a', 's']
False
>>> check_word("te1234et1234")
['t', 'e', '1', '2', '3', '4', 'e', 't']
False
>>> check_word("mar132gus")
['m', 'a', 'r', '1', '3', '2', 'g', 'u', 's']
False
>>>
Upvotes: 0
Reputation: 36662
You do not need to transform strings to lists, strings are iterable:
def check_word(word: str) -> bool:
symbols = "()1234567890!?_@#$%^&*.,'"
end = len(word)
while word[end-1] in symbols:
end -= 1
if len(word[:end]) < 3:
return False
for char in word[:end]:
if char in symbols:
return False
return True
def tests():
assert check_word("jaanus123") is True
assert check_word("123sander") is False
assert check_word("123joonas123") is False
assert check_word("te1234et1234") is False
assert check_word("mar132gus") is False
print("***all tests pass***")
tests()
***all tests pass***
Upvotes: 2
Reputation: 21
first char in ['m', 'a', 'r', '1', '3', '2', 'g', 'u', 's']
is m
and it is not in symbol_list and whole list length is 9, so the function return True.
so the problem is in this statement len(word_list) > 3
, the length is not about the word without the special characters
Upvotes: -1