I have an assessment. I tried many times to solve this. But I couldn't. Please someone help me. Here is the assessment:
The next function should return the smallest integer which is superior to n and whose digits are all different from all of n' s digits.
For example next(654321) should return 700000.
if no such integer exists then the function must return -1.
write the body of the next(n) function
Note: n is a strictly positive integer lower than 2^31
Try this simple Console App. I wrote in C# (you specified both C# and Java in question tags).
Note: updated function further on
Version 1: using Lists and Enumberable operations
internal class Program
{
private static readonly double maxValue = Math.Pow(2, 31); // Max allowed value for function
private static void Main(string[] args)
{
while (true)
{
Console.WriteLine("Please specify start value.");
int startValue = Convert.ToInt32(Console.ReadLine());
int result = YourAssesmentFunction(startValue);
Console.WriteLine($"Your function result is {result}.");
}
}
/// <summary>
/// The next function should return the smallest integer which is superior to n and whose digits are all different from all of n' s digits.
/// For example next(654321) should return 700000.
/// if no such integer exists then the function must return -1.
/// write the body of the next(n) function
/// Note: n is a strictly positive integer lower than 2^31
/// </summary>
private static int YourAssesmentFunction(int startNumber)
{
List<char> startChars = new(); // List of char (numbers) contained in given number
List<char> newChars = new(); // List of char (numbers) contained in next number
IEnumerable<char>? inCommon = null; // IEnumerable of char used to find common chars
int nextNumber = -1; // Initialize result with default value -1
startNumber.ToString().ToList().ForEach(x => startChars.Add(x)); // Use Linq to populate list of digits in given number
while (inCommon == null || inCommon.Any()) // The first execution has inCommon == null as entry condition
{
if (startNumber >= maxValue || startNumber < 0) // If maxValue has been reached or given number is less than 0, exit while loop
break;
nextNumber = startNumber + 1;
newChars.Clear(); // Clear the list from previous values
nextNumber.ToString().ToList().ForEach(x => newChars.Add(x)); // Use Linq to populate list of digits in next number
inCommon = startChars.Intersect(newChars).ToList(); // Use Linq to find common digits
startNumber++; // Increment startNumber for the next while loop execution
}
return nextNumber;
}
}
The code you need is in YourAssesmentFunction()
. Please note that I'm a self-made student programmer and there might be much better ways to write this function.
EDIT: After Klaus Gutter's comment, I tried to find a simpler, faster and more elegant way of writing the same function. Here is it:
Version 2: using loop through digits in trying number
private static int YourAssesmentFunctionOptimized(int startNumber)
{
int tryNext = startNumber + 1; // Next number to try
// String representation of given number
string start = startNumber.ToString();
// Flags for while-loop managing
bool numberFound = false;
bool skipToNext;
while (!numberFound)
{
if (tryNext > maxValue | startNumber < 0) // If number to try is greater than maxValue (2^31) or start number is less than 0, exit and return 0
return -1;
skipToNext = false;
foreach (char c in tryNext.ToString()) // Loop through chars (= digits) in string representation of trying number
{
if (start.Contains(c))
{
tryNext++; // If a digit of the trying number is found in given number, the trying number is not the result
skipToNext = true;
break; // Exit foreach loop and skip to next number
}
}
if (!skipToNext)
numberFound = true; // If skip to next = true, numberFound remains false and while loop keep executing
}
return tryNext;
}
This function is much faster because it doesn't need to create List
s or evaluating IEnumerable
operations.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.