Keypad Errors

August 2, 2016

The match function returns #t if the target matches the key input with 0 or 1 stuck keys, or #f otherwise:

(define (match target keyed)
  (let ((ts (digits target)) (ks (digits keyed)))
    (if (equal? ts ks) #t
      (let loop ((ds (range 10)))
        (if (null? ds) #f
          (if (equal? (remove (car ds) ts) ks) #t
            (loop (cdr ds))))))))

The first if considers the case where no keys are stuck, then the loop considers each digit in turn, removes all occurrences of the digit, and compares:

> (match 18684 164)
#t
> (match 17674 164)
#t
> (match 18684 165)
#f
> (match 18684 18685)
#f

We used digits, remove and range from the Standard Prelude. You can run the program at http://ideone.com/EJJ7oR.

Pages: 1 2

One Response to “Keypad Errors”

  1. ($pin, $v) = @ARGV;
    my %x=map{eval"$pin=~y{$_}{}dr",1}0..9;
    print exists $x{$v} ? 'MATCH' : 'NO MATCH',"\n";
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: