Find the k largest elements in order

One option would be the following: Using a linear-time selection algorithm like median-of-medians or introsort, find the kth largest element and rearrange the elements so that all elements from the kth element forward are greater than the kth element. Sort all elements from the kth forward using a fast sorting algorithm like heapsort or quicksort. … Read more

Custom sort python [duplicate]

Your first link more or less solves the problem. You just need to have the lambda function only look at the first item in your list: alphabet = “zyxwvutsrqpomnlkjihgfedcba” new_list = sorted(inputList, key=lambda word: [alphabet.index(c) for c in word[0]]) One modification I might suggest, if you’re sorting a reasonably large list, is to change the … Read more

How to define (and name) the corresponding safe term comparison predicates in ISO Prolog?

iso_dif/2 is much simpler to implement than a comparison: The built-in \= operator is available You now exactly what arguments to provide to\= Definition Based on your comments, the safe comparison means that the order won’t change if variables in both subterms are instanciated. If we name the comparison lt, we have for example: lt(a(X), … Read more