Pergunta de entrevista da empresa Uber

Input a string and output the number of words (need to run on coderpad)

Respostas da entrevista

Sigiloso

27 de abr. de 2015

str.replace( / +/g, ' ' ).split(' ').length Faster would be with a loop and no regex. But that depends on large your str is

Sigiloso

28 de set. de 2019

var t = input.trim //eliminates any extra spaces at the start of the sentences,which will introduce //extra word count val r = t.split(' ').length println(r) Still this is incomplete in itself as this will fail in the case like " hello my name is xyz" as there are few extra spaces in between "my" and "name"

Sigiloso

1 de jun. de 2015

def count_words(str): return len(str.split())