0

Numbers with identical amount of "1"s should be ordered by decimal representation.

For example:

srt([3,7,8,9]) => [8,3,9,7]  # 1000, 11, 1001, 111
deadshot
  • 8,881
  • 4
  • 20
  • 39
Timothy94
  • 69
  • 2
  • 11
  • Btw the question how to count set bits in a number is quite interesting by itself https://stackoverflow.com/questions/109023/how-to-count-the-number-of-set-bits-in-a-32-bit-integer – algrid Sep 19 '17 at 20:16

4 Answers4

4

You can pass an array to sort_by:

[1,2,3,4,5,6,7,8,9].sort_by { |i| [i.digits(2).count(1), i] }
#=> [1, 2, 4, 8, 3, 5, 6, 9, 7]

This will sort the items via Array#<=>, i.e. by the numbers of 1-bits and numbers with the same number of 1-bits by the number itself:

[
  1, 2, 4, 8,  # 1 1-bit  (0b0001, 0b0010, 0b0100, 0b1000)
  3, 5, 6, 9,  # 2 1-bits (0b0011, 0b0101, 0b0110, 0b1001)
  7            # 3 1-bits (0b0111)
]
Stefan
  • 109,145
  • 14
  • 143
  • 218
  • 1
    You could use `sum` in place of `count(1)`. It doesn't read as well but would be preferable for [Code Golf](https://codegolf.stackexchange.com). Good to see `digits` rather than `to_s` for this kind of problem. Good answer. – Cary Swoveland Sep 19 '17 at 16:21
3

In Ruby you can use a really simple approach:

def srt list
  list.sort_by { |number| number.to_s(2).count('1') }
end

It is not really performance efficient, but rather easy to read.

Peter Toth
  • 974
  • 9
  • 9
1

If your question is : Sort the integers in ascending order by the number of 1's in their binary representations. For example, (7)10 → (111)2 and (8)10 → (1000)2, so 8 (which has one 1 in binary) would be ordered before 7 (which has three 1's in binary.

Then, we can do that in python as below.

Step By Step Explanation

myList = [1,11,7]

# function to get binary 1's
 def get_len_of_ones(val):
    getbinary =lambda val : bin(val)[2:].count('1')
    return getbinary(val)

# mapping function to every element and then zipping it with myList    
myTuple = zip(myList, map(get_len_of_ones,myList))
print myTuple 
Out[1]: [(1, 1), (11, 3), (7, 3)]


# Applying sorting on the second element of the tuple in the List
sortedList = sorted(myTuple , key=lambda tup: tup[1],reverse=True)

print sortedList 
Out[1]: [(11, 3), (7, 3), (1, 1)]

# Unzip myList out and display myList at index 0 and convert to list
print list(zip(*sortedList)[0])
Out[1]: [11, 7, 1]

We can do it pythonic aswell

myList = [1,11,7]

# Using get_len_of_ones function from above code

l= lambda x : list(zip(*sorted(zip(x,map(get_len_of_ones,x)), key=lambda tup: tup[1],reverse=True))[0])

l(myList)
Out[1]: [11, 7, 1]
s_mj
  • 530
  • 11
  • 28
0

import pandas as pd

df = pd.DataFrame([1,2,3,4,5,8],columns=['Integers'])

def countSetBits(n): count = 0 while (n): count += n & 1 n >>= 1 return count

df["Count_of_set_bits"]=df.apply(lambda x: countSetBits(x['Integers']),axis=1)

df['value']=df['Integers']

df=df.sort_values(['Count_of_set_bits', 'value'], ascending=[True, True])

results=list(df['Integers'])

  • 1
    Hi and thanks for contributing an answer! Can you add an explanation on how your code solves the problem? To correctly format your code see this [help center article](https://stackoverflow.com/editing-help). – Jeanne Dark Nov 29 '20 at 08:09