Words combinations without repetition


 
Thread Tools Search this Thread
Top Forums Programming Words combinations without repetition
# 1  
Old 03-02-2011
Words combinations without repetition

How can I get all combinations of 5 words from 10 words.
For example I have 3 words and I want to get all combinations of 2 words.
"A", "B", "C" it would like AB, BC, AC.
Maybe you know some usefull code or example.
Thanx a lot.
P.S. Sorry if I'm not right enough cause I don't know English very good.
# 2  
Old 03-04-2011
With Perl and the CPAN module Math::Combinatoric:

Code:
% perl -MMath::Combinatorics -le'
  @n = qw(A B C);
  print join ",",
    map { join "", @$_ }
  combine 2, @n
  '
AB,AC,BC

# 3  
Old 03-05-2011
Actually,I think "AB" and "BA" should be treat as two different conditions.If so,my code shows how to do this(produce 2 words from 3 words).While if not,before doing the count(),you can sort the oss.str() to make sure there is only "AB" or "BA" in the set.
Code:
#include <iostream>
#include <sstream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <list>
#include <set>

using namespace std;


int main()
{
	string ch[] = {"THIS","IS","OK"};
	vector<string> vec(ch,ch+3);
	set<string> st;

	sort(vec.begin(),vec.end());

	do{
		ostringstream oss;
		vector<string>::iterator pos = vec.begin();	
		oss<<*pos<<*(pos+1);
		if(!st.count(oss.str()))
			st.insert(oss.str());
	}while(next_permutation(vec.begin(),vec.end()));
	
	copy(st.begin(),st.end(),ostream_iterator<string>(cout,"\n"));
}


Last edited by homeboy; 03-05-2011 at 11:44 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Adding same value to variables in does each repetition of command

So, I have this command: mkdir rolled for %%x in (*gif) do convert %%x -roll +2+6 %%x|move %%x rolled I'd like to have the +2 and +6 accumulate here. In each new gif tackled, it should increase by the amount: +2 (for x) and +6 (for y) Is this possible? I'm on Windows, DOS. (0 Replies)
Discussion started by: pasc
0 Replies

2. UNIX for Beginners Questions & Answers

Repetition in a particular interval

Suppose I have a word which is repeating in a string continuously. I have a set of intervals. Then how do I find the number occurrences of that word in those intervals and their location of occurrences. For example - Suppose there is a huge string anfie.......sirn of 10000 letters. Now the word... (2 Replies)
Discussion started by: ANKIT ROY
2 Replies

3. Shell Programming and Scripting

repetition calculation

cat mylist First_NAME Gender Mike M Sara F Raya M Sara F Fibi F Mike M Mike M Micheal M can someone please help me to get a script to cacluate the number of repetions for each (First name... (3 Replies)
Discussion started by: Sara_84
3 Replies

4. UNIX for Advanced & Expert Users

Counting the number of repetition of a pattern

the o/p of a command is -bash-2.05b# grep -o a * cc:a dd:a a dd:a office:a a a a a a a a a a a a a a (2 Replies)
Discussion started by: an2up
2 Replies

5. UNIX for Dummies Questions & Answers

Syntax Help | unix | grep | regular expression | repetition

Hello, This is my first post so, Hello World! Anyways, I'm learning how to use unix and its quickly become apparent that a strong foundation in regular expressions will make things easier. I'm not sure if my syntax is messing things up or my logic is messing things up. ps -e | grep... (4 Replies)
Discussion started by: MykC
4 Replies

6. Shell Programming and Scripting

Count the repetition of a Field in File

Hi, Thanks for keeping such a help-full platform active and live always. I am new to this forum and to unix also. Want to know how to count the repetition of a field in a file. Anything of awk, sed, perl, shell script, solution are welcomed. Input File------------------ abc,12345 pqr,51223... (10 Replies)
Discussion started by: indian.ace
10 Replies

7. Shell Programming and Scripting

How to check the repetition values in a file using bourne shell

Hi all, I have a scenario, like consider a file abc.txt, inside abc.txt, the contents is value1 = aaa, value2 = bbb, value3 = ccc, value1 = ddd. In this situation i need to throw an error for the repeatation of keys like "value1" is repeating twice. how to handle this using bourne... (1 Reply)
Discussion started by: Nandagopal
1 Replies

8. Shell Programming and Scripting

Looping/Repetition in Batch files

Hi All, I'm just new to UNIX, does anyone know how to create a batch file in UNIX that does the following routines: 1.) process multiple files in a directory in DOS, I set my sample input file as: set INPUTFILE=%1 in UNIX>> ???? 2.) every file to be processed by executing a program... (2 Replies)
Discussion started by: kimpot7268
2 Replies

9. UNIX for Advanced & Expert Users

Looping/Repetition in Batch files

Hi All, I'm just new to UNIX, does anyone know how to create a batch file in UNIX that does the following routines: 1.) process multiple files in a directory in DOS, I set my sample input file as: set INPUTFILE=%1 in UNIX>> ???? 2.) every file to be processed by executing a program... (1 Reply)
Discussion started by: kimpot7268
1 Replies

10. UNIX for Dummies Questions & Answers

Random numbers without repetition

Is anyone know some scripts to generate random number without repetition using bash; for example generate 10 different random numbers. Thanks (8 Replies)
Discussion started by: asal_email
8 Replies
Login or Register to Ask a Question