Skip to content

Instantly share code, notes, and snippets.

@anujbiyani
Created February 28, 2017 10:28
Show Gist options
  • Save anujbiyani/2c78ef013c284d90166042609a5ac02d to your computer and use it in GitHub Desktop.
Save anujbiyani/2c78ef013c284d90166042609a5ac02d to your computer and use it in GitHub Desktop.
Code for re-arranging and visualizing students and teachers and their preferences
require 'csv'
require 'json'
STUDENTS_CSV_PATH = 'students.csv'
TEACHERS_CSV_PATH = 'teachers.csv'
EMAIL_KEY = 'Email address'
LANGUAGE_KEY = 'What would you like to learn?'
EXPERIENCE_KEY = 'How experienced are you?'
TEACH_KEY = 'What do you want to teach?'
students_one_language = Hash.new { |h, v| h[v] = Hash.new { |_h, _v| _h[_v] = [] } }
# students = Hash.new { |h, v| h[v] = Hash.new { |_h, _v| _h[_v] = [] } }
students = Hash.new { |h, v| h[v] = [] }
CSV.foreach(STUDENTS_CSV_PATH, headers: true) do |row|
languages = row[LANGUAGE_KEY].split(',').map(&:strip)
students_one_language[languages.first][row[EXPERIENCE_KEY]] << row[EMAIL_KEY] if languages.count == 1
# languages.each { |language| students[language][row[EXPERIENCE_KEY]] << row[EMAIL_KEY] }
languages.each { |language| students[language] << row[EMAIL_KEY] }
end
teachers = Hash.new { |h, v| h[v] = [] }
teachers_one_language = Hash.new { |h, v| h[v] = [] }
CSV.foreach(TEACHERS_CSV_PATH, headers: true) do |row|
languages = row[TEACH_KEY].split(',').map(&:strip)
teachers_one_language[languages.first] << row[EMAIL_KEY] if languages.count == 1
languages.each { |language| teachers[language] << row[EMAIL_KEY] }
end
puts JSON.pretty_generate(students)
puts '---'
# puts JSON.pretty_generate(students_one_language)
# puts '---'
puts JSON.pretty_generate(teachers)
# puts JSON.pretty_generate(teachers_one_language)
# puts '---'
one, two, three = students.values
union = one & two & three
puts '---'
puts JSON.pretty_generate(union)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment