thank you very much guys ... i was hoping that there is an algebraic formula that would enable me to do this check but seems pretty tough without using complex algorithm (regex is doing very complex stuff behind the scene ...)
If you think this hard enough, you will find a formula that does the trick. It will be lots of modula, division, brackets but it can be done.
As for an one liner, Hien's solution is a great way to think about it. I bet you can write something like that in excel, matlab and even dos.
Lisp tackles such mathematical problems easier. I am a little rusty but here is a simple solution.
; Consume a list of single digits, subtract 6 from each item, and filter out the negative values
(define (reduce-list list)
[(and (>= (- (first list) 6) 0) (empty? (rest list))) (- (first list) 6)]
[(>= (- (first list) 6) 0) (cons (- (first list) 6) (reduce-list (rest list)))]
[else (reduce-list (rest list))]))
; Go through the list and find if two successive numbers have the first number greater than the second one.
(define (compute list)
[(or (empty? (rest list)) (empty? (first list))) false]
[(> (first list) (first (rest list))) true]
[else (compute (rest list))]))
; Test it
(compute (reduce-list '(3 7 6 5)))
Slightly more complex, if you use higher order functions, solving it in a single expression is possible:
uses some generic higher order functions, definitions as follows:
foldr : (X Y -> Y) Y (listof X) -> Y
(foldr f base (list x-1 ... x-n)) = (f x-1 ... (f x-n base))
map : (X -> Y) (listof X) -> (listof Y)
to construct a list by applying f to each item on alox
that is, (map f (list x-1 ... x-n)) = (list (f x-1) ... (f x-n))
filter : (X -> boolean) (listof X) -> (listof X)
to construct a list from all those items on alox for which p holds
(define list '(3 7 6 5))
(foldr (lambda (a b) (cond [(> a b) true]
[(equal? b true) true]
(filter (lambda (a) (>= a 0)) (map (lambda (a) (- a 6)) list)))