473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

generating permutations and combinations...

103 100+
Hi

here's a test of logic for mathmeticians (I do have an actual use for this too...)

I need to split a 4 or 5 word phrase into all possible combinations of the individual words:

eg:

4
Post your question in
3
Post your question
post question in
post your in
your question in
2
post your
post question
post in
your question
your in
question in
1
post
your
question
in


It is a matter of logic but I wondered if anyone's already solved it before I spend another 2 hours working it out...

Thanks in advance!

Henry
Feb 28 '08 #1
10 8914
ronverdonk
4,258 Expert 4TB
This is a PHP programming forum, not a logic puzzle solving forum. So show the PHP code you have done so far to get to the solution. And put it between coding tags.

Ronald
Feb 28 '08 #2
henryrhenryr
103 100+
I'm not sure about the difference between programming and logic? Maybe you can enlighten me?

In the meantime, I have been working on it for the past 90 mins and coming to this.

Expand|Select|Wrap|Line Numbers
  1. $phrase="A B C D E";
  2. $words=explode(' ',$phrase);
  3. $c=count($words);
  4. $out=array();
  5. for ($i=1;$i<=$c;$i++) {
  6.   if ($i==1) {
  7.     foreach ($words as $w) { 
  8.       $out[$i][]=$w; 
  9.     }
  10.   }
  11.   if ($i==2) {
  12.     for ($j=0;$j<$c;$j++) {
  13.       $next=$j+1;
  14.       if ($next>=$c) {
  15.         $next=$next-$c;
  16.       }
  17.       $out[$i][]= "{$words[$j]} {$words[$next]}";
  18.     }
  19.     if (($c-$i)>1) {
  20.       for ($j=0;$j<$c;$j++) {
  21.         $next=$j+2;
  22.         if ($next>=$c) {
  23.           $next=$next-$c;;
  24.         }
  25.         $out[$i][]= "{$words[$j]} {$words[$next]}";
  26.       }
  27.     }
  28.   }
  29. }
  30.  
Feb 28 '08 #3
henryrhenryr
103 100+
Yup...took exactly 2 hours...

Could be nicer. Works for up to 5 words. Works for more but then starts to miss some combinations. The sequence is like pascal's triangle if anyone's interested. Maybe there's a logical programming forum where such problems are welcome?

Expand|Select|Wrap|Line Numbers
  1. $phrase="A B C D E";
  2. $words=explode(' ',$phrase);
  3. $c=count($words);
  4. $out=array();
  5. for ($i=1;$i<$c;$i++) {
  6.   if ($i==1) {
  7.     foreach ($words as $w) {
  8.       $out[$i][]=$w; 
  9.     }
  10.   }
  11.   else {
  12.     for ($j=0;$j<$c;$j++) {
  13.       $z=array();
  14.       for ($k=1;$k<=$i;$k++) {
  15.         $z[$k]=$j+$k-1;
  16.         if ($z[$k]>=$c) {
  17.           $z[$k]-=$c;
  18.         }
  19.       }
  20.       $temp=NULL;
  21.       foreach ($z as $index) {
  22.         $temp .= $words[$index].' ';
  23.       }
  24.       $out[$i][]=trim($temp);
  25.     }
  26.     if (($c-$i)>1) {
  27.       for ($j=0;$j<$c;$j++) {
  28.         $z=array();
  29.         $s=0;
  30.         for ($k=1;$k<=$i;$k++) {
  31.           $z[$k]=$j+$k-1+$s;
  32.           if ($z[$k]>=$c) {
  33.             $z[$k]-=$c;
  34.           }
  35.           $s++;
  36.         }
  37.         $temp=NULL;
  38.         foreach ($z as $index) {
  39.           $temp .= $words[$index].' ';
  40.         }
  41.         $out[$i][]=trim($temp);
  42.       }
  43.     }
  44.   }
  45. }
  46.  
prints

A
B
C
D
E

A B
B C
C D
D E
E A
A C
B D
C E
D A
E B

A B C
B C D
C D E
D E A
E A B
A C E
B D A
C E B
D A C
E B D

A B C D
B C D E
C D E A
D E A B
E A B C
Feb 28 '08 #4
hsriat
1,654 Expert 1GB
Wait... I'm working on a general one.

