7 kyu
Sort by binary ones
394 of 1,067guumaster
Description:
In this example you need to implement a function that sort a list of integers based on it's binary representation.
The rules are simple:
- sort the list based on the amount of 1's in the binary representation of each number.
- if two numbers have the same amount of 1's, the shorter string goes first. (ex: "11" goes before "101" when sorting 3 and 5 respectively)
- if the strings have the same length, lower decimal number goes first. (ex: 21 = "10101" and 25 = "11001", then 21 goes first as is lower)
Examples:
Input: [1,15,5,7,3]
- ( in binary strings is:
["1", "1111", "101", "111", "11"]
)
- ( in binary strings is:
Output: [15, 7, 3, 5, 1]
- (and after sortByBinaryOnes is:
["1111", "111", "11", "101", "1"]
)
- (and after sortByBinaryOnes is:
Arrays
Lists
Algorithms
Sorting
Binary
Bits
Similar Kata:
Stats:
Created | Oct 21, 2017 |
Published | Oct 21, 2017 |
Warriors Trained | 2824 |
Total Skips | 36 |
Total Code Submissions | 5789 |
Total Times Completed | 1067 |
JavaScript Completions | 394 |
Python Completions | 444 |
Java Completions | 194 |
Ruby Completions | 67 |
Crystal Completions | 13 |
COBOL Completions | 4 |
C Completions | 28 |
Total Stars | 45 |
% of votes with a positive feedback rating | 87% of 290 |
Total "Very Satisfied" Votes | 230 |
Total "Somewhat Satisfied" Votes | 43 |
Total "Not Satisfied" Votes | 17 |
Total Rank Assessments | 5 |
Average Assessed Rank | 7 kyu |
Highest Assessed Rank | 7 kyu |
Lowest Assessed Rank | 7 kyu |