Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fixed awkward indentation #2

Merged
merged 1 commit into from

2 participants

@rafalchmiel

You should have a look at the community Ruby coding style guide.

@bennacer860 bennacer860 merged commit 8a45cda into bennacer860:master
@rafalchmiel rafalchmiel deleted the unknown repository branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 19, 2013
  1. @rafalchmiel

    Fixed awkward indentation

    rafalchmiel authored
This page is out of date. Refresh to see the latest.
Showing with 69 additions and 71 deletions.
  1. +69 −71 benford.rb
View
140 benford.rb
@@ -3,85 +3,83 @@
require 'csv'
class Benford
- def initialize(fname,attribute)
- # @data = randomize_data_set(12320,true)
- @data = load_file(fname,attribute)
- # puts @data
- hash=compute_first_digit_frequency(@data)
- draw_percentage(hash)
- load_file("data.csv","Population")
- end
+ def initialize(fname,attribute)
+ # @data = randomize_data_set(12320,true)
+ @data = load_file(fname,attribute)
+ # puts @data
+ hash=compute_first_digit_frequency(@data)
+ draw_percentage(hash)
+ load_file("data.csv","Population")
+ end
- #return an integer array
- def randomize_data_set(max,sample=false)
- if sample
- (1...max).to_a.sample(max/2)
- else
- (1...max).to_a
- end
- end
+ #return an integer array
+ def randomize_data_set(max,sample=false)
+ if sample
+ (1...max).to_a.sample(max/2)
+ else
+ (1...max).to_a
+ end
+ end
- #load data from csv file
- def load_file(fname,attribute)
- csv_text = File.read(fname)
- data = Array.new()
- CSV.parse(csv_text, :headers => true) do |row|
- data << row[attribute]
- end
- return data
- end
+ #load data from csv file
+ def load_file(fname,attribute)
+ csv_text = File.read(fname)
+ data = Array.new()
+ CSV.parse(csv_text, :headers => true) do |row|
+ data << row[attribute]
+ end
+ return data
+ end
- #return a hash with the frequency of the first digit in the dataset
- def compute_first_digit_frequency(data)
- dataset_size = data.size
- frequency = Hash.new
- # puts data
- data.each do |el|
- el = el.to_s
- first_digit = el[0]
- # puts first_digit
- if frequency[first_digit]
- frequency[first_digit] += 1
- else
- frequency[first_digit] = 1
- end
- end
-
- frequency.each do |k,v|
- frequency[k]=(100.0*v/dataset_size).round(2)
- end
- #sort
- Hash[frequency.sort]
- end
+ #return a hash with the frequency of the first digit in the dataset
+ def compute_first_digit_frequency(data)
+ dataset_size = data.size
+ frequency = Hash.new
+ # puts data
+ data.each do |el|
+ el = el.to_s
+ first_digit = el[0]
+ # puts first_digit
+ if frequency[first_digit]
+ frequency[first_digit] += 1
+ else
+ frequency[first_digit] = 1
+ end
+ end
- def draw_percentage(hash)
- puts hash
- hash.each { |k,v|
- s = '-' * v.round
- benford_prediction = compute_benford_prediction(k.to_i)
- error_margin = compute_error_margin(v,benford_prediction)
- print "#{k}: #{s}|"
- print "result:#{v.round(2)}%"
- print "-error:#{error_margin.round(2)}%".red
- puts ""
- }
- end
+ frequency.each do |k,v|
+ frequency[k]=(100.0*v/dataset_size).round(2)
+ end
+ #sort
+ Hash[frequency.sort]
+ end
- #compute the benford prediction based on his formula
- def compute_benford_prediction(n)
- p=Math.log(1+(1.0/n),10) * 100
- return p.round(2)
- end
-
- #compute the error margin
- def compute_error_margin(result,prediction)
- return ((result.to_f-prediction.to_f)/prediction.to_f).abs*100
- end
-end
+ def draw_percentage(hash)
+ puts hash
+ hash.each { |k,v|
+ s = '-' * v.round
+ benford_prediction = compute_benford_prediction(k.to_i)
+ error_margin = compute_error_margin(v,benford_prediction)
+ print "#{k}: #{s}|"
+ print "result:#{v.round(2)}%"
+ print "-error:#{error_margin.round(2)}%".red
+ puts ""
+ }
+ end
+ #compute the benford prediction based on his formula
+ def compute_benford_prediction(n)
+ p=Math.log(1+(1.0/n),10) * 100
+ return p.round(2)
+ end
+ #compute the error margin
+ def compute_error_margin(result,prediction)
+ return ((result.to_f-prediction.to_f)/prediction.to_f).abs*100
+ end
+end
time = Benchmark.measure do
- b=Benford.new("data.csv","Population")
+ b=Benford.new("data.csv","Population")
end
puts time*1000
Something went wrong with that request. Please try again.