Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #141915

Re: Anagrams (was: Re: Another Crackpot Production...)

From Eli the Bearded <*@eli.users.panix.com>
Newsgroups comp.lang.c
Subject Re: Anagrams (was: Re: Another Crackpot Production...)
Date 2019-03-13 00:22 +0000
Organization Some absurd concept
Message-ID <eli$1903121946@qaz.wtf> (permalink)
References <q687cl$pvs$1@toylet.eternal-september.org> <aef0988f-9023-4294-96d1-f1f4ba1d214b@googlegroups.com> <q68gi2$kgc$1@news.xmission.com> <5SVhE.204037$U64.101380@fx20.am4>

Show all headers | View raw


In comp.lang.c, Bart  <bc@freeuk.com> wrote:
> On 12/03/2019 14:43, Kenny McCormack wrote:
>> Ray Charles live <--> Silvary lecher
>      #include <stdio.h>
>      #include <string.h>
> 
>      int isanagram(char* s, char* t) {
>          enum {maxstrlen=255};
>          int slen=strlen(s), tlen=strlen(t), found;
>          char sflags[maxstrlen];
> 
>          if (slen!=tlen) {
>              return 0;
>          }
>          if (slen>maxstrlen) {
>              return 0;
>          }
>          memset(sflags,0,slen);
> 
>          for (int i=0; i<slen; ++i) {
>              found=0;
>              for (int j=0; j<tlen; ++j) {
>                  if (s[i]==t[j] && sflags[i]==0) {
>                      sflags[i]=1;
>                      found=1;
>                      break;
>                  }
>              }
>              if (found==0) {
>                  return 0;
>              }
>          }
>          return 1;
>      }
> 
>      int main(void) {
>          char* s = "raycharleslive";
>          char* t = "silvarylecher";
> 
>          printf("%s:%s => %s\n",s,t,(isanagram(s,t)?"anagrams":
>                 "not anagrams"));
>      }
> 
> Not an anagram according to this (rather obvious when you notice they're 
> not the same length).

It's not much of a test when it fails out due to the string length
mismatch.

> (Alternative algorithm: sort s and t, and compare for equality. The 
> above was simpler to write.)

I wrote one in Perl in February 2012. Here's my code, which uses a
slightly different method. Compared to your code, it would be like
dispensing with sflags[] and replacing s[] entries with a space when
the letters are used:

$ remove.pl 'Ray Charles live' 'Silvary lecher'
Unused letters: a
Source string: raycharleslive
Drop letters from: silvarylecher
$ cat remove.pl
#!/usr/local/bin/perl -w
# usage:
# 	remove [options] SOURCE DROP
# For a string SOURCE remove the characters in DROP
# Options:
# 	-i	do NOT ignore case
#	-d	don't warn if a character in DROP isn't found in SOURCE

use strict;
use vars qw( $source $drop $out $char $ic $rc $dw $miss );

$ic = $dw = $miss = '';
$rc = 1;

while( defined($_ = $ARGV[0]) ) {

  if (/^-i$/) { $ic = 0 ; shift; next; }
  if (/^-d$/) { $dw = 1 ; shift; next; }

  if (/^-/) { die qq($0: $_: unknown argument, run with no args for help\n); }
  
  if(!defined($source)) { $source = shift; next; }
  if(!defined($drop))   { $drop   = shift; next; }

  die qq($0: $_: unknown parameter\n);
}

if(!defined($drop)) {
  die qq($0: usage:\n) .
      qq(  remove [options] SOURCE DROP\n) .
      qq(For a string SOURCE remove the characters in DROP\n) .
      qq(Options:\n) .
      qq(  -i      do not ignore case\n) .
      qq(  -d      don't warn if a character in DROP isn't found in SOURCE\n);
}

unless($ic) {
  $source = lc($source);
  $drop   = lc($drop);
}

$source =~ s/\W+//g;
$drop   =~ s/\W+//g;

$out = $source;
$_   = $drop;
while(s/^(.)//) {
  $char = $1;

  if($out =~ s/${char}//) {
    1; # good
  } else {
    $miss .= $char;
  }
}

if(length($out)) {
   print qq(Unused letters: $out\n);
} else {
   if(length($miss)) {
     print qq(All letters used, but there are extras.\n);
   } else {
     print qq(Anagram found\n);
     $rc = 0;
   }
}
if($dw) {
  exit $rc;
}

if(length($miss)) {
  print qq(Letters missing from source: $miss\n);
}

print qq(Source string: $source\n);
print qq(Drop letters from: $drop\n);

exit $rc;
__END__
$

I find it helpful to have the complete string (with whitespace,
punctuation, and capitalization) as inputs, so I don't risk making a
mistake transcribing and so I can just cut-n-paste the arguments from
the script.

And, obviously, most of the code is dealing with input and output, the
actual test is very simple.

Somewhere, not handy, I have code I used to compare sentences against
each other en masse. That used the alphabetization comparison. I then
collected "sentences" from books in the Gutenberg Project and looked for
in-the-wild anagrams. I put results here:

http://qaz.wtf/anagramhunt/show_random.cgi

An example good find:

http://qaz.wtf/anagramhunt/showpair.cgi?pair=938

