Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7011935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:11:26+00:00 2026-05-27T22:11:26+00:00

I need a function which returns subsegments for a given segment. For example, sub_combinations(ABCD)

  • 0

I need a function which returns subsegments for a given segment. For example, sub_combinations("ABCD") should yield:

("A", "B", "C", "D")
("A", "B", "CD")
("A", "BC", "D")
("A", "BCD")
("AB", "C", "D")
("AB", "CD")
("ABC", "D")
("ABCD")
("ABD", "C") *
("AC", "BD") *
("AC", "B", "D") *
("ACD", "B") *
("AD", "BC") *
("AD", "B", "C") *

("A","C","B","D") is not valid since it is not in sequence order. In other words, ("A","B","C","D") is instead correct.

("AC", "B", "D") is valid since “C” follows “A” in sequential order, and “B” follows “AC”.

This is as far as I’ve gotten:

def sub_combinations(segment):
        for i in range(1, len(segment)):
                for j in sub_combinations(segment[i:]):
                        yield (segment[:i],) + j 
        yield (segment,) 

for i in sub_combinations("ABCD"):
        print(i)

('A', 'B', 'C', 'D')
('A', 'B', 'CD')
('A', 'BC', 'D')
('A', 'BCD')
('AB', 'C', 'D')
('AB', 'CD')
('ABC', 'D')
('ABCD',)

However this is missing those extra combinations.

Any suggestions on how to proceed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:11:26+00:00Added an answer on May 27, 2026 at 10:11 pm

    You may change your code as follows:

    def sub_combinations(segment):
      if len(segment) == 1:
        yield (segment,)
      else:
        for j in sub_combinations(segment[1:]):
          yield (segment[0],)+j
          for k in range(len(j)):
            yield (segment[0]+j[k],)+j[:k]+j[k+1:]
    

    If your segment contains only one character the result is quite easy. Otherwise split off the first character and determine all partitions of the rest of your string. Afterwards you have the following (distinct) solutions: the splitt-off character builds a separate tuple or you can add it to any of the tuples of your previous solution.

    Due to the recursive calls this method builds the solution set from the single character case up to the full argument.

    Your example case gives the following result:

    ('A', 'B', 'C', 'D')
    ('AB', 'C', 'D')
    ('AC', 'B', 'D')
    ('AD', 'B', 'C')
    ('A', 'BC', 'D')
    ('ABC', 'D')
    ('AD', 'BC')
    ('A', 'BD', 'C')
    ('ABD', 'C')
    ('AC', 'BD')
    ('A', 'B', 'CD')
    ('AB', 'CD')
    ('ACD', 'B')
    ('A', 'BCD')
    ('ABCD',)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a function which returns a string which should be unique
I need function which returns: for any number from range => result [0.001,0.01) =>
Suppose I need to implement factory function which returns object O which inherits/has members
I need to implement a function which returns a TDictionary, without specifying the exact
Problem: I need to write a function which returns a value for a input
I need a function which returns an array in random order. I want to
I need to write a function in JavaScript, which returns a state from calling
i m a newbie to haskell, currently i need a function 'f' which, given
I need to write a function which returns an list of sum functions, adding
I need to use a function which returns a table, in a inner join

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.