Sorting 정렬

Often we use .sorted(some_array) and a lambda function together.

When used together, sorted() and lambda functions provide a way to define custom sorting criteria. The lambda function is used as the key parameter of sorted() to specify the sorting key for each element in the iterable. The key function takes an element as input and returns a value based on which the elements are sorted.

Practice questions

k번쨰 수

https://school.programmers.co.kr/learn/courses/30/lessons/42748

Really easy question. We don’t even have to use a lambda function for this question. But the most up-voted solution is sick

Mine:

def solution(array, commands):
    answer = []
    for command in commands:
        value = sorted(array[command[0]-1:command[1]])[command[2]-1]
        answer.append(value)
    return answer

Best code:

def solution(array, commands):
    return list(map(lambda x:sorted(array[x[0]-1:x[1]])[x[2]-1], commands))

So map applies this lambda function to each element in commands.

가장 큰 수

https://school.programmers.co.kr/learn/courses/30/lessons/42746 This is a question that I tried figuring out how to implement lambda function to sort this. I tried like

sorted_nums = sorted(nums, key=lambda x: str(x), reverse=True)

But while this lexicographically sorts strings in reverse order, I wanted 34 -> 3 -> 30 but my code gave 34->30->3. I am not getting the biggest number possible this way so how??

The problem is that digit 3 is short but what if it is extended like 33? Then my sort will sort the way i want like 34 -> 3(3) -> 30. So we just multiply the string of digit but how many times? Let’s look at the constraint for number. It is max 999 which means max 3 digits length. So for the smallest digit of length 1, we should multiply it 3 times to compare equally with the number that is length 3.

For example, 871(871871), 8(88) here 8 comes first because 888 is lexicographically greater than 871.

I looked at this reference https://jokerldg.github.io/algorithm/2021/05/06/most-big-number.html

def solution(nums):
    sorted_nums = sorted(nums, key=lambda x: str(x), reverse=True)
    # sorted_nums = sorted(nums, key=lambda x: (str(x)[0], -int(str(x)[1]) if len(str(x)) > 1 else int(str(x)[0]) > 0, -x), reverse=True)
#     sort lexicographically by multiplying str(x) 3 times
    # sorted_nums = sorted(nums, key=lambda x: str(x)*3 , reverse=True)
    answer = ''
    return str(int(''.join(str(num) for num in sorted_nums)))

Btw we have to convert it to int because of edge case like [0,0,0] where if we just return that string form, it would be “000” but the correct answer is “0”. So we convert it to int and back to str.

H-Index

https://school.programmers.co.kr/learn/courses/30/lessons/42747

I had a hard time understanding what H-index is.

def solution(citations):
    # sorted(citations,reverse=True)
    # print(citations)
    citations.sort(reverse=True)
    for index,value in enumerate(citations):
        if index>=value:
            return index
    return len(citations)
        

I saw a cool answer like

def solution(citations):
    citations.sort(reverse=True)
    answer = max(map(min, enumerate(citations, start=1)))
    return answer

Map applies the min function to pair of index and citation. Enumerate associates each citation with an index starting from value 1.