-
Notifications
You must be signed in to change notification settings - Fork 1
/
pair_matrix.rb
66 lines (59 loc) · 1.43 KB
/
pair_matrix.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def find_pairs(user_id)
result = [[]]
u = User.find(user_id)
u.pairings.each do |x|
Pairing.where(pair_id: x.pair_id).find_each do |p|
result.last << p
if result.last.length == 2
result.push([])
end
end
end
result
end
def find_all_pairs(cohort_name)
result = [[]]
c = Cohort.find_by(name: cohort_name)
users = User.where(cohort_id: c.id)
users.each do |u|
u.pairings.each do |x|
Pairing.where(pair_id: x.pair_id).find_each do |p|
result.last << p
if result.last.length == 2
result.push([])
end
end
end
end
result.uniq
end
# to create empty matrix
def empty_matrix(cohort_name)
c = Cohort.find_by(name: cohort_name)
matrix = Array.new(c.users.length)
matrix.map! { |x| x = Array.new(c.users.length) }
matrix.each { |array| array.map! { |x| x = 0 } }
matrix
end
def populate_entire_matrix(cohort_name)
matrix = empty_matrix(cohort_name)
all_pairs = find_all_pairs(cohort_name)
all_pairs.each do |pair|
if !pair.empty?
matrix[pair[0].user_id - 1][pair[1].user_id - 1] += 1
end
end
matrix
end
def populate_matrix(user_id, cohort_name)
@matrix = empty_matrix(cohort_name)
all_pairs = find_pairs(user_id)
all_pairs.each do |pair|
if !pair.empty?
matrix[pair[0].user_id - 1][pair[1].user_id - 1] += 1
pair.reverse!
matrix[pair[0].user_id - 1][pair[1].user_id - 1] += 1
end
end
@matrix
end