Re: [QUIZ] Perl 'Medium' Quiz: Graph Connected Components (#2006-12-29)
"Bob Hiestand" <[email protected]> Mon, 8 Jan 2007 09:56:20 -0600
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
------=_Part_52360_32033584.1168271780142 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline My first attempt has a flaw, in that it would replicate members of a partition if both vertices had previously been encountered and were already part of the same partition. This version corrects that. On 1/4/07, Bob Hiestand <[email protected]> wrote: > On 12/29/06, Shlomi Fish <shlomif-ik1l9ssToec+JF/[email protected]> wrote: > > Here's a quiz for the new year. My sister received this challenge for her > > college homework, and had to write some Standard ML code to solve it, a task > > which I helped her with. I hope you enjoy it too. > > I had originally misunderstood the sorting requirement, so I suspect I > would have done things a little differently if I had incorporated it > from the beginning. > > Nothing fancy or elegant here, just brute force creation of partitions. > > Bob > > > ------=_Part_52360_32033584.1168271780142 Content-Type: application/x-perl; name="connected.pm" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="connected.pm" X-Attachment-Id: f_ewp2u6vr IyEvdXNyL2Jpbi9wZXJsCgp1c2Ugc3RyaWN0Owp1c2Ugd2FybmluZ3M7Cgp1c2UgRGF0YTo6RHVt cGVyOwpzdWIgY29ubmVjdGVkKCQpCnsKICBteSAkZWRnZXMgPSBzaGlmdDsKICBteSAlbm9kZXMg PSAoKTsKCiAgbXkgJGluZGV4ID0gMDsKICBteSAlZWRnZXNPcmRlciA9ICgpOwogIGZvciBteSAk ZWRnZSAoQCRlZGdlcykKICB7CiAgICAkZWRnZXNPcmRlcnskZWRnZX0gPSArKyRpbmRleDsKICAg IG15ICgkbm9kZUEsICRub2RlQikgPSBAJGVkZ2U7CgogICAgaWYoJG5vZGVzeyRub2RlQX0gYW5k ICRub2Rlc3skbm9kZUJ9KQogICAgewogICAgICBpZigkeyRub2Rlc3skbm9kZUF9fSAhPSAkeyRu b2Rlc3skbm9kZUJ9fSkKICAgICAgewogICAgICAgIHB1c2ggQCR7JG5vZGVzeyRub2RlQX19LCAo QCR7JG5vZGVzeyRub2RlQn19KTsKICAgICAgICAkeyRub2Rlc3skbm9kZUJ9fSA9ICR7JG5vZGVz eyRub2RlQX19OwogICAgICB9CiAgICB9CiAgICBlbHNpZigkbm9kZXN7JG5vZGVBfSBvciAkbm9k ZXN7JG5vZGVCfSkKICAgIHsKICAgICAgJG5vZGVzeyRub2RlQX0gfHw9ICRub2Rlc3skbm9kZUJ9 OwogICAgICAkbm9kZXN7JG5vZGVCfSB8fD0gJG5vZGVzeyRub2RlQX07CiAgICB9CiAgICBlbHNl CiAgICB7CiAgICAgICRub2Rlc3skbm9kZUF9ID0gJG5vZGVzeyRub2RlQn0gPSBcW107CiAgICB9 CiAgICBwdXNoIEAkeyRub2Rlc3skbm9kZUF9fSwgJGVkZ2U7CiAgfQogIG15ICVwYXJ0aXRpb25z ID0gKCk7CiAgZm9yZWFjaCAodmFsdWVzICVub2RlcykKICB7CiAgICAkcGFydGl0aW9uc3skJF99 ID0gW3NvcnQgeyRlZGdlc09yZGVyeyRhfSA8PT4gJGVkZ2VzT3JkZXJ7JGJ9fSBAeyQkX31dIHVu bGVzcyBleGlzdHMgJHBhcnRpdGlvbnN7JCRffTsKICB9CiAgcmV0dXJuIFtzb3J0IHskZWRnZXNP cmRlcntAeyRhfVswXX0gPD0+ICRlZGdlc09yZGVye0B7JGJ9WzBdfX0gdmFsdWVzICVwYXJ0aXRp b25zXTsKfSAKMTsK ------=_Part_52360_32033584.1168271780142--