Showing posts with label sort. Show all posts
Showing posts with label sort. Show all posts

Friday, September 19, 2008

Combinations Vs. Permutations On Linux and Unix

Hey There,

Here's a little something to finish the week off and tie up some loose ends. You may have noticed in our Perl script to maximize guaranteed matches in any give number pool that we did all of our work using permutations, which we then went through the trouble of sorting and removing duplicates. Probably a few people out there were thinking: "Why permutations if 1,2,3 and 3,1,2 are going to be considered equal? Isn't that just a bunch of extra work?" The answers to those questions are "why not" and "yes" ;)

If you feel like taking the time now, to save yourself time in the future, it would be well worth your while to replace the permutation subroutine in that script with one that generates all possible combinations instead. The difference between permutations and combinations is very relevant here because, really, we wanted all possible combinations but the author's rampant paranoia and fear of missing something caused him to generate all permutations and then weed that pool down to all possible combinations.

And what is the difference between permutations of a string and combinations of a string (assuming each alphanumeric character in the string is an element of the string)? Very simply: Permutations are all the possible ways "all" elements of the string can be translated, while combinations are all the possible unique groupings of any (and all) the elements that a string contains.

For example: The permutations of a string "abc1" would be composed of (in whatever order you want):

1 c b a
c 1 b a
1 b c a
b 1 c a
c b 1 a
b c 1 a
1 c a b
c 1 a b
1 a c b
a 1 c b
c a 1 b
a c 1 b
1 b a c
b 1 a c
1 a b c
a 1 b c
b a 1 c
a b 1 c
c b a 1
b c a 1
c a b 1
a c b 1
b a c 1
a b c 1


while the combinations of a string "abc1" would be composed of:

a
abc1
ab
1
c
bc1
abc
b
bc
c1


Here's wishing you all an enjoyable weekend. Hope this little script helps you with your combination generation (no permutations today :)

Cheers,


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/usr/bin/perl

#
# combos.pl
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

print "Enter a string... any string: ";
chomp($string = (<STDIN>));

my %results;
for my $i ( 0 .. length $string ) {
for my $j ( 0 .. length $string ) {
$results{ substr $string, $i, $j } = 1;
}
}

delete $results{''};

$results = keys %results;
print "\nYour string $string resulted in $results combinations!\n\n";
foreach (keys %results) {
print "$_\n";
}


, Mike




Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.

Friday, September 5, 2008

Unique Sorting Of Lists And Lists Of Lists With Perl For Linux Or Unix

Hey There,

