Skip to content

Instantly share code, notes, and snippets.

@davidbalbert
Created May 2, 2017 14:12
Show Gist options
  • Select an option

  • Save davidbalbert/23d331dff4b46cdeb30480e9987ea0bc to your computer and use it in GitHub Desktop.

Select an option

Save davidbalbert/23d331dff4b46cdeb30480e9987ea0bc to your computer and use it in GitHub Desktop.
Count the numbers in your code
#!/usr/bin/env ruby
# Use with your code:
# $ grep -hIEro '\d+' . | ruby path/to/numbers.rb
tens = 0
fives = 0
twos = 0
evens = 0
odds = 0
count = 0
STDIN.each_line do |l|
n = Integer(l.chomp.sub(/^0+([^0])/, '\1'))
if n % 10 == 0
tens += 1
elsif n % 5 == 0
fives += 1
end
if Math.log2(n) % 1 == 0
twos += 1
end
if n % 2 == 0
evens += 1
else
odds += 1
end
count += 1
end
puts "fives: #{fives}/#{count} = #{fives.to_f/count}"
puts "tens: #{tens}/#{count} = #{tens.to_f/count}"
puts "twos: #{twos}/#{count} = #{twos.to_f/count}"
puts "evens: #{evens}/#{count} = #{evens.to_f/count}"
puts "odds: #{odds}/#{count} = #{odds.to_f/count}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment