Subset Question 
Author Message
 Subset Question

I'm trying to write an algorithm that gives me all combinations of an array

ab ac ad bc bd cd
    or
 aa ab ac ad bb bc bd cc cd

I've tried variations on the example program below to no avail. It seems
that I should be able to delete the first item of the array each time
through the loop and end up with all the combinations. However, if I comment

For example:
aa ab ac ad ba bb bc bd ca cb cc cd da db dc dd
They are there, but there's too much redundancy for what I want.

It looks like a problem for CS101, but I'm at a loss to figure out what the
problem is.

#!/usr/local/bin/perl -w



        print "$item times $element\n";

Quote:
}


Quote:
}

---------------------------
Bob Gwynne

Speech Comm
University of Tennessee, Knoxville

JAPN (Just Another Perl Newbie)



Tue, 04 Sep 2001 03:00:00 GMT  
 Subset Question


! I'm trying to write an algorithm that gives me all combinations of an array

! ab ac ad bc bd cd
!     or
!  aa ab ac ad bb bc bd cc cd
!

one way:


for(my $i = 0; $i < $#array; $i++){
    print map{"$array[$i]$array[$_]\n"} $i+1 .. $#array;

Quote:
}

regards
andrew


Tue, 04 Sep 2001 03:00:00 GMT  
 Subset Question
:I'm trying to write an algorithm that gives me all combinations of an array

:ab ac ad bc bd cd
:    or
: aa ab ac ad bb bc bd cc cd
:I've tried variations on the example program below to no avail. It seems
:that I should be able to delete the first item of the array each time
:through the loop and end up with all the combinations. However, if I
comment

:For example:
:aa ab ac ad ba bb bc bd ca cb cc cd da db dc dd
:They are there, but there's too much redundancy for what I want.
:
:It looks like a problem for CS101, but I'm at a loss to figure out what the
:problem is.



:        print "$item times $element\n";
:}

:}

Probably a bad idea to mess with the array you are looping over.
How about?

#!/usr/local/bin/perl -w
use strict;


my $i = 0;
my $j = 0;

    foreach ($j..$#array){
        print "$array[$i] times $array[$j]\n";
        $j++;
    }
    $j=++$i;

Quote:
}

HTH

AmD
--

--random quote --
When you come to a fork in the road take it.
 - Yogi Berra



Tue, 04 Sep 2001 03:00:00 GMT  
 Subset Question

Quote:

>I'm trying to write an algorithm that gives me all combinations of an array

>ab ac ad bc bd cd
>    or
> aa ab ac ad bb bc bd cc cd

sub combinations {


  return [] if $n == 0;





  }

Quote:
}

Uncomment the (#) line for the first behavior.
Uncomment the (##) line for the second behavior.


Tue, 04 Sep 2001 03:00:00 GMT  
 Subset Question
:I'm trying to write an algorithm that gives me all combinations of an array

:ab ac ad bc bd cd
:    or
: aa ab ac ad bb bc bd cc cd
:I've tried variations on the example program below to no avail. It seems
:that I should be able to delete the first item of the array each time
:through the loop and end up with all the combinations. However, if I
comment

:For example:
:aa ab ac ad ba bb bc bd ca cb cc cd da db dc dd
:They are there, but there's too much redundancy for what I want.
:It looks like a problem for CS101, but I'm at a loss to figure out what the
:problem is.



:        print "$item times $element\n";
:}

:}

Lunch break one-liner:


--

--random quote --
Down that path lies madness.  On the other hand, the road to hell is paved
with melting snowballs.



Tue, 04 Sep 2001 03:00:00 GMT  
 Subset Question
::I'm trying to write an algorithm that gives me all combinations of an
array

::ab ac ad bc bd cd
::    or
:: aa ab ac ad bb bc bd cc cd

:Lunch break one-liner:
:

Oops forgot for the second behavior set change:
(0..$#temp-1)
to
(0..$#temp)
--

--random quote --
A computer lets you make more mistakes faster than any invention in human
history - with the possible exceptions of handguns and tequila.
 - Mitch Ratliffe



Tue, 04 Sep 2001 03:00:00 GMT  
 Subset Question

Quote:
>Lunch break one-liner:


I was wondering if someone would come up with a one liner. And, yes, it fits
on one line. But isn't that technically three lines--if ";" is a break?

Foreach is more intuitive than map for me.  If the array is long, however,
speed would be important. Is map faster?  And finally, can someone tell me
why my original mangled the array? It looks logical enough to me. My version
of Perl is  5.004_04 built for sun4-solaris.

Bob

********************
Robert Gwynne, Ph.D.
Speech Communication
University of Tennessee, Knoxville

http://www.*-*-*.com/ ~gwynne
423-523-1097 Home
423-974-6836 Office
423-974-4879 Fax
***********************************
Liberalism has always derived its authority and persuasiveness from a vision
of human nobility, from the idea that our dignity is derived from the
exercise of m{*filter*}choice. M{*filter*}absolutism fears this act of choice and
fears the freedom required by the act of choosing.

Liberalism depends, ultimately, on faith in human choosing, and a liberal
revival depends on recovering the inspiration of this central conviction.

Michael Ignatieff



Tue, 04 Sep 2001 03:00:00 GMT  
 Subset Question
: Foreach is more intuitive than map for me.  If the array is long, however,
: speed would be important. Is map faster?  And finally, can someone tell me
: why my original mangled the array? It looks logical enough to me. My version
: of Perl is  5.004_04 built for sun4-solaris.

This is the original code:
#!/usr/local/bin/perl -w
use strict;
my ($element,$item);



        print "$item times $element\n";

Quote:
}

}

its results      array and element at each loop of foreach

                                   0 1 2 3

a times b
a times c           first loop of foreach : $element = $array[0];
a times d
                                   0 1 2

c times c
c times d           second loop of foreach : $element = $array[1];

                                   0 1

                    third loop of foreach : $element = $array[2];
                    and the array no longer even has such an element.

                         --thelma

: Bob

: ********************
: Robert Gwynne, Ph.D.
: Speech Communication
: University of Tennessee, Knoxville

: http://www.*-*-*.com/ ~gwynne
: 423-523-1097 Home
: 423-974-6836 Office
: 423-974-4879 Fax
: ***********************************
: Liberalism has always derived its authority and persuasiveness from a vision
: of human nobility, from the idea that our dignity is derived from the
: exercise of m{*filter*}choice. M{*filter*}absolutism fears this act of choice and
: fears the freedom required by the act of choosing.

: Liberalism depends, ultimately, on faith in human choosing, and a liberal
: revival depends on recovering the inspiration of this central conviction.

: Michael Ignatieff



Tue, 04 Sep 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Simple Subset Question

2. Perl tuning/speed question: all subsets of a set of length k -- keeping the inital order

3. Algorithm for Text Subsets

4. Small subset of Perl....

5. make subset of the search result

6. newbie Q:create a subset

7. all unique subsets of a list

8. alphabetical subset of hash keys

9. Finding "best fit" subset of a set?

10. A question of Style, was: SPUG: Sort an array question

11. Perl: web.pl question, install module and dmake question

12. Perl 6 question $# question - dollar sign pound sign

 

 
Powered by phpBB® Forum Software