Yesterdays post on sorting Perl lists and removing duplicates did end up being a bit heavy at the end, as I feared. I received my fair share of "here's how you can do it better" emails, which (although I'm not trying to do my most succinct and efficient work here) I always appreciate. In all seriousness, I really do enjoy getting different perspectives on methods and practices put up on this blog. I'm always looking to better my own skill-set, so keep the free advice coming. I'll probably devote an entire future post to the suggestions I've gotten regarding this (with attribution to the contributor's, of course) as the myriad means to an end are quite diverse, clever and interesting. And, yes, we do print contrary opinions as well, but they have to be logical arguments or, at the very least, scathing put-downs that make sense within the given context. If you just write us to tell us to go #### ourselves, we may take you up on that (it can get lonely out here in cyberspace ;) but it won't ever end up as a comment attached to a post.

For today, we're going to break down two areas which generated the most response (mostly because, although I tried, the code just wasn't self-explanatory enough). Sometimes it's actually harder to try and explain something you already do by rote than to explain something you're in the process of picking up on yourself. The two examples for today fall into the former category. My blinders made me think that the code I put out would be easily digested, but (after reading a few comments) I agree that perhaps I was a bit presumptuous. But, life goes on and we learn from our mistakes (even if we just keep making them over and over again ;)

The basic questions, and a bit more detailed of a breakdown:

Q. Why the klunky foreach block at the top of the attached code? Shouldn't sorting be simpler?

A. Actually, sorting data is simpler, but, in this case we were dealing with sorting on two levels, which may not have been readily apparent. The @s_perms array that gets returned from the public domain "permute" subroutine actually contains multiple scalar pseudo-lists. That is to say that $s_perms[0] could be equal to "1 3 2 4 5." While this resembles our lists that we want to sort, it is actually just one variable made up of numbers and spaces. So, for each member of the @s_perms array, we did the following:

1. First we split the scalar variable into an actual array, so that "1 3 2 4 5" became @temparray, with values of 0 through 4 ($temparray[0] = 1, $temparray[1] = 3, etc), like so:

@temparray = split(" ", $unsorted);

2. Then, we sorted that newly created array. The reason we sorted each value returned by the "permute" subroutine was so that each value would be in order. When we do it this way (below), every @temparray list ends up with the members, in order, of "1 2 3 4 5." If we had just called a numeric sort ( $a <=> $b ) on @s_perms, it would have sorted all of the individual lists using their initial numeric value as the sort index. To put it more simply, assuming @s_perms contained only 3 scalar values of:

"1 2 3 5 4"
"4 5 3 2 1"
"1 5 4 2 3"


a straight-up sort on @s_perms would generate a sorted list like this (in order, top to bottom):

"1 2 3 5 4" <-- All 3 scalar values ("1 2 3 5 4," "1 5 4 2 3," etc, being considered single entities) are being sorted as individual elements within an array.
"1 5 4 2 3"
"4 5 3 2 1"


Since our Objective states that all permutations of the same lists of values should be considered equal, the straight-up sort wouldn't make it easier (later) to determine if any, or all, of the 3 scalars were representative of unique or duplicate lists. When we break down each scalar value within the @s_perms array and split that scalar into a temporary array (@temparray) and then do the sort on that, only the values in @temparray get sorted. That is to say that "1 2 3 5 4" gets sorted, and then "1 5 4 2 3," etc. This results in our generating a sorted list like this:

"1 2 3 4 5" <-- Each scalar is split into an array with the "@temparray = split(" ", $unsorted);" statement from above and that array is sorted independently of each succeeding or preceding one!
"1 2 3 4 5"
"1 2 3 4 5"


3. Then we get to the step that caused some folks to wonder why it wasn't the first action taken:

@temparray = sort { $a <=> $b } (@temparray);

Now, we're sorting just like we would have in step 2 (if we did it this way), except we're sorting array variables that have already had their "insides" sorted. Having a list of array elements like:

"1 2 3 4 5"
"1 2 3 4 5"
"1 2 3 4 5"


makes it a lot easier to weed out the duplicates later.

4. Then, we add that array (of individually, and universally, sorted arrays) to the right hand side of the @sorted_perms array, which we use further on in the script.

unshift(@sorted_perms, "@temparray");

Simple enough ;) Hopefully, I'm not making this any worse for the folks out there who wrote in ;) Seriously, though, I do hope this explanation helps out at least a little.

The second question was just as valid:

Q. What's with the @unik and @last_unik variables. I understand that you're comparing arrays, but what are you doing in your "elsif" equality test?

A. This is a great question, because, on its face it seems that all we would have to do is check if the two arrays are equal and that would be that. Unfortunately, this practice has one fatal flaw (and, curiously enough, the behaviour is the same if you use the numeric or alpha comparison operators!) The basic test of the "entire array" actually only considers quantity of indices as indicators of uniqueness. For instance, this small script:

#!/usr/bin/perl

@bob = qw (1 2 3 4 5);
@joe = qw (1 3 2 5 4);

if ( @bob == @joe ) {
print "BOB and JOE are == Equal\n";
}

if ( @bob eq @joe ) {
print "BOB and JOE are EQ Equal\n";
}


produces this output:

host #./test
BOB and JOE are == Equal
BOB and JOE are EQ Equal


