Skip to content

Instantly share code, notes, and snippets.

@noriaki
Created March 3, 2013 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noriaki/5076806 to your computer and use it in GitHub Desktop.
Save noriaki/5076806 to your computer and use it in GitHub Desktop.
rubyでビット文字列から'1'の位置を求める ref: http://qiita.com/items/0726851e110e2e356791
"10100100110".reverse.split('1',-1).inject([]){|ret,i| ret << i.size + (ret.last.nil? ? 0 : ret.last + 1); ret }[0..-2]
# => [1, 2, 5, 8, 10]
"10100100110".split('1',-1).inject([]){|ret,i| ret << i.size + (ret.last.nil? ? 0 : ret.last + 1); ret }[0..-2]
# => [0, 2, 5, 8, 9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment