Dear Masters,
Given a seed string, I want to find its neighbors with at most differ in 2 positions. All the digits involve in generating string are only four (i.e. 0,1,2,3). This is the example for what I mean:
# In this example, 'first' column are neighbours with only 1 position differ. The rest of the columns are 2 positions differ Seed = 000 100 110 120 130 101 102 103 200 210 220 230 201 202 203 300 310 320 330 301 302 303 010 011 012 013 020 021 022 023 030 031 032 033 001 002 003 Seed = 001 101 111 121 131 100 102 103 201 211 221 231 200 202 203 301 311 321 331 300 302 303 011 010 012 013 021 020 022 023 031 030 032 033 000 003 002 Hence given a tag of length L we will have 3*L + 9L(L-1)/2 neighbors
But why this code of mine fails to generate it correctly? Especially when the seed string is other than "000".
#!/usr/bin/perl -w use strict; use Data::Dumper; use Carp; # If possible we prefer not to use any CPAN module # or regex. It's for my understanding my $seed = $ARGV[0] || "000"; # Seed can be longer than 3 digits but always 0,1,2,3 for (my $i = 0; $i < length($seed); $i++) { # First loop is to generate 1 position differ for (my $bs=1; $bs<=3; $bs++) { my $bval = $bs; if ( substr($seed,$i,1) == $bs) { $bval = 0; } my $ns = $seed; substr($ns,$i,1,$bval); print "$ns\n"; # Second loop for tags in 2 positions differ for (my $j=($i+1); $j < length($seed); $j++) { for (my $cs = 1; $cs<=3;$cs++) { my $cval = $cs; if (substr($ns,$j,1) == $cs) { $cval = $cs; } my $ns2 = $ns; substr($ns2,$j,1,$cval); print "\t$ns2\n"; } } }


---
neversaint and everlastingly indebted.......

In reply to Finding String's Neighbors By Up To 2 Differing Positions by neversaint

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.