Since the basic equality operators only count indices to compare, I used that as the "outside loop" test. I figured, if the two arrays didn't have the same number of members, they couldn't possibly be equal, and we'd move on to the next loop without doing all the extra work. However, if the index counts match, then we need to check and see if the "content" of the arrays are exactly equal. So, if:

} elsif ( @unik == @last_unik ) {

returns true (both @unik and @last_unik have the same number of members), we'll go check them out index by index. I started out by setting three variables, just for ease of reading and to establish controls.

1. $total = @unik; <-- This populates the $total scalar variable with the length of the @unik array

2. $zero = $nope = 0 < -- This is just simple shorthand for assigning the variables $zero and $nope the value of "0."

3. Then, I iterated through both arrays at the same time (At this point in the program, every array has been sorted, so they should all be "1 2 3 4 5"). This was accomplished by a simple while loop that incremented the $zero variable (which represented the index of the @unik and @last_unik arrays) and compared those index values with each other. If, at any point, an index in @unik didn't match the corresponding index in @last_unik, the variable $nope was set to 1 and the while loop was broken (no sense calculating the rest of the index values, since we just hit a difference and the two arrays can't be identical anymore). Every time the corresponding values matched, the value of $zero was incremented by 1, and the process repeated, until the value of $zero reached the value of $total (the length of the @unik array:

while ( $zero < $total ) {
if ( "$unik[$zero]" ne "$last_unik[$zero]" ) {
$nope = 1;
last;
} else {
$zero++;
}
}


4. The block of code following that would only execute if we had found a difference between the two arrays, and the value of $nope was set to 1. This would be an indicator that @unik was a different (unique) array than the previous one and should be printed out. Then (and this shows up more than once), after the value is printed, I set @last_unik to be equal to @unik (Note that the = "assignment" operator is used here, rather than the == "comparative" operator). This sets us up to test the next @unik against @last_unik (which was @unik in the previous iteration) and easily be able to tell if that next array is just more of the same. This is set elsewhere within the larger construct, as well (on a different condition).

if ( $nope ) {
print "$s_p";
@last_unik = @unik;
}


5. As a side note for this piece of code, again, it was written to be simple to follow and works on the assumption that we've sorted the individual arrays numerically (see the first question at the beginning of this book ...I mean, post ;) and they are all the same. For instance, if the array @sorted_perms contained @array1, @array2 and @array3, and they were populated like this:

@array1 = "1 2 3"
@array2 = "3 2 1"
@array3 = "1 2 3"


You would not get a return of 2 values ("123" and "321") since we're not maintaining state in any other way. Since array's 1, 2 and 3 are processed in order and different from each other (in order, one after the other), they would all show up as unique (and be printed) in our example. If we wanted to make sure that we only got "123" and "321" back, we'd have to implement another method to double check that we weren't repeating ourselves. Of course, that would add another level of complexity, and I'm trying to keep this as simple as possible, but, since you asked, you can easily avoid duplicate unique values (??? Can those really exist in a sane universe? ;), you can do this most simply (I think), using grep, and Perl hashes, in the right place, like in the pseudo-code below:

foreach $bogus_value (@bogus_array) {
unless ($seen{$bogus_value}) {
$seen{$item} = 1;
unshift(@bogus_array, $bogus_value);
}
}


and, that would guarantee you that no "duplicate unique" values would appear in your output :)

That's about all I can sneak in at lunch here today ;) Until next time.
Cheers,

, Mike




Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.

Thursday, September 4, 2008

Sorting Perl Lists And Removing Duplicates On Linux Or Unix

Hey there,

This may be the first time I've run a series of posts where the topic was different every step of the way. That's a good thing, because I get just as bored as anyone else reading about the same thing over and over ;) So, following in the footsteps of our posts on Number Pools And Guaranteed Combinations Within Fixed Lists and Perl List Permutations, we'll move straight ahead to the next logical step, which is almost an entirely different subject altogether. I will however, reiterate (repeat again... sorry, one of my idiosyncrasies ;) our Objective in this whole exercise (Skip to the next paragraph if you feel I've done this sentence to death ;) : Given a Number Pool of "x through y," create the maximum possible Fixed List Length variations of our Fixed List that contain some variation of our Guaranteed Combination, without any duplication (i.e. 1, 2, 3 is equal to 2, 3, 1 and would only count as one match), and return the results.

Of course, to revisit the definitions of our key components , please refer back to our previous post on Number Pools

Today, we're going to look a Perl list sorting. In this context, you can actually consider our lists as arrays if you prefer. I'm going to keep calling them lists because, in the end, it will most closely approximate what we will want to manipulate; a fixed list of numbers.

From yesterday's posts on list permutations you can basically understand, even if you didn't run the code, how very many different combinations of 6 numbers there can be, when duplication is not a factor (i.e. 1, 2, 3 is different than 3, 2, 1). In our case, given our Objective, we would want those number lists, in parentheses above, to be considered equal. So how do we go about that?

There are actually quite a number of ways you can go about doing this. They run the gamut from human-readable and computer-intensive to nearly-hieroglyphic and incredibly efficient. Some folks like to dump all their values into hashes and sort it out that way, since the Perl hash function is built to sort keys and values. I'm not against that at all, but feel like it might stray us farther off the path and I want to keep this series of posts somewhat accessible.

My answer is actually a fairly commonly used one, as well. I don't consider myself a Perl expert (or really even a Unix or Linux expert. Like in Eastern philosophy: Even the teacher is ever the student). If I ever do convince myself that I know it all, I'll probably have a fatal seizure from lack-of-interest within moments of my revelation ;)

