Skip to content

Commit 58b026e

Browse files
author
Leon Rische
committed
code for chapter 8 in ruby
1 parent 738f9ca commit 58b026e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require "set"
2+
3+
# You pass an array in, and it gets converted to a Set.new.
4+
states_needed = Set.new(%w(mt wa or id nv ut ca az))
5+
6+
stations = {}
7+
stations["kone"] = Set.new(%w(id nv ut))
8+
stations["ktwo"] = Set.new(%w(wa id mt))
9+
stations["kthree"] = Set.new(%w(or nv ca))
10+
stations["kfour"] = Set.new(%w(nv ut))
11+
stations["kfive"] = Set.new(%w(ca az))
12+
13+
final_stations = Set.new
14+
15+
until states_needed.empty?
16+
best_station = nil
17+
states_covered = Set.new
18+
19+
stations.each do |station, states|
20+
covered = states_needed & states
21+
if covered.length > states_covered.length
22+
best_station = station
23+
states_covered = covered
24+
end
25+
end
26+
27+
states_needed -= states_covered
28+
final_stations << best_station
29+
end
30+
31+
p final_stations

0 commit comments

Comments
 (0)