Karthik Selvakumar Bhuvaneswaran is a Software Engineer working on Saas(RoR) and PaaS(Salesforce.com) applications. He beleives on day starts at night and requires nothing more than Music, a cup of cofee and a fully charged laptop.
© 2016 karthikselva. Distributed with an MIT license.
def subsets(str) n = 2 << (str.length - 1) result = [] n.times do |i| result << convert_in_to_set(i,str) end return result end def convert_in_to_set(x,str) tmp = x index = 0 set = '' while tmp > 0 if (tmp & 1) == 1 set << str[index..index] end index = index + 1 tmp = tmp >> 1 end return set end p subsets('abc') #["", "a", "b", "ab", "c", "ac", "bc", "abc"]