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 8946583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:33:29+00:00 2026-06-15T12:33:29+00:00

I have a file: test.txt with a sentence on each line. Hello World 99

  • 0

I have a file: test.txt with a sentence on each line.

Hello World  
99 Bottles of Beer  
Humpty Dumpty Sat on the wall

I wish to produce an output displaying all combinations of the input from this file (i.e. 2n-1 combinations). In the above instance, the algorithm will spill out the following – Each combination to be separated with a pipe (|)

Hello World  
99 Bottles of Beer  
Humpty Dumpty Sat on the wall  
Hello World | 99 Bottles of Beer  
Hello World | Humpty Dumpty Sat on the wall  
99 Bottles of Beer | Humpty Dumpty Sat on the wall  
Hello World | 99 Bottles of Beer | Humpty Dumpty Sat on the wall  

Ideally, I would like this to be done in bash or python or perl scripting but I am open to suggestions.

  • 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-06-15T12:33:30+00:00Added an answer on June 15, 2026 at 12:33 pm
    import itertools
    
    l = [s.strip() for s in open('test.txt')]
    
    for i in range(len(l)):
      print '\n'.join(map(' | '.join, itertools.combinations(l, i + 1)))
    

    produces

    Hello World
    99 Bottles of Beer
    Humpty Dumpty Sat on the wall
    Hello World | 99 Bottles of Beer
    Hello World | Humpty Dumpty Sat on the wall
    99 Bottles of Beer | Humpty Dumpty Sat on the wall
    Hello World | 99 Bottles of Beer | Humpty Dumpty Sat on the wall
    

    If you don’t like the style of '\n'.join() (I am not sure I do), you could replace it with an explicit loop:

    for i in range(len(l)):
      for c in map(' | '.join, itertools.combinations(l, i + 1)):
        print c
    

    This is slightly more verbose, but is more economical.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file called test.txt like that: hello world it is, just a
so i have ruby script that simply writes to a test.txt file with hello
Well, I have a file test.txt #test.txt odsdsdoddf112 test1_for_grep dad23392eeedJ test2 for grep Hello
Say I have this file: cat > test.txt <<EOF line one word line two
I have a file test.txt which has an array: array = [3,5,6,7,9,6,4,3,2,1,3,4,5,6,7,8,5,3,3,44,5,6,6,7] Now what
I have a small file test.txt contains a Long number inside while another piece
I have one file (for example: test.txt), this file contains some lines and for
I need to read test.TXT file(tab delimited) into MATLAB. TXT file have form: Datum
I have a text file test.txt with the following content: text1 text2 And I
I have a text file named test.txt I want to write a C program

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.