|
Hi,
1.
that is one of those rare problems that is handled well by using recursion. Basically you need to implement a single method that appends in turn each conceivable fraction of a combination to what you already have, then calls itself for the remainder of the job description.
Example: assuming you already have "A1" call yourself with argument "[8B]XY[TL]"; getting "[8B]" now, you foreach the possibilities, adding them to the result string, calling yourself again with "XY[TL]"
You could simply output the results or add them to a collection.
One advantage of recursion is you will get all possibilities in a nice predictable order.
2.
there is an alternate tactic provided the number of combinations is representable as an integer number.
It goes like this:
a. assume the variable substrings have P1, P2, ... Pp length
a. calculate the number of combinations N, i.e. the product of P1*P2*...*Pp as you stated;
b. loop an integer J from 0 to N-1
c. divide J by P1, use the remainder as the index in the leftmost possibilities string
d. divide the quotient by P2, use the remainder again as an index in the second possibilities string
etc.
The advantage here is all combinations are ordered again (different from the first method though), and you can simply calculate the K-th combination without calculating all the preceeding ones first.
|
|
|
|
|
With so few, I'd just define a struct with two character fields and make a list:
public struct Junk
{
public readonly char First ;
public readonly char Second ;
public Junk
(
char First
,
char Second
)
{
this.First = First ;
this.Second = Second ;
return ;
}
}
System.Collections.Generic.List<Junk> l =
new System.Collections.Generic.List<Junk>() ;
l.Add ( new Junk ( '8' , 'T' ) ) ;
l.Add ( new Junk ( '8' , 'L' ) ) ;
l.Add ( new Junk ( 'B' , 'T' ) ) ;
l.Add ( new Junk ( 'B' , 'L' ) ) ;
foreach ( Junk j in l )
{
System.Console.WriteLine ( "A1{0}XY{1}" , j.First , j.Second ) ;
}
Unless this is a homework assignment...
For a more general solution, I'd use my Combination extension method.
|
|
|
|
|
Hi, thanks for your response.
I'm actually looking for an absolute algorithm. A method that can be used with any string, not just with one or two specific ones.
|
|
|
|
|
Well, then I'd use loops to fill the List. No need to keep producing the same combinations over and over again.
System.Collections.Generic.List<Junk> l =
new System.Collections.Generic.List<Junk>() ;
foreach ( char f in new char[] { '8' , 'B' } )
{
foreach ( char s in new char[] { 'T' , 'L' } )
{
l.Add ( new Junk ( f , s ) ) ;
}
}
foreach ( Junk j in l )
{
System.Console.WriteLine ( "A1{0}XY{1}" , j.First , j.Second ) ;
}
This could be made more general.
Maybe I should read Luc's a little closer to see what it's doing.
|
|
|
|
|
FWIW: if you try method 2 and get bad results, I would guess you are doing something wrong. I just know it works well!
|
|
|
|
|
Actually I went for option #2 (long time since I have not work with recursion! even when I used to master a recursive programing language called Scheme!)
Anyways, did option #2 and It worked excellent, now I can submit any string into this method and it works great!
Thanks for sharing your light!
Regards.
|
|
|
|
|
you're welcome.
BTW: method 1 is actually simpler than method 2 provided you want all combinations.
|
|
|
|
|
Here's a generic version of a "one from column A, one from column B" algorithm.
Any number of columns should work and they don't need to all have the same number of items.
Empty columns are ignored (not tested).
public static System.Collections.Generic.IList<System.Collections.Generic.IList<T>>
MakeCombinations<T>
(
params System.Collections.Generic.IEnumerable<T>[] Values
)
{
System.Collections.Generic.List<System.Collections.Generic.IList<T>> result =
new System.Collections.Generic.List<System.Collections.Generic.IList<T>>() ;
if ( Values != null )
{
result.Add ( new System.Collections.Generic.List<T>().AsReadOnly() ) ;
for ( int i = Values.Length - 1 ; i >= 0 ; i-- )
{
if ( Values [ i ] != null )
{
System.Collections.Generic.List<System.Collections.Generic.IList<T>> temp =
new System.Collections.Generic.List<System.Collections.Generic.IList<T>>() ;
foreach ( T t in Values [ i ] )
{
foreach ( System.Collections.Generic.IList<T> many in result )
{
System.Collections.Generic.List<T> one =
new System.Collections.Generic.List<T>() ;
one.Add ( t ) ;
one.AddRange ( many ) ;
temp.Add ( one.AsReadOnly() ) ;
}
}
if ( temp.Count > 0 )
{
result = temp ;
}
}
}
}
return ( result.AsReadOnly() ) ;
}
System.Collections.Generic.IList<System.Collections.Generic.IList<string>> c =
MakeCombinations
(
new string[] { "Blonde" , "Brunette" , "Redhead" , "Black" }
,
new string[] { "Blue" , "Brown" , "Green" }
,
new string[] { "A" , "B" , "C" , "D" , "DD" , "DDD" }
) ;
Now I need to get to bed...
|
|
|
|
|
I'm building kiosk software where we need to be able to display a PDF file. Because this is a kiosk, there is no user interaction which means that it is filled with issues. What do you do with multiple pages, we are displaying on a plasma so the aspect ratio is wrong which means there will be black bars, we'd like to not have any menus since they take up real estate.
So does anybody have any ideas? One we came up with was to convert the document to jpgs, one page per image, and then scroll through them. This works but the library I looked at (http://www.aspose.com/) is slow (about 2 seconds a page).
I've also looked at the Adobe ActiveX control (just started looking at it) but we've had issues before where Adobe wants to update itself and puts a message on the screen.
So does anybody have any suggestions/thoughts on this?
TIA - Jeff.
|
|
|
|
|
Just a thought...
jbradshaw wrote: One we came up with was to convert the document to jpgs, one page per image, and then scroll through them. This works but the library I looked at (http://www.aspose.com/) is slow (about 2 seconds a page).
Is the PDF generated dynamically by software you don't control? Can you just create the JPEG's in advance (once, when you write the software), then display those and not have to worry about PDF's? And why does speed matter that much? Can't you just convert the PDF in advance (i.e., right before it's needed), then you'll have each page as a JPEG to scroll as you please? Also, any reason you chose JPEG? Do your PDF's contain natural type images (e.g., photography), or does it contain just text? If just text, PNG may be a more ideal format (lossless and compresses very well with computer generated images).
I did some Googling and came across ImageMagick, which apparently converts images and supports PDF as an input format (note that, for PDF's, it says it requires Ghostscript).
|
|
|
|
|
If Aspose is too slow, here's another library you might want to try: GDPicture.NET[^]
I haven't used it in any projects yet, but I did try it for a proof-of-concept to create a pdf from a composite image. It wasn't too hard to use and got the job done.
|
|
|
|
|
Turns out you can use the Adobe ActiveX as long as the Reader is installed on the machine.
Does everything I need.
Thanks everybody.
Jeff.
|
|
|
|
|
hey, I had a cross thread exception...
then i rewrite the problematic part to the next one
private void calculateEuclidianThread()
{
CalculateDistance euclidianKNN = new EuclideanDistance_KNN();
euclidianKNN.stLabel += delegate(object sender, string className)
{
string[] s = className.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (dataGridView2.InvokeRequired)
dataGridView2.Invoke(new MethodInvoker(delegate
{
dataGridView2.Rows.Add((object[])s);
}));
};
euclidianKNN.calculateDis(trainingSet, testSet);
}
but now it stucks when the event rises
what is the problem here ?
thanks
|
|
|
|
|
igalep132 wrote: now it stucks when the event rises
You'll have to do better than this. Coherently describe the problem and what is happening, including and exceptions.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
as i wrote before, the exception was
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on repair doesnt work
(i've dataGridView control which i tried to fill from thread i created...)
then i rewrite it (as it was posted in the first message)
and now no exception for the cross thread... but when the following line
dataGridView3.Invoke(new MethodInvoker(delegate
{
dataGridView3.Rows.Add((object[])s);
}));
is executed, the whole application freezes.
|
|
|
|
|
igalep132 wrote: the whole application freezes.
More descriptive then "it stucks"
Why are you even bothering with this? Bind the grid to a datasource and update the datasource, not the grid.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
i'm not familiar much with whole binding stuff,
i'll be glad to learn but i haven't mentioned that the data is actually some computation result which is calculated during the run time
can i bind this kind of source ?
and, if you can' please explain me what was the problem with the invoker ?
thanks
edit
|
|
|
|
|
igalep132 wrote: i'll be glad to learn
Then do so and quite possibly you will be able to answer your own question.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
what exactly i need to look for ?
and again, i would like to know what was wrong with the invoker ?
|
|
|
|
|
igalep132 wrote: what exactly i need to look for ?
DATABINDING
Does that help?!?
igalep132 wrote: what was wrong with the invoker ?
It is unnecessary! Learn to use the tools and techniques properly rather than trying to force something to make it work.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
thanks for being so angry...
|
|
|
|
|
ok, it still doesn't help !!
i still get the Cross-thread operation not valid: Control... exception when i'm trying to bind the dataGridView with the dataSet
so what should i do now ?
|
|
|
|
|
What do you mean by "it stucks when the event rises"? You sure it gets stuck and just doesn't add the row? In particular:
dataGridView2.Invoke(new MethodInvoker(delegate
{
dataGridView2.Rows.Add((object[])s);
}));
That code will never be fired if invoke is not required (that is, if dataGridView2.InvokeRequired is set to false). What you need is an "else" condition. See here for more information.
|
|
|
|
|
but in my case, invoke required, i'm running from different thread...
|
|
|
|
|
Ok, but you didn't answer my question. Answer that and maybe I can assist you.
|
|
|
|