karlgoethebier has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I read Algorithm for "Incrementing" strings and tried to solve the problem for learning - following the recommendation by tye to use Math::Fleximal.

The specs as provided by ibm1620:

$val = 'BB'; incr($val); ==> 'BC' $val = 'BZ'; incr($val); ==> 'CB'; $val = 'ZZ'; incr($val) ==> 'BBB';

I think that "ZZ" should be incremented to "CBB".

I tried this:

#!/usr/bin/env perl use strict; use warnings; use Math::Fleximal; # use Data::Dump; use feature qw (say); my @consonants = qw (B C D F G H J K L M N P Q R S T V X Y Z); my $number = Math::Fleximal->new( "BB", \@consonants ); say $number->to_str(); say $number->base_10(); $number = $number->add("C"); say $number->to_str(); say $number->base_10(); say qq(--); $number = $number->set_value("BZ"); say $number->to_str(); say $number->base_10(); $number = $number->add("C"); say $number->to_str(); say $number->base_10(); say qq(--); $number = $number->set_value("ZZ"); say $number->to_str(); say $number->base_10(); $number = $number->add("C"); say $number->to_str(); say $number->base_10(); __END__ karls-mac-mini:monks karl$ ./fleximal.pl B 0 C 1 -- Z 19 CB 20 -- ZZ 399 CBB 400

Seems like it works as designed (or i hope so).

But in the solutions provided by other monks "ZZ" is incremented to "BBB".

What do i miss?

Update: Thank you very much to all who helped.

Update2: Reset state to [UNSOLVED]

Update3: Set state to [SOLVED]. Thanks again to all for help and patience.

Thank you very much for any hint and best regards, Karl

P.S.: Sorry, i'm pretty bad in even essential math.

«The Crux of the Biscuit is the Apostrophe»

Replies are listed 'Best First'.
Re: Incrementing strings revisited: What do i miss?
by SuicideJunkie (Vicar) on Feb 13, 2015 at 18:27 UTC

    Eh? It is just counting, with letters...

    'ZZ' comes one before 'AAA', which comes before 'BBB' which comes before 'CBB'.

    Same as 99 comes before 111 before 222 before 322

      BBB comes before BBC :-)
Re: Incrementing strings revisited: What do i miss?
by Anonymous Monk on Feb 13, 2015 at 19:27 UTC

    You're thinking about the solution in terms of the letters being the equivalent of digits in a number. B=0, C=1, etc. and your equivalent of "get the next item in the sequence" is to say "current + 1", or "add C". In that system, it doesn't make sense for ZZ+C to be BBB, since BBB=0, and it also makes sense for "BB" (00) to magically become "B" (0).

    But that's not the behavior you'd expect from a string: "BB" would stay "BB" and the next item in the sequence is BC, the same way that in the other solutions BBB comes after ZZ - that's just the next combination in a sequence of strings. Another way to think about it: In a sequence of strings, what reason would one have for skipping BBB?

      "...thinking...in terms...letters being the equivalent of digits in a number..."

      Yes. Base 20 in this case.

      "...BBB comes after ZZ..."

      So the sequence should be like this...

      my @seq_01 = qw (B C D F G H J K L M N P Q R S T V X Y Z); my @seq_02 = qw (BB CC DD FF GG HH JJ KK LL MM NN PP QQ RR SS TT VV XX YY ZZ); my @seq_03 = qw (BBB CCC DDD FFF GGG HHH JJJ KKK LLL MMM NNN PPP QQQ RRR SSS TTT +VVV XXX YYY ZZZ); my @merged_seq = (@seq_01, @seq_02, @seq_03);

      D'oh! I didn't recognize the pattern.

      Thank you very much for advice and breaking my mental block.

      Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

        Maybe I'm not understanding your @merged_seq, but do you mean to say CC will come directly after BB? I don't think that's the intent, BB would still be followed by BC, BD, etc., so in that respect the sequence is still like a counter. The point here is that at ZZ the sequence would roll over to BBB and not CBB, followed by BBC, BBD, etc.

Re: Incrementing strings revisited: What do i miss?
by Anonymous Monk on Feb 14, 2015 at 00:02 UTC
    Update2: Reset state to UNSOLVED

    Why?

    Anyway, I think what's confusing, at least to me, is that B=0; and I find what soonix said above is key (emphasis mine): "Counting B ... Z (without A) is like counting 1 ... 9 (without 0)."

    How about this:

    use feature 'state'; use Math::Fleximal; sub getnext { state $num = Math::Fleximal->new("A", [qw/A B C D F G H J K L M N P Q R S T V X Y Z/]); do { $num=$num->add("B") } until $num->to_str!~/A/; return $num->to_str; } print getnext."\n" for 1 .. 1000; __END__ B C D ... Y Z BB BC BD ... ZY ZZ BBB BBC ...
      "Why?"

      Because i didn't understand it.

      "How about this?"

      I begin to see it clearer now. And thanks for the example with state in action.

      Thank you and best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

Re: Incrementing strings revisited: What do i miss?
by soonix (Chancellor) on Feb 13, 2015 at 20:10 UTC

    let me rephrase what the others said:

    Counting B ... Z (without A) is like counting 1 ... 9 (without 0).

    Now, why would you want to go from 99(ZZ) to 211(CBB) instead of 111(BBB)?