-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApartmentRanking.rb
More file actions
47 lines (45 loc) · 1.29 KB
/
Copy pathApartmentRanking.rb
File metadata and controls
47 lines (45 loc) · 1.29 KB
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
module ApartmentRanking
#apartments that reach certain criterian are weighted heavier than other apartments so they will be posted more often. criteria for weighting is in
def apartment_weighted_sample
# original algorithm found below
# https://gist.github.com/O-I/3e0654509dd8057b539a
distribute_points.max_by { |_, weight| rand ** (1.0 / weight) }.first
end
def give_points
points = apartments.map do |apartment|
[
true,
apartment.roommate_groups.count > 0,
apartment.roommate_groups.count > 0,
apartment.roommate_groups.count > 0,
apartment.roommate_groups.count > 0,
apartment.roommate_groups.count > 1,
apartment.roommate_groups.count > 2,
apartment.available <= Date.today
].select{|trues| trues}.count
end
apartments.zip(points)
.sort { |(k1,v1), (k2,v2)| v2 <=> v1 }.to_h
end
def distribute_points
keys = give_points.keys
values = give_points.values
sum = 0
percentages = values.map do |n|
if sum >= 10
0
elsif n >= 10
sum += 10
10
elsif sum + n >= 10
difference = (10.0 - sum) / 10
sum += 10 - sum
difference
else
sum += n
n / 10.0
end
end
keys.zip(percentages)
end
end