Just few minutes plz...
Feb 28 '08 #5
hsriat
1,654 Expert 1GB
Woo!!
Done in approx 2 hours!!
No limit for number of words!! (until memory utilization is fine)[php]<?php
$string = "one two three";
$word_array = explode(" ",$string);
for ($i=count($word_array); $i>=1; $i--)
{
$word = $i == 1 ? " word" : " words";
echo "Strings containing combination of ".$i.$word.":<br>";

$flags = array();
$combination_flags = get_combination_flags(count($word_array), $i);
$flags_groups = explode(",",$combination_flags);
foreach ($flags_groups as $val)
{
$temp = chunk_split($val,1,".");
array_push($flags, explode(".", substr($temp,0,(strlen($temp)-1))));
}
for ($j=0; $j<count($flags); $j++)
{
$new_string = "";
for ($k=0; $k<count($word_array); $k++)
$new_string .= $flags[$j][$k] ? " ".$word_array[$k] : "";
trim ($new_string);
echo $new_string."<br>";
}
echo "<br>";
}

function get_combination_flags($total, $required)
{
$flag_array = array();
$bin = "";
for ($i=0; $i<$total; $i++)
$bin .= "1";
$dec = bindec($bin);
for ($i=0; $i<=$dec; $i++)
{
$num_of_chars = count_chars(decbin($i), 1);
if ($num_of_chars[49]==$required)
{
$temp = decbin($i);
$length = strlen($temp);
$pre = "";
if ($length<$total) for ($j=$length; $j<$total; $j++) $pre .= "0";
$temp = $pre.$temp;
array_push($flag_array,$temp);
}
}
return implode(",",$flag_array);
}
?>[/php]

Hope you will use it... :)

- Harpreet
Feb 28 '08 #6
hsriat
1,654 Expert 1GB
Output of the above is:
Expand|Select|Wrap|Line Numbers
  1. ... removed ...
Feb 28 '08 #7
henryrhenryr
103 100+
Hey Harpreet

That's genious! Thanks very much - I was going to settle for the 5 word one but this is really fantastic - makes my system work really nicely.

I'm not 100% sure how you got the get_combination_flags to do what it does. I'll keep trying to work it out though... really helps me learn new methods too...

Thanks again!

Henry
Feb 29 '08 #8
hsriat
1,654 Expert 1GB
Hey Harpreet

That's genious! Thanks very much - I was going to settle for the 5 word one but this is really fantastic - makes my system work really nicely.

I'm not 100% sure how you got the get_combination_flags to do what it does. I'll keep trying to work it out though... really helps me learn new methods too...

Thanks again!

Henry
I love to solve Mathematical Puzzles!

I'm glad to know that it really helped you!! :)

And as far as that get_combination_flags() is concerned, look at the structure of binary numbers. You will get it quickly.
Feb 29 '08 #9
henryrhenryr
103 100+
Hi Harpreet

I've adapted it into my code and it works a dream. I'm using it to create SQL queries which count the number of occurances of any combination of words input and order them... Let me know if you're interested in the code...

Understand the binary more now - it's a good solution.

Thanks again!

Henry
Feb 29 '08 #10
hsriat
1,654 Expert 1GB
Hi Harpreet

I've adapted it into my code and it works a dream. I'm using it to create SQL queries which count the number of occurances of any combination of words input and order them... Let me know if you're interested in the code...

Understand the binary more now - it's a good solution.

Thanks again!

Henry
Right now I don't need anything like that. Will tell you if I'll need.

You might be doing something unique. :p
Feb 29 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
20
by: John Trunek | last post by:
I have a set of X items, but want permutations of length Y (X > Y). I am aware of the permutation functions in <algorithm>, but I don't believe this will do what I want. Is there a way, either...
2
by: Alex Vinokur | last post by:
Does STL contain algorithms which generate (enable to generate) exponents, permutations, arrangements, and combinations for any numbers and words? -- Alex Vinokur mailto:alexvn@connect.to...
0
by: Alex Vinokur | last post by:
C++ program for generating combinatorial objects (exponents, permutations, arrangements, combinations for any numbers and words) can be downloaded at...
4
by: pauljwilliams | last post by:
Hi there, I'm looking for an algorithm that will generate combinations in the following way: Given a lower bound, and an upper bound, generate a set of size n containing all combinates of...
8
by: Mir Nazim | last post by:
Hello, I need to write scripts in which I need to generate all posible unique combinations of an integer list. Lists are a minimum 12 elements in size with very large number of possible...
0
by: JosAH | last post by:
Greetings, last week I stated that a lot of topics in a lot of forums mention all sorts of sorting problems. Last week's tip gave an answer to quite a bit of those sorting problems. Another...
6
by: py_genetic | last post by:
Hi, I'm looking to generate x alphabetic strings in a list size x. This is exactly the same output that the unix command "split" generates as default file name output when splitting large...
14
by: bjorklund.emil | last post by:
Hello pythonistas. I'm a newbie to pretty much both programming and Python. I have a task that involves writing a test script for every possible combination of preference settings for a software...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.