Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 30th, 2003, 03:54 PM
milzzy milzzy is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 1 milzzy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Finding all possible combos?

Hi group,

What I want to do is figure out the formula needed to output all possible combonations of an inputted string. For example:

Original string: 12345

Thus, I want to come up with a way to display all combonations of those five numbers (12354, 12534, 15234, 51234, etc..) without ever repeating the same combonation or having the same number twice in any new combonation. I have really thought hard on this for the past few weeks, but haven't yet found a solution that works. It seems like the solution isn't that hard to figure out, but I just can't get it right. If anyone can just give me the general idea on what to do (For...Loops, variable increments, etc.) I'd greatly appreciate it!

Reply With Quote
  #2  
Old July 30th, 2003, 05:07 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 42 m 50 sec
Reputation Power: 185
This is called "permutation", iirc it is a *very* simple recursive algorithm and you should find a lot about it on google.

The general idea is to use a recursive function and make it run only over half of the possible results. The "towers of hanoi" problem iirc resolves to the same algorithm.

hth,
M.
__________________
--
Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more.

Reply With Quote
  #3  
Old July 30th, 2003, 06:49 PM
dog135's Avatar
dog135 dog135 is offline
Doggie
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Location: Seattle, WA
Posts: 751 dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 10 h 38 m 25 sec
Reputation Power: 7
Off the top of my head:
Code:
pseudo code:

list lst
int pos

for(pos=0 to length(lst)){
	doit(lst,pos)
)

function doit(lst,pos){
	list sublist
	int subpos
	if(length(lst)==1){
		print lst[0]
	} else {
		sublist=lst-char@(pos)
		for(subpos=0 to length(sublist)){
			print lst[pos]
			doit(sublist,subpos)
		}
	}
}

Reply With Quote
  #4  
Old August 4th, 2003, 02:21 AM
Pi Man Pi Man is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 27 Pi Man User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 10 sec
Reputation Power: 0
Ever heard of a factorial?

All you have to do is multiply the number of things being arranged (say 5) times the number - 1 times the number - 2 times the number - 3. So it would be (in the case of there being 5 things being arranged) 5*4*3*2 (*1, but it makes no difference). There is a factorial built into many programming languages. In some, you can just say "m = 5!" for "m = 5*4*3*2*1" But you can always use a loop. Note: It may be easier, conceptually, if you use 1*2*3*4*5 because you get to do it "forwards."

Reply With Quote
  #5  
Old August 4th, 2003, 02:25 AM
Pi Man Pi Man is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 27 Pi Man User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 10 sec
Reputation Power: 0
Oops. I thought you meant you needed to know the *number* of possible combinations.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > Finding all possible combos?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT