Undulant numbers

An undulant number is a number where its digits go up and down like the following number: 461902 or 708143, or even 1010101, but not 123, because 2 < 3. write function which returns true if a number is undulant, false otherwise. The shortest function wins.

NOTE: For completeness, single digit numbers are accepted by the function and the function would return false (I imagine this case as non-undulant number), thus isUndulant returns false for n < 10.

-
Number input as string, integer, float...? – leftaroundabout Jul 11 '11 at 19:07
What's the objective here? Code-golf (shortest function)? – Alexandru Jul 11 '11 at 20:22
@JBernardo: I would think True or undefined behavior, as it would be a better base case for recursion. – Joey Adams Jul 11 '11 at 21:26
Your definition of undulant number is not in agreement with the standard definition: mathworld.wolfram.com/UndulatingNumber.html. Is this intentional? – mellamokb Jul 14 '11 at 12:03
My solution could be 16% smaller if the base case were true (as would make sense IMHO). – eternalmatt Jul 15 '11 at 23:22

APL, Dyalog (43)

ASCII is way too limited! :P

``````{⍵≤9:0⋄(1 ¯1⍴⍨⍴A)≡(⊃A)×A←×(⍎¨¯1↓⍕⍵)-⍎¨1↓⍕⍵}
``````

In standard APL fashion, it returns 1 for true and 0 for false.

Usage:

``````  {⍵≤9:0⋄(1 ¯1⍴⍨⍴A)≡(⊃A)×A←×(⍎¨¯1↓⍕⍵)-⍎¨1↓⍕⍵}461902
1
{⍵≤9:0⋄(1 ¯1⍴⍨⍴A)≡(⊃A)×A←×(⍎¨¯1↓⍕⍵)-⍎¨1↓⍕⍵}708143
1
{⍵≤9:0⋄(1 ¯1⍴⍨⍴A)≡(⊃A)×A←×(⍎¨¯1↓⍕⍵)-⍎¨1↓⍕⍵}123
0
{⍵≤9:0⋄(1 ¯1⍴⍨⍴A)≡(⊃A)×A←×(⍎¨¯1↓⍕⍵)-⍎¨1↓⍕⍵}6
0
``````

Explanation:

• `⍵≤9:0⋄`: If the argument is smaller or equal to `9`, it's not an undulant number. Otherwise...
• `⍎¨1↓⍕⍵`: Turn number into string (`⍕⍵`), drop the first char (`1↓`), now turn each individual digit back into a number (`⍎¨`), giving a list of numbers. (`123` => `[2, 3]`)
• `⍎¨¯1↓⍕⍵`: Same as above, but drop the last digit instead.
• `A←×(⍎¨¯1↓⍕⍵)-⍎¨1↓⍕⍵`: These two lists of numbers are subtracted from each other. So if the input was `132`, you get `[1,3] - [3,2]`, which is `[-2,1]`. The sign is taken, giving `[-1,1]`, and this is stored in variable `A`.
• `(⊃A)×A`: Each number in list `A` is multiplied by the first element of list `A`. If the number is undulant, we now have a list of the format `[1, -1, 1, ...]`.
• `(1 ¯1⍴⍨⍴A)`: This is a list of the format `[1, -1, 1, ...]`, with the length of list `A`.
• `≡`: Compare these lists. If they are equal, the input number was undulant, otherwise it wasn't.

So why is `⍵≤9:0⋄` necessary? Otherwise, a one-digit input would result in the function trying to subtract the list `[]` ('6' with the first digit dropped) from the list `[]` ('6' with the last digit dropped). This gives a `LENGTH ERROR`.

-

Python (79 77 69)

``````a=raw_input()
print{cmp(a[i+1],a[i])for i in range(len(a)-1)}=={-1,1}
``````
-
 Incorrect. Try 1221. – Steven Rumbalski Jun 23 '12 at 14:19 No, 1221 works. It returns True. – beary605 Jun 23 '12 at 15:55 It should return false. – Steven Rumbalski Jun 24 '12 at 4:32 Fixed it. Now it's one single formula. :) – beary605 Jun 24 '12 at 6:21 12321 should return False. – Steven Rumbalski Jun 25 '12 at 15:07

Mathematica 46

``````#!=Sort@#&&#!=Reverse@Sort@#&[IntegerDigits@n]
``````

Examples (spaces are not required):

``````# != Sort@# && # != Reverse@Sort@# &[IntegerDigits@5]
# != Sort@# && # != Reverse@Sort@# &[IntegerDigits@123]
# != Sort@# && # != Reverse@Sort@# &[IntegerDigits@132]
# != Sort@# && # != Reverse@Sort@# &[IntegerDigits@321]

(*  out *)
False  False  True  False
``````
-

Python 2.7, 89 chars

``````def f(a):z=[cmp(*x)for x in zip(a[1:],a)];return{(a&b)-(a^b)for a,b in zip(z[1:],z)}=={3}
``````
-

C, 78

``````u(i){int d,p=0;while(i>9){d=i%10-(i/=10)%10;if(d*p>0|!d)i=d=0;p=d;}return!!p;}
``````

expanded code with test harness:

``````u(i){
int d,p=0;
while(i>9) {
d=i%10-(i/=10)%10;
if(d*p>0|!d)i=d=0;
p=d;
}
return !!p;
}

void test(int i) {
printf("%8d = %d\n", i, u(i));
}

int main(int argc, char *argv[]) {
test(132);
test(123);
test(5);
test(1332);
test(176823);
}
``````

(compiled with Apple clang version 3.1)

results:

``````     132 = 1
123 = 0
5 = 0
1332 = 0
176823 = 1
``````
-
 Apparently `u(132)` is `0`. Am I doing something wrong? ideone.com/MMrfM – w0lf Jun 26 '12 at 7:15 while I spotted an error in my first version, it was still returning 1 for u(132) for me - odd. – baby-rabbit Jun 26 '12 at 10:25