1

I have string s ='edsd'

I am trying to print the combination of string using python

my expected output is below

['e', 'ed', 'eds', 'edsd', 'd', 'ds', 'dsd', 's', 'sd', 'd']

l = []
j=0
while j < len(s):
    for i in range(len(s)):
        l.append(s[j:i + 1])
    j+= 1

My out is having blank string are coming

['e', 'ed', 'eds', 'edsd', '', 'd', 'ds', 'dsd', '', '', 's', 'sd', '', '', '', 'd']

I can try str_list = list(filter(None, l))

but my expectation is apply directly in the code, not wanted to apply again the filter after getting the output

Mak Hus
  • 27
  • 4
  • 2
    Your algorithm won't produce combinations. E.g. `es` is also a combination, but it's missing from your "expected output" and from your actual output – Alexey S. Larionov Apr 11 '23 at 10:11
  • 1
    I think this answer can help you -> https://stackoverflow.com/a/28870795 – Tugberk Demirtas Apr 11 '23 at 10:15
  • your expected output is missing many combinations. – Talha Tayyab Apr 11 '23 at 10:20
  • Your question confuses people because what you asked is not called _combinations_. It has been closed as a duplicate of the wrong question. – Jorge Luis Apr 11 '23 at 10:30
  • If you posted a new question, I would answer with this code: `l = [s[begin:end+1] for begin in range(len(s)) for end in range(begin, len(s))]`. The problem with your code is that you let `i` be lower than `j`. – Jorge Luis Apr 11 '23 at 10:39
  • Link to the question this one is a duplicate of: [Algorithm that generates all contiguous subarrays](https://stackoverflow.com/questions/64137236/) – Jorge Luis Apr 11 '23 at 10:47

0 Answers0