logo CodeStepByStep logo

DigitsSorted

Language/Type: C# recursion string return

Write a recursive method named DigitsSorted that takes an integer as a parameter and returns true if the digits of the integer are sorted and false otherwise. The digits must be sorted in non-decreasing order (i.e. increasing order with duplicate digits allowed) when read from left to right. An integer that consists of a single digit is sorted by definition. The method should be also able to handle negative numbers. Negative numbers are also considered sorted if their digits are in non-decreasing order.

The following table shows several calls to your method and their expected return values:

Call Value Returned
DigitsSorted(0) true
DigitsSorted(2345) true
DigitsSorted(-2345) true
DigitsSorted(22334455) true
DigitsSorted(-5) true
DigitsSorted(4321) false
DigitsSorted(24378) false
DigitsSorted(21) false
DigitsSorted(-33331) false

Constraints: Do not declare any global variables. Do not use any loops; you must use recursion. Do not use any auxiliary data structures like List, SortedDictionary, SortedSet, array, etc. Also do not solve this problem using a string. You can declare as many primitive variables like ints as you like. You are allowed to define other "helper" methods if you like; they are subject to these same constraints.

Method: Write a C# method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.