# Does this problem have a solution?

#### kmer

What is the formula to check if a number string includes a number > 6 followed by a number > 5?

ex: 3765 is fine because 7 > 6 and 6 > 5
116 is not good 1 < 6
but 189 is good because 8 > 6 and 9 > 5

#### Andy Nguyen

I don't know any formula but you can write some quick C++ code to check this.
C++:
[COLOR=Blue] #include[/COLOR][COLOR=Sienna]<iostream>[/COLOR]
[COLOR=Blue] void[/COLOR] main()
{
[COLOR=Blue]int[/COLOR] number=0,tmp1=0, tmp2=0, x =0;
std::cout<<[COLOR=Sienna]"Enter the number: "[/COLOR];
std::cin>>number;
x=number; //the original number
[COLOR=Blue]for[/COLOR](;number>0;)
{
tmp1=number%10;
number /=10;
tmp2=number%10;
[COLOR=Blue] if[/COLOR](tmp1 > 5 && tmp2 >6)//Check the conditions here.
{
std::cout<<x<<" [COLOR=Sienna]IS GOOD\n[/COLOR]";
[COLOR=Blue]break[/COLOR];
}
[COLOR=Blue]if [/COLOR](number ==0)//At the end now.
{
std::cout<<x<<" [COLOR=Sienna]IS NOT GOOD\n[/COLOR]";
}
}
std::cin>>tmp1;//stops the console from closing too quickly
}
Here is the result
C++:
Enter the number: 3765
3765 IS GOOD
C++:
Enter the number: 1567
1567 IS NOT GOOD

#### Hien

You can also use regular expressions for this sort of things. It's pretty powerful and can be used to match very complicated patterns.

Regular expression - Wikipedia, the free encyclopedia

So for your case, the regex would be something like: .*[6-9][5-9].*

If you have a unix terminal, you can play with this very easily. Here is a quick test for the above regex:

C++:
java:~ $echo "1234" | egrep -i '.*[6-9][5-9].*' java:~$echo "12347" | egrep -i '.*[6-9][5-9].*'
java:~ $echo "123475" | egrep -i '.*[6-9][5-9].*' 123475 java:~$echo "123474" | egrep -i '.*[6-9][5-9].*'
java:~ $echo "123479" | egrep -i '.*[6-9][5-9].*' 123479 java:~$echo "123479343" | egrep -i '.*[6-9][5-9].*'
123479343
java:~ \$

#### kmer

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 ...)

#### Andy Nguyen

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.

#### samson

Lisp tackles such mathematical problems easier. I am a little rusty but here is a simple solution.

C++:
; Consume a list of single digits, subtract 6 from each item, and filter out the negative values
(define (reduce-list list)
(cond
[(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)
(cond
[(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

C++:
(define list '(3 7 6 5))

(foldr (lambda (a b) (cond [(> a b) true]
[(equal? b true) true]
[else a]))
10
(filter (lambda (a) (>= a 0)) (map (lambda (a) (- a 6)) list)))

Replies
0
Views
352
Replies
7
Views
844
Replies
42
Views
7K
Replies
2
Views
1K
Replies
6
Views
2K