Sollicitatievraag bij Bloomberg

Remove duplicate consecutive spaces from a given string, in-place. For example: "Have a nice day" should become "Have a nice day". Removal of leading and trailing spaces was not specifically mentioned.

Antwoorden op sollicitatievragen

Anoniem

25 jul 2017

foo - Using a recursive function foo2 - Using an iterative process def foo(s): if len(s) = 2: if s[i] == s[i+1] == " ": s = s[:i] + s[i+1:] else: i += 1 return s

Anoniem

25 jul 2017

The iterative process: def foo2(s): if len(s) = 2: if s[i] == s[i+1] == " ": s = s[:i] + s[i+1:] else: i += 1 return s

Anoniem

25 jul 2017

Recursive solution: def foo(s): if len(s) < 2: return s if s[0] == s[1] == " ": return foo(s[1:]) return s[0] + foo(s[1:])