aboutsummaryrefslogtreecommitdiff
path: root/challenge-271/sgreen/python/ch-2.py
blob: 7ebb7f412a1a35e91efa16bdc80ee0be18f6135d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python3

import sys


def sort_by_1_bits(ints: list) -> list:
    """
    Sorts a list of integers based on the number of set bits (1s) in their
    binary representation. If they are equal, they are sorted by their value.

    Args:
        ints (list): A list of integers to be sorted.

    Returns:
        list: A new list of integers sorted based on the criteria above.
    """
    sorted_ints = sorted(ints, key=lambda x: (bin(x).count('1'), x))
    return sorted_ints


def main():
    # Convert input into integers
    array = [int(n) for n in sys.argv[1:]]
    result = sort_by_1_bits(array)
    print(tuple(result))


if __name__ == '__main__':
    main()