Earn extra honor and gain new allies!
Honor is earned for each new codewarrior who joins.
Learn more
Sorting
Algorithms

Added tests

Code
Diff
  • def sort_by_salary(workers):
        return tuple(x[0] for x in sorted([x for x in workers if x[1]!='-'], key=lambda x: int(x[1]), reverse=True)[:5])
  • 11
    def sort_by_salary(workers):
    
    2
        filtered = list(filter(lambda x: x[1] != "-", workers))
    
    3
        if not filtered:
    
    4
            return tuple()
    
    5
        new =  list(sorted(filtered, key=lambda x: int(x[1])))[::-1]
    
    6
        return tuple(i[0] for i in new)[:5]
    
    2+
        return tuple(x[0] for x in sorted([x for x in workers if x[1]!='-'], key=lambda x: int(x[1]), reverse=True)[:5])
    
Code
Diff
  • from itertools import accumulate
    from operator import mul
    
    def prod(arr):
        if arr:
            for x in accumulate(arr, mul):
                pass
            return x
        return 1
  • 1
    from math import prod
    
    1+
    from itertools import accumulate
    
    2+
    from operator import mul
    
    3+
    4+
    def prod(arr):
    
    5+
        if arr:
    
    6+
            for x in accumulate(arr, mul):
    
    7+
                pass
    
    8+
            return x
    
    9+
        return 1
    
Code
Diff
  • import heapq
    def find_max(arr):
        arr = [-x for x in arr]
        heapq.heapify(arr)    
        return -arr[0]
  • 1
    def find_max(arr) : return max(arr)
    
    1+
    import heapq
    
    2+
    def find_max(arr):
    
    3+
        arr = [-x for x in arr]
    
    4+
        heapq.heapify(arr)    
    
    5+
        return -arr[0]
    
Code
Diff
  • def flip_the_number(n):
        return int(f'{n}'[::-1])
  • 11
    def flip_the_number(n):
    
    2
        return int(str(n)[::-1])
    
    2+
        return int(f'{n}'[::-1])
    
Code
Diff
  • def flip_the_number(n):
        # takes int, flips int, return int
        r = 0
        while n > 0:
            r *= 10
            r += n % 10
            n //= 10
        return r
  • 1
    def flip_the_number(number):
    
    2
        number = list(number)
    
    3
        print(number)
    
    4
        num = []
    
    5
        for i in range(len(number)):
    
    6
            lenth = len(number) 
    
    7
            num.append(str(number[lenth-1-i]))
    
    8
            print(num)       
    
    9
        join = ""
    
    10
        return join.join(num)
    
    11
            
    
    12
    13
            
    
    14
        
    
    15
        
    
    1+
    def flip_the_number(n):
    
    2+
        # takes int, flips int, return int
    
    3+
        r = 0
    
    4+
        while n > 0:
    
    5+
            r *= 10
    
    6+
            r += n % 10
    
    7+
            n //= 10
    
    8+
        return r