Sort integers by number of 1 bits in binary, then by value.
# Source: https://github.com/sickn33/agentic-awesome-skills | License: MIT | Copyright 2026 Antigravity User
# Provided via justclaw.it.com; keep this notice with the code.
# HumanEval/116
# Loki Mode Multi-Agent Solution
# Attempts: 1
# Passed: True
def sort_array(arr):
"""
In this Kata, you have to sort an array of non-negative integers according to
number of ones in their binary representation in ascending order.
For similar number of ones, sort based on decimal value.
It must be implemented like this:
>>> sort_array([1, 5, 2, 3, 4]) == [1, 2, 3, 4, 5]
>>> sort_array([-2, -3, -4, -5, -6]) == [-6, -5, -4, -3, -2]
>>> sort_array([1, 0, 2, 3, 4]) [0, 1, 2, 3, 4]
"""
return sorted(arr, key=lambda x: (bin(x).count('1'), x))
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key