I like to sort all of my permutations (in this case numerically) first, and then check for duplicates. This makes sense to me, for some reason, and (especially in this case) it can greatly reduce the pressure you feel when dealing with large numbers of numbers. For instance, our 720 permutations are all of the same 6 numbers, so, once they've all been sorted numerically, we're going to have 720 identical lists. We don't even need a computer to tell us what our Objective obliquely defines as a "unique list" in this situation. It's "1, 2, 3, 4, 5."

All of our actions are predicated on the output of the "public domain" code we put out yesterday, which has been revised slightly to suit our purposes (the original just printed out the answers and didn't do any variable assignation or a return) and, yes, I do use my lists as arrays from time to time (we all need time to relax and unshift ;) The "permute" subroutine is called with an array, or list, (@number_seeds) as its only argument (which contains "1, 2, 3, 4, 5"), like:

@s_perms = permute([@number_seeds], []);

Modified code below:

sub permute {
my @items = @{ $_[0] };
my @perms = @{ $_[1] };
unless (@items) {
# LIMITED TO ONE CONTIGUOUS PERMUTATION CHAIN
@sortperms = join(" ", @perms);
unshift(@s_perms, "@sortperms" );
} else {
my(@newitems,@newperms,$i);
foreach $i (0 .. $#items) {
@newitems = @items;
@newperms = @perms;
unshift(@newperms, splice(@newitems, $i, 1));
permute([@newitems], [@newperms]);
}
}
return @s_perms;
}


And that's the lesson for today. I've attached code to the end of the post to illustrate the steps of sorting each list in our list of lists and checking for (and removing) duplicate lists from the pool. Note that we don't take into account multiple variable lists, as this won't be germane later (and adds some heft to the scriplet :) I tried to make this as easy to read as possible, so various steps, which could be combined, have been spelled out. If it has the opposite effect and makes things more confusing, I apologize profusely. And let me know! If there's interest, I'd be happy to write about this stuff at a much more granular level :)

Here's the mock output, to save you a screenshot:

host # ./program
1 2 3 4 5


Enjoy!


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/usr/bin/perl

#
# program - an unoriginal name for an unoriginal concept ;)
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

foreach $unsorted (@s_perms) {
@temparray = split(" ", $unsorted);
@temparray = sort { $a <=> $b } (@temparray);
unshift(@sorted_perms, "@temparray");
}
#@sorted_perms = sort { $a <=> $b } (@s_perms);

$unik="0";
$last_unik="0";
foreach $s_p (@sorted_perms) {
@unik = split(" ", $s_p);
@unik = sort(@unik);
if ( $unik == $last_unik ) {
print "$s_p";
@last_unik = @unik;
$last_unik++;
} elsif ( @unik == @last_unik ) {
$total = @unik;
$zero = $nope = 0;
while ( $zero < $total ) {
if ( "$unik[$zero]" ne "$last_unik[$zero]" ) {
$nope = 1;
last;
} else {
$zero++;
}
}
if ( $nope ) {
print "$s_p";
@last_unik = @unik;
}
} else {
print "$s_p";
@last_unik = @unik;
}
}


, Mike




Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.

Monday, October 29, 2007

Sorting "Human Readable" Files By Size Numerically

Hey There,

This one used to drive me nuts! Not less than few revisions ago, Solaris' implementation of df and du only had the "-k" option, so you could look at file sizes, etc, in terms of kilobytes rather than blocks (which could be variably sized).

Then everything started getting fancy with Solaris 9+ (Linux, I think, sported this option first). They began including the "-h" (Human Readable) option so that you could see file sizes, etc, in very easy to read format (e.g. 1Mb rather than 1024 in kilobytes).

The main problem I had with this was that you could no longer run the command stream "du -sk *|sort -n" and have the files list out in numerical order from smallest to largest (or vice versa). If you ran "du -h|sort -n" you'd almost get the same thing, but 2Mb might be listed right after 1Gb and before 3Gb. The numbers were still in order, but the sizes really weren't being listed out from smallest to largest correctly.

Here's a quick little scriptlet that I put together to avoid this headache. If they haven't fixed it yet, hopefully they will soon (assuming it's an "error," which, technically, it isn't).

Just put the following in a script and run it as "./myscript" (or add command line options, like you would to du, if you want to be more selective):


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/bin/ksh

#
# 2007 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

if [ ! -z $1 ]
then
ARGS="$@"
else
ARGS="*"
fi

/usr/bin/du -sk $ARGS|/usr/bin/sort -n|/usr/bin/awk '{print $2}'|/usr/bin/xargs -ia /usr/bin/du -sh "a"


Voila! The sizes are listed in Human Readable format and they're also correctly listed in size from smallest to largest :)

, Mike