An example bad find:

http://qaz.wtf/anagramhunt/showpair.cgi?pair=538

I filtered the obvious word rearrangement ones, but that filtering isn't
clever enough to notice a compound word spelled as two words, an issue
with historic texts like in Gutenberg ("to-morrow" versus "tomorrow"
for example or, apparently, numbers in the strings [pair=344]). This
gets you a reasonable number of bad finds from, say, the two slightly
different copies of _Huckleberry Finn_ in Gutenberg.

Elijah
------
was inspired by the bot that searched for anagram pairs in tweets

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

As the Web Turns 30, Its Inventor Explains 3 Ways It Went Wrong—and How to Fix It "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2019-03-12 20:06 +0800
  Re: As the Web Turns 30, Its Inventor Explains 3 Ways It Went Wrong—and How to Fix It gazelle@shell.xmission.com (Kenny McCormack) - 2019-03-12 12:12 +0000
    Re: As the Web Turns 30, Its Inventor Explains 3 Ways It Went Wrong—and How to Fix It "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2019-03-12 20:25 +0800
      Re: As the Web Turns 30, Its Inventor Explains 3 Ways It Went Wrong—and How to Fix It Martin Edwards <buzzard554@fastmail.co.uk> - 2019-03-13 07:14 +0000
        Re: As the Web Turns 30, Its Inventor Explains 3 Ways It Went Wrong—and How to Fix It "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2019-03-13 19:39 +0800
        Re: As the Web Turns 30, Its Inventor Explains 3 Ways It Went Wrong—and How to Fix It Wolf K <wolfmac@sympatico.ca> - 2019-03-13 10:59 -0400
          Re: As the Web Turns 30, Its Inventor Explains 3 Ways It Went Wrong—and How to Fix It Martin Edwards <buzzard554@fastmail.co.uk> - 2019-03-14 07:18 +0000
    Re: As the Web Turns 30, Its Inventor Explains 3 Ways It Went Wrong—and How to Fix It Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-03-12 06:40 -0700
    Re: As the Web Turns 30, Its Inventor Explains 3 Ways It Went Wrong—and How to Fix It "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-03-12 07:32 -0700
      Another Crackpot Production... gazelle@shell.xmission.com (Kenny McCormack) - 2019-03-12 14:43 +0000
        Re: Another Crackpot Production... "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-03-12 08:24 -0700
          Re: Another Crackpot Production... fir <profesor.fir@gmail.com> - 2019-03-12 09:30 -0700
            Re: Another Crackpot Production... fir <profesor.fir@gmail.com> - 2019-03-12 09:42 -0700
            Re: Another Crackpot Production... "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-03-12 09:54 -0700
              Re: Another Crackpot Production... fir <profesor.fir@gmail.com> - 2019-03-12 10:10 -0700
                Re: Another Crackpot Production... "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-03-12 10:29 -0700
                Re: Another Crackpot Production... fir <profesor.fir@gmail.com> - 2019-03-12 10:45 -0700
        Anagrams (was: Re: Another Crackpot Production...) Bart <bc@freeuk.com> - 2019-03-12 21:51 +0000
          Re: Anagrams (was: Re: Another Crackpot Production...) Eli the Bearded <*@eli.users.panix.com> - 2019-03-13 00:22 +0000
            Re: Anagrams Bart <bc@freeuk.com> - 2019-03-13 11:20 +0000
              Re: Anagrams Eli the Bearded <*@eli.users.panix.com> - 2019-03-14 20:25 +0000
                Re: Anagrams Learn to Filter you e-tard <cdalten@gmail.com> - 2019-03-15 08:02 -0700
                Re: Anagrams Wouter Verhelst <w@uter.be> - 2019-03-15 17:11 +0200
          Re: Anagrams (was: Re: Another Crackpot Production...) Ike Naar <ike@iceland.freeshell.org> - 2019-03-13 07:28 +0000
            Re: Anagrams Bart <bc@freeuk.com> - 2019-03-13 10:50 +0000
          Re: Anagrams Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-03-13 11:51 +0000
            Re: Anagrams Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-03-13 06:10 -0700
              Re: Anagrams Bart <bc@freeuk.com> - 2019-03-13 14:50 +0000
              Re: Anagrams Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-03-13 15:15 +0000
                Re: Anagrams Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-03-13 08:42 -0700
                Re: Anagrams Bart <bc@freeuk.com> - 2019-03-13 16:17 +0000
                Re: Anagrams Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-03-14 01:23 +0000
  2001: A Space Odyssey - HAL 9000 "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2019-03-13 20:28 +0800
    Re: 2001: A Space Odyssey - HAL 9000 Martin Edwards <buzzard554@fastmail.co.uk> - 2019-03-14 07:19 +0000
      Re: 2001: A Space Odyssey - HAL 9000 "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2019-03-14 22:30 +0800
        Re: 2001: A Space Odyssey - HAL 9000 gazelle@shell.xmission.com (Kenny McCormack) - 2019-03-14 14:32 +0000
          Re: 2001: A Space Odyssey - HAL 9000 "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-03-14 07:50 -0700
          Re: 2001: A Space Odyssey - HAL 9000 "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2019-03-15 00:16 +0800

csiph-web