in reply to Check a string for consecutive digits

All posts at the time of writing, including the OP, seem to assume that passwords only contain digits; at least, that's all that's being tested. I see nothing at Asterisk Voicemail (or pages that links to) to indicate this restriction. Accordingly, 'a12b543c', for instance, would be valid (as a standard password) but invalid (under your consecutive digits special rules).

In the test script below, sequential digits are pulled from the whole password; canonicalised into ascending order (e.g. 'a12b543c' would produce the two sequences, '12' and '345', for checking); tested for consecutiveness; and rejected immediately any check fails.

#!/usr/bin/env perl -l use strict; use warnings; my @passwords = qw{ 1234 1243 4321 298761 4562 4568 4578 123 12 1 01234 01243 04321 0298761 04562 04568 04578 0123 012 01 a1234 1a234 12a34 123a4 1234a a1b2c3 a12b34c56 a1b2c3d a12b34c56d a123b45c6 a12b345c6 a123b45c6d a12b345c6d 1a2 1ab2 12ab34 12abc34def 12abc34def567 }; push @passwords, map { scalar reverse } @passwords; my $too_many = 3; check($_, $too_many) for @passwords; sub check { my ($pw, $too_many) = @_; if ($too_many > length $pw) { pw_ok($pw); return; } for my $pw_digit_str (split /\D+/, $pw) { my $pw_digit_str_len = length $pw_digit_str; next if $too_many > $pw_digit_str_len; OFFSET: for my $offset (0 .. $pw_digit_str_len - $too_many) { my $digits = substr $pw_digit_str, $offset, $too_many; my $rev_digits = scalar reverse $digits; my @ints = split //, $digits < $rev_digits ? $digits : $re +v_digits; my $test_int = $ints[0]; for (@ints) { if ($test_int != $_) { next OFFSET; } ++$test_int; } pw_nok($pw); return; } } pw_ok($pw); } sub pw_ok { print "Accept: $_[0]" } sub pw_nok { print "Reject: $_[0]" }

This generates 76 tests (I did note a couple of duplicates). The output is in the spoiler, below.

I've covered many edge cases. Don't assume I've caught them all.

— Ken

Replies are listed 'Best First'.
Re^2: Check a string for consecutive digits
by AnomalousMonk (Archbishop) on Nov 26, 2015 at 08:17 UTC
    All posts at the time of writing, including the OP, seem to assume that passwords only contain digits; at least, that's all that's being tested.

    The code above produces the same output as yours with your test set and suitable modification to the print statement and  $min set to 3.


    Give a man a fish:  <%-{-{-{-<

      OK, that's good.

      FWIW, you hadn't posted when I started to write my response but, clearly, you did post before I did.

      "All posts at the time of writing" did not include your post; however, I can see that isn't obvious.

      My apologies for any unintended confusion.

      — Ken

        No problem, nor any need for apology. I just wanted to mention that my approach would also work with the expanded test set. I think it's important to stress your point that any approach to this problem requires a testing framework that can easily exercise all the common, degenerate and corner cases.


        Give a man a fish:  <%-{-{-{-<

Re^2: Check a string for consecutive digits
by Anonymous Monk on Nov 26, 2015 at 10:09 UTC
    All posts at the time of writing, including the OP, seem to assume that passwords only contain digits; at least, that's all that's being tested.
    That's not true, only the OP's method assumes that. All others work with any passwords.
      "That's not true, ..."

      It appears I missed the test data:

      qw{ abc ab12c 2345 234y 01234 }

      from johngg's post.

      I've rechecked and I'm pretty sure that's the only one with test data not containing only digits.

      "All others work with any passwords."

      While that maybe true, I've no idea why you thought it was important to state it. I made no reference to what code did or didn't work. I did comment on test data.

      — Ken

Re^2: Check a string for consecutive digits
by Anonymous Monk on Nov 26, 2015 at 17:30 UTC
    Thanks for the reply and the code. This is a password that's sent over the phone, so has to be entered with the phone keypad. I'll grant you that A, B, C, and D are all valid DTMF "digits" but I've yet to sell a phone that can produce them!