Vidya
Vidya

Reputation: 657

find the stack rank between two values

I have below input as list

input_list = ['Machine1:500:1', 'Machine2:500:2', 'Machine3:100:1', 'machine4:400:1', 'Machine5:600:10', 'Machine6:600:1', 'Machine7:600:8', 'Machine8:599:1']

Element Format is:

Machine1: core number: Reliability number

sort based on good Machine

Condition for Good Machine rule is

Max number core and lowest Reliability number is considered as Good machine 

what i tried is :

input_list = ['Machine1:500:1', 'Machine2:500:2', 'Machine3:100:1', 'machine4:400:1', 'Machine5:600:10', 'Machine6:600:1', 'Machine7:600:8', 'Machine8:599:1']


def stack_rank(input_list):
    for i in range(0, len(input_list) - 1):
        for j in range(0, len(input_list) - 1):
            # high to low
            core_sort = int(input_list[j].split(":")[1]) < int(input_list[j + 1].split(":")[1])

            if core_sort:
                input_list[j], input_list[j + 1] = input_list[j + 1], input_list[j]

    for i in range(0, len(input_list) - 1):
        for j in range(0, len(input_list) - 1):
            # low to high
            rb_sort = int(input_list[j].split(":")[2]) > int(input_list[j + 1].split(":")[2])

            if rb_sort:
                input_list[j], input_list[j + 1] = input_list[j + 1], input_list[j]





stack_rank(input_list)
print(input_list)

Output:

  ['Machine6:600:1', 'Machine8:599:1', 'Machine1:500:1', 'machine4:400:1', 'Machine3:100:1', 'Machine2:500:2', 'Machine7:600:8', 'Machine5:600:10']

Output is not valid base on the condition rule

Upvotes: 0

Views: 37

Answers (1)

sushanth
sushanth

Reputation: 8302

try this,

def cmp(x):
    split_ = x.split(":")
    return int(split_[2]), -int(split_[1])

sorted(input_list, key=lambda x: cmp(x))

Upvotes: 2

Related Questions