Skip to content

Instantly share code, notes, and snippets.

@pads
Last active August 29, 2015 14:13
Show Gist options
  • Save pads/0f19575ab848f77aeed4 to your computer and use it in GitHub Desktop.
Save pads/0f19575ab848f77aeed4 to your computer and use it in GitHub Desktop.
Find the location(s) of the max value in two result sets within a CSV of survey results
require 'csv'
csv_contents = CSV.read('Data.csv')
# Shift 3 rows of headers to get to the data
csv_contents.shift
csv_contents.shift
csv_contents.shift
set1 = []
set2 = []
csv_contents.each do |row|
set1 << row[0..12]
set2 << row[16..28]
end
def write_row(setNumber, row, index, csv_file)
max = row.map(&:to_i).max
max_indices = row.each_index.select{|index| row[index] == max.to_s }
# Output requires offset-by-1 based indexing
max_indices.map!{|index| index + 1 }
csv_file << ["#{setNumber}", "#{index + 1}", "#{max}", "#{max_indices.join(' ')}"]
end
CSV.open("Data.csv", "a+") do |csv_file|
csv_file << []
csv_file << ['Set', 'Row', 'Max', 'Index']
set1.each_with_index do |row, index|
write_row(1, row, index, csv_file)
end
set2.each_with_index do |row, index|
write_row(2, row, index, csv_file)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment