in reply to Perl Golf idea

Why don't you go ahead and post a Perl version of your own here, just for a bit of fun. The worst you could do is just transcribe the C version over to a Perl equivalent, and then you'd be showing us a target to try to best.


Dave

Replies are listed 'Best First'.
Re^2: Perl Golf idea
by sweepy838 (Acolyte) on Apr 27, 2012 at 22:00 UTC
    haha yes the WORST i could do is convert it to perl! im ashamed of posting it yet.. im trying to make it better. but here is the c version:
    string myword; string wlist = "dict.txt";string line; int strLen = myword.length( +);int c = 0; ifstream cfile(wlist); if (cfile.is_open()) { while (cfile.good()) { getline (cfile,line); if (strLen == line.length()) { bool match[strLen];string used[strLen];int matchcount = + 0; for (int i = 0; i < strLen; i++) { string ch = myword.substr(i, 1); for (int y = 0; y < strLen; ++y) { string kh = line.substr(y, 1); if (ch == kh) { if (used[y] == ch) match[matchcount] = 0; else { used[y] = ch; match[matchcount] = 1; matchcount++; break; } } } } c = 0; for (int t = 0; t < strLen; t++){if (match[t] == true) + ++c; } if (c == strLen) { return line; } delete [] match; } } cfile.close(); }
    I have a feeling that people think i'm getting them to do my "homework" ? seriously?

      Rewritten in Cerl (C++ transliterated into something that a Perl interpreter in a good mood may care to execute):

      my $myword; my $wlist = "dict.txt"; my $strLen = length $myword; open my $cfile, '<', $wlist or die "Can't open $wlist: $!\n"; while (defined (my $line = <$cfile>)) { next if $strLen != length $line; my @match; my @used; my $matchcount = 0; for (my $i = 0; $i < $strLen; $i++) { my $ch = substr $myword, $i, 1; for (my $y = 0; $y < $strLen; ++$y) { next if $ch ne substr $line, $y, 1; if ($used[$y] eq $ch) { $match[$matchcount] = 0; } else { $used[$y] = $ch; $match[$matchcount] = 1; $matchcount++; last; } } } my $c = 0; for (my $t = 0; $t < $strLen; $t++) { ++$c if ($match[$t]); } return $line if ($c == $strLen); } close $cfile;

      This is of course completely untested and not what I would consider to be Perlish, although it is strict compliant. Note that the minor structure changes I made could actually be pushed back to the original C++ code to clean up that code somewhat.

      True laziness is hard work