Feijó Posted March 4, 2014 at 06:58 PM Report #547524 Posted March 4, 2014 at 06:58 PM Uma duvida num problema: You are given a string with words and numbers separated by whitespaces (one space). The words contains only letters. You should check if the string contains three words in succession. Hints: You can easily solve this task with these useful functions: str.split, str.isalpha and str.isdigit. Input: A string with words. Output: True or False, a boolean. def checkio(words): s= str.split(words) vx=0 for i in s: vx=vx+1 if str.isdigit(i) == True: return False if vx < 3: return False else: return True o problema é que eles usam para testar, strings com unicode. if __name__ == '__main__': assert checkio(u"Hello World hello") == True, "Hello" assert checkio(u"He is 123 man") == False, "123 man" assert checkio(u"1 2 3 4") == False, "Digits" assert checkio(u"bla bla bla bla") == True, "Bla Bla" assert checkio(u"Hi") == False, "Hi" alguma ideia como resolver ? obrigado.
Rui Carlos Posted March 9, 2014 at 06:20 PM Report #547987 Posted March 9, 2014 at 06:20 PM Convinha indicar o problema que o código dava, em particular a mensagem de erro que obtinhas. Se estás a passar argumentos unicode, deverás usar as funções do unicode e não do str. def checkio(words): s = unicode.split(words) vx=0 for i in s: vx=vx+1 if unicode.isdigit(i) == True: return False if vx < 3: return False else: return True Uma forma ainda melhor de resolver o problema seria usando a notação de objectos, pois passa a funcionar para ambos os tipos ao mesmo tempo. def checkio(words): s = words.split() vx=0 for i in s: vx=vx+1 if i.isdigit() == True: return False if vx < 3: return False else: return True Rui Carlos Gonçalves
Feijó Posted March 9, 2014 at 11:02 PM Author Report #548006 Posted March 9, 2014 at 11:02 PM utilizei a segunda maneira e já consegui resolver o problema, muito obrigado 😄
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now