|
Sure, but probably inefficient, in particular I think finding the shortest is a waste of cycles.
I also wouldn't bother sorting them, even though that could lead to only having to find the common portion of the first and last.
Consider a very large collection of very long strings.
|
|
|
|
|
PIEBALDconsult wrote: Consider a very large collection of very long strings.
Thats ok, I'm here till Wednesday....
|
|
|
|
|
You could probably do it with a single pass, by simply moving through the list, comparing / discarding / comparing as you go.
|
|
|
|
|
What I wrote uses a pair of nested for loops -- comparing all the first characters, then all the second characters, etc. until a difference or the end of a string is found.
|
|
|
|
|
Stringsplit the first two paths into two arrays.
Compare the elements until you find a difference.
Compare the common result with the next string.split.
Repeat.
|
|
|
|
|
While the OP wants it to work for the specific case of file specs, the challenge is for it to work in the general case of any collection of strings.
|
|
|
|
|
ok, gotta read the specs better. I got stuck on your first paragraph.
|
|
|
|
|
Here's what I devised.
Obviously, I didn't add any handling of special cases -- it assumes a collection of at least two non-NULL strings.
My first implementation builds up the result in a StringBuilder and spits it out once a difference is found or after all characters have been found to be equal.
public static string
GetCommonPrefixA
(
params string[] List
)
{
System.Text.StringBuilder result = new System.Text.StringBuilder ( List [ 0 ].Length ) ;
for ( int i = 0 ; i < List [ 0 ].Length ; i++ )
{
for ( int j = 1 ; j < List.Length ; j++ )
if ( ( i >= List [ j ].Length ) || ( List [ 0 ] [ i ] != List [ j ] [ i ] ) )
return ( result.ToString() ) ;
result.Append ( List [ 0 ] [ i ] ) ;
}
return ( result.ToString() ) ;
}
This implementation doesn't use a StringBuilder; it simply uses the value from the outer for loop to perform a substring -- this might be better for languages that don't have a StringBuilder.
public static string
GetCommonPrefixB
(
params string[] List
)
{
int i = 0 ;
for ( ; i < List [ 0 ].Length ; i++ )
for ( int j = 1 ; j < List.Length ; j++ )
if ( ( i >= List [ j ].Length ) || ( List [ 0 ] [ i ] != List [ j ] [ i ] ) )
return ( List [ 0 ].Substring ( 0 , i ) ) ;
return ( List [ 0 ].Substring ( 0 , i ) ) ;
}
Because I'm not a fan of having multiple return s, I also devised this third implementation that uses a boolean to stop the loops.
Something I don't like about this is that the outer loop variable (i) is incrememted an extra time.
public static string
GetCommonPrefixC
(
params string[] List
)
{
bool same = true ;
int i = 0 ;
for ( ; same && i < List [ 0 ].Length ; i++ )
for ( int j = List.Length - 1 ; same && j > 0 ; j-- )
same = ( ( i < List [ j ].Length ) && ( List [ 0 ] [ i ] == List [ j ] [ i ] ) ) ;
return ( List [ 0 ].Substring ( 0 , i - 1 ) ) ;
}
Of course a goto would also work and not perform the extra increment.
public static string
GetCommonPrefixD
(
params string[] List
)
{
int i = 0 ;
for ( ; i < List [ 0 ].Length ; i++ )
for ( int j = List.Length - 1 ; j > 0 ; j-- )
if ( ( i >= List [ j ].Length ) || ( List [ 0 ] [ i ] != List [ j ] [ i ] ) )
goto done ;
done: return ( List [ 0 ].Substring ( 0 , i ) ) ;
}
You may have noticed that these last two also run the second for loop (j) as a decrement; the purpose of this is to detect a difference as soon as possible when the collection has been sorted. It also reduces calls to List [ 0 ].Length . These "optimisations" probably won't yield any big improvements, but they're cheap.
modified 15-Sep-14 16:29pm.
|
|
|
|
|
|
|
Wowah!
|
|
|
|
|
|
Nope, thanks I have replied to the message...
|
|
|
|
|
Looks like a gloved fist.
If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering.-Wernher von Braun Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein
|
|
|
|
|
Oracle sucks.
Oracle tools suck - they look like something out of the '90s - they want me to use the command line to install/uninstall stuff. How is this a billion dollar company?
Even then the uninstall didn't work so I'm digging around in the registry because Oracle for .NET has so fragged VS2008 that I cannot even establish a SQL connection anymore. It really is a crappy product.
Seriously, die in a fire.
|
|
|
|
|
Oracle's drivers and file system structure suck and I'd also love something like Management Studio for Oracle.
But as the database is designed for other billion dollar companies, the command line tools are just fine. Admins can run them in scripts, in scheduled jobs at night ... instead of clicking through them again and again for ever virtual machine they have to install.
This statement is false.
|
|
|
|
|
Corinna ? This must have been ages !! What brings you around here ? Lost something in a picture and looking for it ?
~RaGE();
I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
Entropy isn't what it used to.
|
|
|
|
|
I don't know why both cannot be done well.
|
|
|
|
|
Corinna John wrote: I'd also love something like Management Studio for Oracle Why not use TOAD or SQL Developer?
|
|
|
|
|
Long time, no see, Corinna - Welcome!
Yep, a gazillion years ago Oracle sent me a free copy of Oracle 8 - I tried it for a few days and uninstalled it. Not even worth trying to master that junk.
Will Rogers never met me.
|
|
|
|
|
|
MehGerbil wrote: How is this a billion dollar company?
By not investing thousands of dollars in developing a Fisher Price UI that everybody will anyway criticize and by relying on solid good'ol' command line ?
~RaGE();
I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
Entropy isn't what it used to.
|
|
|
|
|
I suppose you still use DOS to navigate to files on your PC?
Because if you're using Windows Explorer then that is 'Fisher Price' and much too easy for a serious professional to use.
|
|
|
|
|
I use TotalCommander Given, this is not DOS, but the design did not evolve since Win98.
~RaGE();
I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
Entropy isn't what it used to.
|
|
|
|
|
Good Heavens!
I'm going to have to begrudgingly give you a +5.
|
|
|
|