473,799 Members | 3,822 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a good algo to do this

hi
i need to do something like this
eg given a number (as a string) = "123"
there are a few combination i want to find with this string, ie
"132","321","23 1","312" and "213". so there are 6 combinations
altogether. i remember there's a formula for this, but forgot. Does
python have any modules/functions to do this?

thanks

Mar 6 '06 #1
3 1457

ei***********@y ahoo.com wrote:
hi
i need to do something like this
eg given a number (as a string) = "123"
there are a few combination i want to find with this string, ie
"132","321","23 1","312" and "213". so there are 6 combinations
altogether. i remember there's a formula for this, but forgot. Does
python have any modules/functions to do this?

thanks


factorial = [ 1,
1,
2,
6,
24,
120,
720,
5040,
40320,
362880,
3628800,
39916800,
479001600,
6227020800,
87178291200,
1307674368000,
20922789888000,
355687428096000 ,
640237370572800 0 ]

def permutation( iterable ):
length = len(iterable)
count = factorial[length]
for i in range(count):
sequence = list(iterable[:])
index = i
N = count
result = []
for j in range( length, 0, -1):
N = N / j
choice, index = index // N, index % N
result += [ sequence.pop(ch oice) ]
yield result

a = [ ''.join(perm) for perm in permutation('12 3')]

print a

['123', '132', '213', '231', '312', '321']
Gerard

Mar 6 '06 #3
Gerard Flanagan wrote:
ei***********@y ahoo.com wrote:
hi
i need to do something like this
eg given a number (as a string) = "123"
there are a few combination i want to find with this string, ie
"132","321","23 1","312" and "213". so there are 6 combinations
altogether. i remember there's a formula for this, but forgot. Does
python have any modules/functions to do this?

thanks


factorial = [ 1,
1,
2,
6,
24,
120,
720,
5040,
40320,
362880,
3628800,
39916800,
479001600,
6227020800,
87178291200,
1307674368000,
20922789888000,
355687428096000 ,
640237370572800 0 ]

def permutation( iterable ):
length = len(iterable)
count = factorial[length]
for i in range(count):
sequence = list(iterable[:])
index = i
N = count
result = []
for j in range( length, 0, -1):
N = N / j
choice, index = index // N, index % N
result += [ sequence.pop(ch oice) ]
yield result

a = [ ''.join(perm) for perm in permutation('12 3')]

print a

['123', '132', '213', '231', '312', '321']


Which is actually not a good algorithm at all, having just tried
'123456' - it's better for randomly-accessing a particular permutation.
There's a better 'algo' here:

http://gflanagan.net/site/python/05/Johnson.html

for getting all perms, though there's no getting away from the
factorial - IIRC, NPermutation(7) took less than a minute,
NPermutation(8) took 2 minutes and NPermutation(9) took 10 minutes on
my machine.

Also, search this group for Jack Diederich and probstat.

HTH

Gerard

Mar 6 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9935
by: Christian Meisinger | last post by:
hi. has anyone tryed to check .htpasswd passwords with php? everythink works fine as long as they are encrypted with the default CRYPT mode of apaches htpasswd. crypt($pass, $htpass) === $htpass but how to check MD5? if you use ./htpasswd -m .htpasswd user
10
4266
by: tgh003 | last post by:
So lets assume I have a list of tasks in db table (table looks like: ID, task, sort) And I want to reorder the task list without updating all the records in the table. I dont want to run a sql update stmt against every record to update the "sort" field. Any other easy way to do this? there has got to be a simple algo out there.
1
1777
by: MJ | last post by:
Hi I am searching for the algo book "The Algorithm Design Manual" by Steve S. Skiena If any one can tell me where can I get the free soft copy of this book It will be rally useful for me Regards MJ
4
352
by: chellappa | last post by:
Hi Every body ! Any one Knows , very fastest ,accurate ,optimize finding word in a given string .. i have some algorthms ,,, but genious has good algorthm then mine............ i need this one..... by (c)hellappa
4
2850
by: Sean Kelly | last post by:
The old one, not the .NET version. I don't see it listed, but since it's available under .NET I thought I'd ask. Also, there are some API calls which are supposedly not available under most Windows installations (CryptBinaryToString, for example). I don't suppose this has changed? Basically, I'm trying to replicate the functionality of some .NET crypto stuff in C++ and finding that some of the stuff I need isn't available. Sean
3
1654
by: autocomp | last post by:
Hi, Can someone tell me how to incorporate planner with calrender ... or just a calender algo...will help
1
2916
by: soyspinoza | last post by:
hello out there, does anyone know of a good C/C++ implementation of the hungarian algorithm, (i.e. munkres algo?) thanks,
4
1430
by: nishit.gupta | last post by:
finding a loop in a lisked list without modifying it. 1.reverse and check for beginning. 2.take two pointers, keep moving 1st by one and 2nd by two locations, if they meet loop exists. 3.store addresses in an array. is their any other possible solution???
3
2220
by: empiresolutions | last post by:
I am building a app that creates quizzes. This is how it goes - - Create Quiz - Provide up to 10 different types of Quiz Results - Give up to 50 Questions - Each Question has up to 10 possible Answers. - Each Answer is assigned a Weighted value.... for each type of Quiz Result. - Weighted values are in the range of -6, 0, +6. - Each Quiz will also apply the same Weight range for if a M/F is taking and what of 6 age groups the taker is...
0
9543
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10029
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6808
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.