How do I generate all possible combinations of a string with spaces between the characters?
[in]: "foobar"
[out]:
['foobar', 'f oobar', 'fo obar', 'f o obar', 'foo bar', 'f oo bar', 'fo o bar',
'f o o bar', 'foob ar', 'f oob ar', 'fo ob ar', 'f o ob ar', 'foo b ar',
'f oo b ar', 'fo o b ar', 'f o o b ar', 'fooba r', 'f ooba r', 'fo oba r',
'f o oba r', 'foo ba r', 'f oo ba r', 'fo o ba r', 'f o o ba r', 'foob a r',
'f oob a r', 'fo ob a r', 'f o ob a r', 'foo b a r', 'f oo b a r', 'fo o b a r',
'f o o b a r', 'foobar', 'f oobar', 'fo obar', 'f o obar', 'foo bar',
'f oo bar', 'fo o bar', 'f o o bar', 'foob ar', 'f oob ar', 'fo ob ar',
'f o ob ar', 'foo b ar', 'f oo b ar', 'fo o b ar', 'f o o b ar', 'fooba r',
'f ooba r', 'fo oba r', 'f o oba r', 'foo ba r', 'f oo ba r', 'fo o ba r',
'f o o ba r', 'foob a r', 'f oob a r', 'fo ob a r', 'f o ob a r', 'foo b a r',
'f oo b a r', 'fo o b a r', 'f o o b a r']
To add spaces between the characters of a string: Call the join() method on a string containing a space. Pass the string as an argument to the join method. The method will return a string where the characters are separated by a space.
Python isspace() method is used to check space in the string. It returna true if there are only whitespace characters in the string. Otherwise it returns false. Space, newline, and tabs etc are known as whitespace characters and are defined in the Unicode character database as Other or Separator.
The formula for combinations is generally n! / (r! (n -- r)!), where n is the total number of possibilities to start and r is the number of selections made.
import itertools as it
def func(s):
if not s:
return [s]
binary = it.product(['',' '], repeat=len(s)-1)
zipped = (it.izip_longest(s , comb, fillvalue='') for comb in binary)
return [''.join(it.chain.from_iterable(x)) for x in zipped]
func('foobar')
output:
['foobar',
'fooba r',
'foob ar',
'foob a r',
'foo bar',
'foo ba r',
'foo b ar',
'foo b a r',
'fo obar',
'fo oba r',
'fo ob ar',
'fo ob a r',
'fo o bar',
'fo o ba r',
'fo o b ar',
'fo o b a r',
'f oobar',
'f ooba r',
'f oob ar',
'f oob a r',
'f oo bar',
'f oo ba r',
'f oo b ar',
'f oo b a r',
'f o obar',
'f o oba r',
'f o ob ar',
'f o ob a r',
'f o o bar',
'f o o ba r',
'f o o b ar',
'f o o b a r']
from itertools import product
text = "foobar"
L = [''.join(reversed(x)).rstrip()
for x in product(*[(c, c+' ') for c in reversed(text)])]
print L
['foobar', 'f oobar', 'fo obar', 'f o obar', 'foo bar', 'f oo bar', 'fo o bar', 'f o o bar', 'foob ar', 'f oob ar', 'fo ob ar', 'f o ob ar', 'foo b ar', 'f oo b ar', 'fo o b ar', 'f o o b ar', 'fooba r', 'f ooba r', 'fo oba r', 'f o oba r', 'foo ba r', 'f oo ba r', 'fo o ba r', 'f o o ba r', 'foob a r', 'f oob a r', 'fo ob a r', 'f o ob a r', 'foo b a r', 'f oo b a r', 'fo o b a r', 'f o o b a r', 'foobar', 'f oobar', 'fo obar', 'f o obar', 'foo bar', 'f oo bar', 'fo o bar', 'f o o bar', 'foob ar', 'f oob ar', 'fo ob ar', 'f o ob ar', 'foo b ar', 'f oo b ar', 'fo o b ar', 'f o o b ar', 'fooba r', 'f ooba r', 'fo oba r', 'f o oba r', 'foo ba r', 'f oo ba r', 'fo o ba r', 'f o o ba r', 'foob a r', 'f oob a r', 'fo ob a r', 'f o ob a r', 'foo b a r', 'f oo b a r', 'fo o b a r', 'f o o b a r']
Here's an implementation of my recursive idea above:
def string_spaces(s):
ret = set([s]) # use a set rather than a list to prevent duplicates
for i in range(1, len(s)):
for fst in string_spaces(s[:i]):
for snd in string_spaces(s[i:]):
ret.add(fst + ' ' + snd)
return ret
Example:
In [11]: string_spaces('foo')
Out[11]: set(['foo', 'f o o', 'f oo', 'fo o'])
NB: Python has a recursion limit of 1000 stack frames, so this will crash for very long strings (longer than 1000 characters).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With