so I have a test assessment that I need to do their is one question that I have been having trouble. I need to pick the heaviest object(the biggest number) and place them in stacking order (heavy on bottom, light on top) the problem that I'm having is that I don't know where to start exactly, the code that I'm using is C#, However I do know that I have to use the sorting method. so my main issue is that I'm trying to find out what my beginning move is and how to progress from their. Also I believe this may be on codin games but have not yet to find it anywhere, if anyone can find the name of it that would also be of great help.
CodePudding user response:
I think that in this case, calling Sort
is an overkill, as it is used on IEnumerable
s, which you dont have and need to create.
I think that it would be easier to just use this page to learn from: https://www.tutorialspoint.com/Chash-program-to-find-the-maximum-of-three-numbers
It looks like sorting would not help in that case as you need to return the index of the weight, not the size.
You could try to create a dictionary with index and weight and use linq to find the biggest weight:
public static int GetHeaviestWeightIndex(int weight1, int weight2, int weight3)
{
var weightToIndexMapping = new Dictionary<int, int> {{weight1, 0}, {weight2, 1}, {weight3, 2}};
return weightToIndexMapping[weightToIndexMapping.Keys.Max()];
}
If you insist on using Sort
you can try and use a SortedDictoinary
like here: https://www.geeksforgeeks.org/sorteddictionary-class-in-c-sharp/
CodePudding user response:
Without any allocation, collections and lists:
using System;
public class Program
{
public static void Main(string[] args)
{
var a = 13;
var b = 14;
var c = 15;
Console.WriteLine(Solve(a, b, c));
}
public static int Solve(int weight0, int weight1, int weight2)
{
return weight0 > weight1
? weight0 > weight2
? 0 : 2
: weight1 > weight2
? 1 : 2;
}
}
Mind that it does not check for negatives.