473,387 Members | 1,431 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,387 software developers and data experts.

Permutation listing

I'm trying to create a program to list all of the possible permutations of a
string of characters. This obviously is equal to x!, where x is the number
of characters to permutate. I have created a function to alphabetize the
string (I lowercase it before this), and another that swaps any two
characters within a string, with proper error-checking. I have fully tested
these functions to ensure they work properly. The problem lies in the fact
that the recursive procedure that I have designed, rather than iterating
through itself x! times, actually iterates 2 ^ (x - 1) times.

The code is as follows:

' Given any string, displays all permutations of the characters within the
string.

Private Sub PermutateString(ByVal originalString As String, ByVal numCalls
As Integer)

' Interesting bug: it's going through the loop 2^(x - 1) times, x = chars in
text

' Add 1 to count of iterations through the sub

Debug += 1

Dim tempString As String

Dim strLength As Integer

Dim Iterations As Integer

Dim timesToLoop As Integer

tempString = originalString

strLength = originalString.Length

Iterations = numCalls

' Go through the characters until reach the point we need to swap

For timesToLoop = 1 To Iterations - 1

' Call self to find proper swapping location

PermutateString(tempString, Iterations - timesToLoop)

Next

tempString = SwapCharsInString(tempString, strLength - Iterations + 1, _

strLength - Iterations)

txtCombinations.Text &= tempString & vbCrLf

Return

End Sub

....Any ideas, anyone? I'm kinda stuck here... and tired... heh. You don't
need to answer it for me. This is a personal project. If you can give me a
hint as to what I'm doing wrong, or how I can add to it to make it right,
that would be terrific. Thanks! :)


Jan 30 '06 #1
2 1610

Eric A. Johnson wrote:
I'm trying to create a program to list all of the possible permutations of a
string of characters. This obviously is equal to x!, where x is the number
of characters to permutate. I have created a function to alphabetize the
string (I lowercase it before this), and another that swaps any two
characters within a string, with proper error-checking. I have fully tested
these functions to ensure they work properly. The problem lies in the fact
that the recursive procedure that I have designed, rather than iterating
through itself x! times, actually iterates 2 ^ (x - 1) times.


I'm not going to asses whether or not your algorithm is correct; I'm
just going to point out that the erroneous behaviour suggests that
maybe you are not marking particular characters as 'used' when you use
them.

--
Larry Lard
Replies to group please

Jan 30 '06 #2
I think you've got it! That idea had been lurking in the back of my mind
last night... I knew that it was *something*, and part of my brain had a
vague idea it might be kinda like that, but I was so tired that the idea
never fully formed itself. I'll work on that when I get hom from work this
evening (after my final). Thanks for pointing that out! :)

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

Eric A. Johnson wrote:
I'm trying to create a program to list all of the possible permutations
of a
string of characters. This obviously is equal to x!, where x is the
number
of characters to permutate. I have created a function to alphabetize the
string (I lowercase it before this), and another that swaps any two
characters within a string, with proper error-checking. I have fully
tested
these functions to ensure they work properly. The problem lies in the
fact
that the recursive procedure that I have designed, rather than iterating
through itself x! times, actually iterates 2 ^ (x - 1) times.


I'm not going to asses whether or not your algorithm is correct; I'm
just going to point out that the erroneous behaviour suggests that
maybe you are not marking particular characters as 'used' when you use
them.

--
Larry Lard
Replies to group please

Jan 30 '06 #3

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

Similar topics

7
by: Mike | last post by:
Hello all, I'm looking for a free method to apply a one-way encryption to a 64-bit value in an ASP script. Does anyone have any links to existing code or at least a process that I can follow to...
10
by: Talin | last post by:
I'm sure I am not the first person to do this, but I wanted to share this: a generator which returns all permutations of a list: def permute( lst ): if len( lst ) == 1: yield lst else: head =...
3
by: Jack Middleton | last post by:
Hi! I'm lookin for a faster permutation algorithm for matrices. I know that it can be done with multiplying a matrix with a permutation matrix. It just seems a waste to iterate through all those...
1
by: user | last post by:
Hello I have Array of 50 ints. I want to receive random permutation, so in each int will be different number from 0-49. Is there any class for permutation ? Thanx Michal
6
by: Rajesh | last post by:
Hello Everybody, Can anybody help me in writing a C program to generate and print all possible combinations of n numbers. For eg. for 3 numbers(1,2,3) there turn out 3! combinations. (1,2,3),...
27
by: onkar | last post by:
How to generate different permutations of n char array? ex : for n= 3, and basic string = abc bca cab bac cab ..... ... ..
3
by: weidongtom | last post by:
Hi, I have been working at this problem, and I think I need a permutation algorithm that does the following: Given a list of elements that are either a character or a character follows by a...
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
7
by: xirowei | last post by:
Let's say i create a String array that store 4 Alphabets {"A","B","C","D"} How can i get the result if i need permutation of 4P3 and 4P2? I had refer to many examples from the internet, but...
0
by: 249740 | last post by:
Write a program that reads N phrases and determines if one phrase is a permutation of the other. For example: Phrase 1 is: “One World One Dream” Phrase 2 is: “World One One Dream”. Then the output...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...

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.