Skip to content

Instantly share code, notes, and snippets.

@noaacrappy
Created August 26, 2022 10:34
Show Gist options
  • Save noaacrappy/1e586455fce3008d285af99d89883e5d to your computer and use it in GitHub Desktop.
Save noaacrappy/1e586455fce3008d285af99d89883e5d to your computer and use it in GitHub Desktop.
Compute percentages of NOAA stations that are zombies per state
# ruby compute-percent-zombies-by-state.rb zombies.csv > percent-zombies-by-state.csv
Record = Struct.new(:num_zombies, :num_not_zombies)
def db_make(filename)
db = {}
data = File.read(filename)
lines = data.split("\n")
lines.each do |line|
fields = line.split(",")
state = fields[3]
is_zombie = fields[11] == "true"
unless db.has_key?(state)
db[state] = Record.new(0, 0)
end
if is_zombie
db[state].num_zombies += 1
else
db[state].num_not_zombies += 1
end
end
db
end
def db_dump(db)
db.each_pair do |state, r|
total = r.num_zombies + r.num_not_zombies
percent = r.num_zombies.to_f / total * 100
puts "#{state},#{total},#{"%.2f" % percent}"
end
end
if $0 == __FILE__
unless ARGV.size == 1
$stderr.puts "<zombies.csv>"
exit 1
end
db = db_make(ARGV.first)
db_dump(db)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment