-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathhire_assistant.rb
More file actions
58 lines (56 loc) · 1.67 KB
/
Copy pathhire_assistant.rb
File metadata and controls
58 lines (56 loc) · 1.67 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
48
49
50
51
52
53
54
55
56
57
58
require_relative './permuting_arrays'
module RandomizedAlgorithms
class << self
# Public: Consolidates a list of all the candidates that got hired
#
# ARGS:
# candidates - Array with each candidate score
#
# Return: Array
#
# Examples
# hire_assistant([0, 9, 2, 15, 17, 9, 10, 20])
# => [1, 3, 4, 7]
def hire_assistant(candidates)
best = 0
hired_candidates = []
(0..(candidates.size-1)).each do |i|
if candidates[i] > candidates[best]
best = i
hired_candidates << i
end
end
hired_candidates
end
# Public: Consolidates a list of all the candidates that got hired
# RANDOMIZED
#
# ARGS:
# candidates - Array with each candidate score
# permutation_method - Method by which the input array has to be randomized
# Available methods "permute_by_sorting" DEFAULT
# "randomize_in_place"
# "permute_without_identity"
# "permute_with_all"
# Return: Array
#
# Examples
# randomized_hire_assistant([0, 9, 2, 15, 17, 9, 10, 20])
# => [2, 7]
# => [1]
# => []
# NOTE: Every output for each input will be different due to randomization
def randomized_hire_assistant(candidates, permutation_method="permute_by_sorting")
candidates = RandomizedAlgorithms.send(permutation_method, candidates)
best = 0
hired_candidates = []
(0..(candidates.size-1)).each do |i|
if candidates[i] > candidates[best]
best = i
hired_candidates << i
end
end
hired_candidates
end
end
end