I need to write a loop that prints all the combination possibilities of a
character array. Basically, taking a scrambled word, or a regular word, and
printing out all the combinations. The letters in the word will only be
used once. It sounds easy, but for some reason, I can't get the logic
straight in my head. The formula is as follows:
If n = the number or letters in the word, the C = the number of
combinations, so:
C = (n)(n - 1)(n - 2)....(n - (n - 1))
Or
C = (n)(n - 1)(n - 2)...(1)
I'm totally screwed up on the logic behind writing a loop (or nested loop)
that would take care of this. Any help is appreciated. 4 5246
Let's make this even harder for all you hardcore guys. The formula below
only works when each letter is distinct. It will not work if there are
duplicate letters. For example:
There are 24 different combinations for ABCD.
However, if I have AABB, then there are quite a few less:
AABB
ABAB
ABBA
BBAA
BABA
BAAB
So, the problem now becomes, how do I cycle through loops and print out all
DISTINCT possible combinations?
"OpticTygre" <op********@adelphia.net> wrote in message
news:Oo********************@adelphia.com... I need to write a loop that prints all the combination possibilities of a character array. Basically, taking a scrambled word, or a regular word, and printing out all the combinations. The letters in the word will only be used once. It sounds easy, but for some reason, I can't get the logic straight in my head. The formula is as follows:
If n = the number or letters in the word, the C = the number of combinations, so:
C = (n)(n - 1)(n - 2)....(n - (n - 1))
Or
C = (n)(n - 1)(n - 2)...(1)
I'm totally screwed up on the logic behind writing a loop (or nested loop) that would take care of this. Any help is appreciated.
Hi, here's a little code that I put together, it seems to work fairly well.
I just used a hashtable to remove duplicates: I'm new to VB.NET, so there
is probably some more efficient way to do this. (I didn't comment any of
the code, but I used descriptive variable names, so it should be easy enough
to read.
Imports System.Collections.Hashtable
Public Class Form1
Inherits System.Windows.Forms.Form
Private mScrambledWords As Hashtable = New Hashtable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim scrambledword As String
Dim i As Long
Dim currentWord As String
Dim currentEntry As DictionaryEntry
Dim oEnum As IEnumerator
scrambledword = "ABCDEFF"
ScrambleWord("", scrambledword)
oEnum = mScrambledWords.GetEnumerator()
'Write out the words
Debug.WriteLine(mScrambledWords.Count & " Unique Words Found.")
Me.Text = mScrambledWords.Count & " Unique Words Found."
Do Until oEnum.MoveNext = False
currentEntry = oEnum.Current
currentWord = currentEntry.Value
Debug.WriteLine(currentWord)
Loop
End Sub
Private Sub ScrambleWord(ByVal baseLetters As String, ByVal
ScrambledLetters As String)
Dim c As String
Dim subScrambledLetters As String
Dim i As Integer
For i = 1 To Len(ScrambledLetters)
'take the current letter in position 1, and then scramble the
remaining letters
c = Mid(ScrambledLetters, i, 1)
subScrambledLetters = excludeLetter(ScrambledLetters, i)
ScrambleWord(baseLetters & c, subScrambledLetters)
If Len(subScrambledLetters) = 0 Then
addWordToHash(baseLetters & c)
End If
Next
End Sub
Private Sub addWordToHash(ByVal word As String)
Try
mScrambledWords.Add(word, word)
Catch ex As Exception
'Debug.WriteLine("Duplicate Word Found: " & word)
End Try
End Sub
Private Function excludeLetter(ByVal sString As String, ByVal
letterIndex As Integer) As String
If Len(sString) <= 1 Then Exit Function
excludeLetter = sString.Remove(letterIndex - 1, 1)
End Function
End Class
To actually calculate C (which I don't think that is what you're
looking for) you could use a recursive function (untested):
Public Function Calc(n as integer) As Integer
If n > 1 Then
Return Calc(n-1)
Else
Return 1
Endif
End Function
Perhaps a different recursive function might help you in getting the
possible combinations of the word. Here is a link that may be useful
to you: http://mathforum.org/library/drmath/view/60844.html
Hi Chris,
Actually, calculating C is useful, however, the below function only works if
all the letters are different.
'ABCD' has 24 solutions: n! = n(n-1)(n-2)(n-3) = 4(3)(2)(1) = 24
But 'AABB' only has 6 distinct solutions:
AABB
ABAB
ABBA
BBAA
BABA
BAAB
So after lots of math toiling, I came up with the following:
If 'a' represents the count of the letter 'A', and 'b' represents the count
of the letter 'B' and so on, then:
(a + b + c.....)! / (a! * b! * c!....)
Or, more simply
n! / (a! * b! * c!...)
So in the case of 'AABB':
C = (2 + 2)! / (2! * 2!)
C = 4! / 4
C = 24 / 4
C = 6
This formula should work for finding distinct combinations, whether or not
there are duplicate letters.
-Jason
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com... To actually calculate C (which I don't think that is what you're looking for) you could use a recursive function (untested):
Public Function Calc(n as integer) As Integer If n > 1 Then Return Calc(n-1) Else Return 1 Endif End Function
Perhaps a different recursive function might help you in getting the possible combinations of the word. Here is a link that may be useful to you:
http://mathforum.org/library/drmath/view/60844.html This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: teoryn |
last post by:
I've been spending today learning python and as an exercise I've ported
a program I wrote in java that unscrambles a word. Before describing
the problem, here's the code:
*--beginning of file--*...
|
by: acuttitta |
last post by:
I ran into something really bizzaro today.
A team member had me look into some export code for some data he was
outputting. The data looks at shipping containers and attempts to look
at how...
|
by: Ben |
last post by:
Hi,
I am looking to incorporate the scrambled text functionality (a graphic
etc with scrambled text appears and the user is required to type in what
he sees to prevent anonymous hits/spamming etc....
|
by: infedil |
last post by:
I've been trying to make a program that takes as input a scramble word,
gets the anscii value of that scrambled word, and numerically sorts it. And tries to find the same numerical order in the...
|
by: Poly |
last post by:
Here is a block of code that I am having trouble with. I rewrote it as a main function, so I could isolate the problem to fix it, but still haven't been able to figure out the problem. I am pretty...
|
by: Qbert16 |
last post by:
I'm having a lot of trouble with the following task for one of my assignments in python. Basically I need to take the user input and replace any words in the input and replace them with words from a...
|
by: helpmesos |
last post by:
hihi
Firstl i got most of this code from another person on another forum but I have been trying like crazy to modify it so that it reads a list of scrambled words from a file and outputs the...
|
by: tpgames |
last post by:
I do not understand why this code does not work? It will show ????? for the word length, but will does not actually access the individual letters within the word list. I enter the vowels as guesses,...
|
by: madafaka |
last post by:
guys i am this term i am taking basic c++ course and in this homework, i encountered an infinite loop problem and i dont know how to fix it. i am pretty sure that it is an easy problem for you, so if...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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,...
| |