Jump to content

Recommended Posts

Posted

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.

Posted

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.