-
Recursion, to get all vertical combinations in an array
Hello there, this is my 1st post in the site...
My problem follows:
I have an array M x N (eg 4 x 2)
1 X
X 2
1 2
X 2
What I want to do, is (USING RECURSION) to get all the possible vertical combinations (16 in the 4x2 example) as a (~) separated string. A few of the combinations are:
1 1 1 1
X X X X
1 1 2 2
X 2 X 2 ... and so on...
That is : "1X1X~1X12~1X2X~1X22~..."
TIA
-
Welcome! This sounds suspiciously like a class assignment. ;-) What have you tried so far?
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
Nothing I wrote worked. I 'm trying for 6 days now
I 'm on a football bets application (1:Home win, X:Even, 2:Guest win), so I want these vertical combinations. I believe its rather easy for someone who masters recursion. Obviously, I don't. That's why I admire this kind of coding.
PS.
I' ve post this in other forums too, and got the same first comment about it: Once again, IT'S NO CLASS ASSIGNMENT!!! Isn't it possible for a programmer to get stack on a few lines of difficult code? You may be the 3rd person to say this... I call out for open source help here. HEEEEEELP!
-
Is anybody interested in my question out theeeeere?
-
I'll try to help, but I don't understand the question. How do you get from:
1 X
X 2
1 2
X 2
to:
1 1 1 1
X X X X
1 1 2 2
X 2 X 2 ... ?
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
First of all, thanks for coming in. It's been a while since I opened my PC, so here I come too...
Well, it's simple. Think always vertically, trying to pass through all the possible ways, from every number on the top, to every number at the bottom, producing unique sequences of 4 numbers each time. E.g.:
1
X
1
X
then, another possible (and unique) pass, is
1
X
1
2
and another, is
1
X
2
X
and so on...(totally N^M=2^4=16 possible and unique passes.
All these have to become a single string separated by a special char
E.g.: s="1X1X~1X12~1X2X~...." using only one function call (recursively)
Looking forward for your next reply...
TIA
-
Hi,
I've been thinking of your problem, and it's just a matter of an extensive 'For' loop(s).
However, it would be easier to do, if i could understand how your data gets into the program. If I understand your example, you have 3 things that you want every combination of,right? Home,Even,Visitor? If thats true, you lost me when you said you had an array 4x2. Shouldnt it have been 3x2? Next, if Home,Even,Visitor are your base vertical info, then I suppose the horitontal is what each vertical items outcome is,correct? So, if thats so, then wouldnt the horizontal rows be possibly infinate, because you dont know how many times the initial vertical column will have an outcome? Or do you know? Say, how many games there will be in a season? I'm just trying to define the 2 dimensional array; TheArray(2,20) (the 20 is just a made up number for games of the season) Am I following you correctly in my line of thinking?
-
OK, let's start from the beginning.
The app I'm trying to develop has to do with betting slips, with various types of bets about the outcome a number of football games. Eg, if we have 4 games (A,B,C,D) that the player want to bet on, then the possible ways to bet is
1:Home win, X:Even, 2:Guest win,
or combinations, eg. the player bets on 1 or X. Or again, X or 2.
So we may have a variable number of games (vertically) and a variable number or betting types (horizontally) for each game, which are shown in an array below:
Bet types
A) 1 (home win)
B) X or 2 (even or guest win)
C) 1 or 2 (home or guest win)
D) X (even only)
So the array is MxN (in this case 4x2). Actually, in the horizontal direction, we could have more than those three types of bets (eg, the bets for a single game could be: 1 or X or SG<=3 or HS:1-0, where SG is the Sum of the Goals in the game, HS is the Half time Score, and so on.
In the simple case (to ease the development), I cut off the possible values horizontally to two, but as you see, it may be more (which multiplies the calculations of course). So if we have the following array (the minus symbol, means no second bet for the same match)
1 -
X 2
1 -
X -
it would produce the following two vertical combinations:
1 1
X 2
1 1
X X
which I'd like in the form
1X1X~121X
Again, if the initial array was
1 -
X 2
1 X
X -
it would produce the following four vertical combinations:
1 1 1 1
X X 2 2
1 X 1 X
X X X X
which I would like in the form :
1X1X~1XXX~121X~12XX
And again, if the initial array was
1 - -
X 2 1
1 X -
X - -
it would produce the following vertical combinations:
1 1 1 1 1 1
X X 2 2 1 1
1 X 1 X 1 X
X X X X X X
which I would like in the form :
1X1X~1XXX~121X~12XX~111X~11XX
That's all there is to it.
-
OK, try this:
Code:
Option Explicit
Dim BetArray(1 To 3, 1 To 4) As Variant
Sub Main()
Dim Row As Integer
Dim Path As String
Dim Combos As New Collection
BetArray(1, 1) = "1"
BetArray(1, 2) = "X"
BetArray(1, 3) = "1"
BetArray(1, 4) = "X"
BetArray(2, 1) = Null
BetArray(2, 2) = "2"
BetArray(2, 3) = "X"
BetArray(2, 4) = Null
BetArray(3, 1) = Null
BetArray(3, 2) = "1"
BetArray(3, 3) = Null
BetArray(3, 4) = Null
Row = LBound(BetArray, 2) - 1
Call GetCombinations(Row, Path, Combos)
End Sub
Sub GetCombinations(ByVal Row As Integer, ByVal Path As String, ByRef Combos As Collection)
Dim Col As Integer
Dim Temp As String
' If we're not at the last row...
If Row < UBound(BetArray, 2) Then
' Look at next row
Row = Row + 1
' Loop through columns
For Col = LBound(BetArray, 1) To UBound(BetArray, 1)
' If not null...
If Not IsNull(BetArray(Col, Row)) Then
' Add value at this row, col to path that led here
Temp = Path & BetArray(Col, Row)
' If length = number of rows, we're done;
' add to Combos collection
If Len(Temp) = UBound(BetArray, 2) Then
Combos.Add Temp, Temp
Exit Sub
Else
' Get combinations for next row
Call GetCombinations(Row, Temp, Combos)
End If
End If
Next
End If
End Sub
Now you can simply loop through the Combos collection and build your delimited string.
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
Just perfect and smart. Thanks.
Similar Threads
-
By brouse in forum VB Classic
Replies: 1
Last Post: 05-10-2005, 02:19 PM
-
By kanakatam in forum Java
Replies: 2
Last Post: 04-15-2005, 09:06 PM
-
By Jason Salas in forum ASP.NET
Replies: 2
Last Post: 04-20-2003, 11:39 PM
-
Replies: 0
Last Post: 06-04-2002, 09:54 AM
-
By Mark Alexander Bertenshaw in forum VB Classic
Replies: 10
Last Post: 06-16-2000, 05:34 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks