I am trying to convert a Python program into C#. I do not understand what is being done here.
def mincost(alg):
parts = alg.split(' ')
return sorted([cost(0, parts, 'G0 '),cost(1, parts, 'G1 ')], key=operator.itemgetter(1))[0]
def cost(grip, alg, p = '', c = 0.0, rh = True):
if (len(alg) == 0):
return (postProcess(p),c)
postprocess returns a string
cost returns multiple parameters used on the sorted() function? How are these multiple values being used by the sorted() function?
what does key=operator.itemgetter(1) do? Is this the basis for the sorting, so in this case the multiple value return of cost , it will use the value of c?
Is there a way to do this in C#?
The use of
sortedthere is a bit weird. You can easily replace that by a simple if-statement. Even weirder though is thatcostreturns justcas the second value of the return tuple. Inmincost,costis never called with a value ofcthat is not the default, socis always0.0making the sorting quite redundant. But I guess there are some missing parts about the cost function.Nevertheless, you could implement it function like this:
(untested)