-5

I'm trying to turn a given string ("hello") to a list containing every list of substrings. for example:

[["hello"],["h,"ello"],["he","llo"],["hel","lo"],["hell","o"],\
["h","e","llo"],["h","e","l","lo"],["h","e","l","l","o],["he","l,"lo"],\
["hel","l,"o"],["hell","o]....etc....].

I understand the fastest way should be a recursion function, but i just can't get it right. something similar to:

x = "hello"
wordset=[]
string_div(0,x,wordset)
...
...
def string_div(i,word,wordset)
  wordset.append(wordset+[word[i:])
  ......
  
(This isn't like other questions posted before because I only want lists of substrings that when concatenated form the same original word) help would be appreciated! thanks
yairHas
  • 1
  • 3
  • Possible duplicate of https://stackoverflow.com/questions/22469997/how-to-get-all-the-contiguous-substrings-of-a-string-in-python – match Feb 11 '18 at 17:37
  • 1
    Possible duplicate of [How To Get All The Contiguous Substrings Of A String In Python?](https://stackoverflow.com/questions/22469997/how-to-get-all-the-contiguous-substrings-of-a-string-in-python) – Shahriar Feb 11 '18 at 17:43

1 Answers1

1

I believe this is not strictly a duplicate, and I provide an exact solution to your problem.

Solution

For a given string of length n, we shall obtain a unique and valid partition of the string for every binary string of length (n-1). For example: The string "coconut" and the binary string "001010" corresponds to the partition: ['coc', 'on', 'ut'] and the binary string "100101" corresponds to: ['c','oco','nu','t'].

Thus we can get the full list of partitions as you require, by taking all ((2^(n-1))-1) different partitions corresponding to the different binary sequences.

Implementation

import itertools
def get_list_partitions(string):
  partitions = []
  
  #list of binary sequences
  binary_sequences = ["".join(seq) for seq in itertools.product("01", repeat=len(string)-1)]

  #go over every binary sequence (which represents a partition)
  for sequence in binary_sequences:
    partition = []
    
    #current substring, accumulates letters until it encounters "1" in the binary sequence
    curr_substring = string[0]
    for i, bit in enumerate(sequence):
      #if 0, don't partition. otherwise, add curr_substring to the current partition and set curr_substring to be the next letter
      if bit == '0':                        
        curr_substring = curr_substring + string[i+1]
      else:
        partition.append(curr_substring)
        curr_substring = string[i+1]  
    
    #add the last substring to the partition
    partition.append(curr_substring)

    #add partition to the list of partitions
    partitions.append(partition)  

  return partitions 

  
print(get_list_partitions("coconut"))
      
Community
  • 1
  • 1
Amir Biran
  • 54
  • 1
  • 8