fellowship 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0bdcc3705be4ae42116a54fa3588bbad22b61384d9f98d70c9c669b1470000c3
4
- data.tar.gz: 32a3526bfd2fe3da97f7407e0c8bb166134c05d0f795ce8bc68c8f367d7eebb9
3
+ metadata.gz: 67b5d04ef75e5ed109f261913cda1091abcc6fca05d5cd7770777a2e7202630e
4
+ data.tar.gz: 9d3e8e2472c2ddd580b1f5f679fb84676be046b185676c5b8b591f94cff1c7a1
5
5
  SHA512:
6
- metadata.gz: 1cd0ef69299281fb34a180cd74d9726ab68e29e78ed30417c43b191f6b12aad87546a207de6f5f886ca17ff1d4082571633d0c908d9fac36b34bf54c65dfaacd
7
- data.tar.gz: d9ae646db2b6119305b8b8612b6359a9e90be1ce0cc305841d92aa342603358c41285ff4cc604eb8859293529a02d3ba6e8deb8c5c0fd5dc9427c3a07fa9a31b
6
+ metadata.gz: 88fe84b3752fce3ab743fa4c6f9389ef7cc3628596175296a766c54d910f97e2ec1e10b6d8788d1d7e6c36dffb0d4d1c9f8858c2b83078f0971faf3f3899ab0c
7
+ data.tar.gz: 9dcb931bc85042139b59e883c12e793c45b91e77fc8eff1ae78bb8036020057b43d83a6e320a169889d1b02e32c332fbbd9fd667ac7a109355758d60cbfe9c3a
data/README.md CHANGED
@@ -1,9 +1,5 @@
1
1
  # Fellowship
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fellowship`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
3
  ## Installation
8
4
 
9
5
  Add this line to your application's Gemfile:
@@ -22,17 +18,138 @@ Or install it yourself as:
22
18
 
23
19
  ## Usage
24
20
 
25
- TODO: Write usage instructions here
21
+ ### Basic
22
+ ```ruby
23
+ Fellowship::Fellowship.deep_copy(x) # => x
24
+ Fellowship::Fellowship.factorial(5) # => 120
25
+ Fellowship::Fellowship.prime?(83) # => true
26
+ Fellowship::Fellowship.int_digit(234678, 1) # => 3
27
+ ```
28
+
29
+ ### Hash Sorting
30
+ ```ruby
31
+ Fellowship::Fellowship.sort_hashes_by(array_of_hashes, *keys)
32
+ ```
33
+
34
+ ### Route Finding
26
35
 
27
- ## Development
36
+ ```ruby
37
+ Fellowship::Fellowship.find_a_route(start_id, end_id, segments, endpoint1, endpoint2)
38
+ Fellowship::Fellowship.find_shortest_route(start_id, end_id, segments, endpoint1, endpoint2, distance)
39
+ ```
28
40
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
41
+ Finding a route with the fewest number of segments or steps:
30
42
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+ ```ruby
44
+ segments = [
45
+ {id: 407, nw_end: 1008, se_end: 1034, distance: 0.0451090841605882},
46
+ {id: 408, nw_end: 1010, se_end: 1051, distance: 0.28526714884356},
47
+ {id: 409, nw_end: 1007, se_end: 1034, distance: 0.0337117138131401},
48
+ {id: 410, nw_end: 1034, se_end: 1009, distance: 0.0651634067494387},
49
+ {id: 411, nw_end: 1005, se_end: 1006, distance: 0.00927910855352394},
50
+ {id: 412, nw_end: 1051, se_end: 1035, distance: 0.133084855652394},
51
+ {id: 413, nw_end: 1002, se_end: 1035, distance: 1.27839209339889},
52
+ {id: 414, nw_end: 1006, se_end: 1007, distance: 0.0125502145551738},
53
+ {id: 415, nw_end: 1009, se_end: 1010, distance: 0.012853363285835},
54
+ {id: 416, nw_end: 1035, se_end: 1031, distance: 0.735882208263929},
55
+ {id: 418, nw_end: 1031, se_end: 1030, distance: 1.12586171627523},
56
+ {id: 419, nw_end: 1050, se_end: 1015, distance: 0.0412667354025934},
57
+ {id: 420, nw_end: 1015, se_end: 1021, distance: 0.0714369928872643},
58
+ {id: 421, nw_end: 1021, se_end: 1023, distance: 0.176347122662142},
59
+ {id: 422, nw_end: 1023, se_end: 1032, distance: 0.175818715988043},
60
+ {id: 423, nw_end: 1023, se_end: 1026, distance: 0.087528672029317},
61
+ {id: 424, nw_end: 1030, se_end: 1028, distance: 0.634790077513064},
62
+ {id: 425, nw_end: 1028, se_end: 1050, distance: 0.311440698359342},
63
+ {id: 426, nw_end: 1028, se_end: 1033, distance: 0.101534760031209},
64
+ {id: 430, nw_end: 1003, se_end: 1002, distance: 0.116309923531378},
65
+ {id: 431, nw_end: 1025, se_end: 1003, distance: 0.533810729538892},
66
+ {id: 432, nw_end: 1029, se_end: 1032, distance: 0.023222712},
67
+ {id: 433, nw_end: 1004, se_end: 1015, distance: 1.04990888024116},
68
+ {id: 436, nw_end: 1004, se_end: 1012, distance: 0.108583019227206},
69
+ {id: 437, nw_end: 1012, se_end: 1052, distance: 0.047289907},
70
+ {id: 457, nw_end: 1070, se_end: 1095, distance: 11.6214987078938},
71
+ {id: 458, nw_end: 1114, se_end: 1081, distance: 8.20314176659542},
72
+ {id: 459, nw_end: 1081, se_end: 1082, distance: 12.8813919590166},
73
+ {id: 460, nw_end: 1082, se_end: 1071, distance: 13.7702138943404},
74
+ {id: 461, nw_end: 1071, se_end: 1083, distance: 10.3263252875694},
75
+ {id: 462, nw_end: 1083, se_end: 1085, distance: 20.9073747305393},
76
+ {id: 463, nw_end: 1085, se_end: 1047, distance: 9.28349400463438},
77
+ {id: 464, nw_end: 1086, se_end: 1087, distance: 70.0664133754052},
78
+ {id: 465, nw_end: 1046, se_end: 1071, distance: 14.8395759342706},
79
+ {id: 466, nw_end: 1087, se_end: 1088, distance: 13.4108176074637},
80
+ {id: 467, nw_end: 1088, se_end: 1089, distance: 8.50714924428603},
81
+ {id: 468, nw_end: 1089, se_end: 1075, distance: 11.9067149349027},
82
+ {id: 469, nw_end: 1064, se_end: 1081, distance: 4.20243376792587},
83
+ {id: 470, nw_end: 1047, se_end: 1086, distance: 7.77735216342798},
84
+ {id: 471, nw_end: 1095, se_end: 1096, distance: 8.99184860070038},
85
+ {id: 472, nw_end: 1096, se_end: 1097, distance: 11.6214987078938},
86
+ {id: 473, nw_end: 1068, se_end: 1070, distance: 25.3573178747273},
87
+ {id: 474, nw_end: 1097, se_end: 1098, distance: 10.8183848988841},
88
+ {id: 475, nw_end: 1098, se_end: 1099, distance: 10.5952644987299},
89
+ {id: 476, nw_end: 1099, se_end: 1100, distance: 18.6504162723198},
90
+ {id: 477, nw_end: 1100, se_end: 1101, distance: 10.5344562782683},
91
+ {id: 478, nw_end: 1102, se_end: 1069, distance: 15.9602569235506},
92
+ {id: 479, nw_end: 1017, se_end: 1011, distance: 16.9370044102313},
93
+ {id: 480, nw_end: 1079, se_end: 1093, distance: 41.4264760326217},
94
+ {id: 481, nw_end: 1080, se_end: 1090, distance: 7.1291229632752},
95
+ {id: 482, nw_end: 1080, se_end: 1079, distance: 7.05553097923426},
96
+ {id: 483, nw_end: 1068, se_end: 1055, distance: 14.8564042223522},
97
+ {id: 484, nw_end: 1055, se_end: 1056, distance: 10.1549592290523},
98
+ {id: 485, nw_end: 1056, se_end: 1057, distance: 5.12111325727507},
99
+ {id: 427, nw_end: 1066, se_end: 1039, distance: 19.3930411292224},
100
+ {id: 428, nw_end: 1039, se_end: 1043, distance: 26.1784711360986},
101
+ {id: 429, nw_end: 1074, se_end: 1093, distance: 8.58285100334328},
102
+ {id: 434, nw_end: 1073, se_end: 1076, distance: 5.93055368559559},
103
+ {id: 435, nw_end: 1073, se_end: 1068, distance: 26.7415940754477},
104
+ {id: 438, nw_end: 1093, se_end: 1022, distance: 3.8477698},
105
+ {id: 439, nw_end: 1092, se_end: 1093, distance: 7.6955396},
106
+ {id: 440, nw_end: 1069, se_end: 1077, distance: 22.4299224757837},
107
+ {id: 441, nw_end: 1043, se_end: 1078, distance: 39.9454087710314},
108
+ {id: 443, nw_end: 1093, se_end: 1077, distance: 61.6791863036295},
109
+ {id: 444, nw_end: 1018, se_end: 1048, distance: 29.0811827310994},
110
+ {id: 445, nw_end: 1048, se_end: 1066, distance: 7.46633331398889},
111
+ {id: 446, nw_end: 1074, se_end: 1069, distance: 49.9020895344982},
112
+ {id: 447, nw_end: 1049, se_end: 1072, distance: 30.0601321090231},
113
+ {id: 448, nw_end: 1067, se_end: 1049, distance: 17.1188565471891},
114
+ {id: 449, nw_end: 1072, se_end: 1046, distance: 45.5878513494496},
115
+ {id: 450, nw_end: 1053, se_end: 1094, distance: 4.20243376792587},
116
+ {id: 451, nw_end: 1094, se_end: 1092, distance: 1.75574271304472},
117
+ {id: 452, nw_end: 1065, se_end: 1114, distance: 37.3019531251371},
118
+ {id: 453, nw_end: 1090, se_end: 1115, distance: 16.6715332931735},
119
+ {id: 454, nw_end: 1115, se_end: 1011, distance: 32.578470872611},
120
+ {id: 456, nw_end: 1113, se_end: 1017, distance: 22.3989999419667},
121
+ {id: 486, nw_end: 1057, se_end: 1058, distance: 6.38410276942025},
122
+ {id: 487, nw_end: 1058, se_end: 1059, distance: 3.98740847200949},
123
+ {id: 488, nw_end: 1059, se_end: 1060, distance: 24.6836288585802},
124
+ {id: 489, nw_end: 1060, se_end: 1061, distance: 22.1255421595633},
125
+ {id: 490, nw_end: 1061, se_end: 1062, distance: 6.97732444211168},
126
+ {id: 491, nw_end: 1062, se_end: 1074, distance: 12.5162551635436},
127
+ {id: 492, nw_end: 1101, se_end: 1102, distance: 21.6136610101975},
128
+ {id: 493, nw_end: 1033, se_end: 1103, distance: 23.2370185711613},
129
+ {id: 494, nw_end: 1103, se_end: 1104, distance: 6.65775221794366},
130
+ {id: 495, nw_end: 1104, se_end: 1114, distance: 8.53679307443082},
131
+ {id: 496, nw_end: 1114, se_end: 1105, distance: 2.64881612468246},
132
+ {id: 497, nw_end: 1105, se_end: 1106, distance: 0.76955396},
133
+ {id: 498, nw_end: 1106, se_end: 1107, distance: 3.46593160021922},
134
+ {id: 499, nw_end: 1107, se_end: 1111, distance: 11.5723069612762},
135
+ {id: 500, nw_end: 1111, se_end: 1112, distance: 17.69974108},
136
+ {id: 501, nw_end: 1112, se_end: 1108, distance: 18.9463112119279},
137
+ {id: 502, nw_end: 1108, se_end: 1109, distance: 6.97732444211168},
138
+ {id: 503, nw_end: 1109, se_end: 1110, distance: 6.15643168},
139
+ {id: 504, nw_end: 1110, se_end: 1018, distance: 8.83639194154377},
140
+ {id: 505, nw_end: 1038, se_end: 1112, distance: 3.8477698},
141
+ {id: 506, nw_end: 1045, se_end: 1111, distance: 3.19205138471012},
142
+ {id: 417, nw_end: 1075, se_end: 1073, distance: 16.886886298079},
143
+ {id: 442, nw_end: 1074, se_end: 1053, distance: 6.65775221794366},
144
+ {id: 455, nw_end: 1022, se_end: 1113, distance: 9.27321379585551}
145
+ ]
146
+
147
+
148
+ ```
32
149
 
33
150
  ## Contributing
34
151
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fellowship. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
152
+ Bug reports and pull requests are welcome on GitHub at https://github.com/joshsarna/fellowship. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
153
 
37
154
  ## License
38
155
 
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -57,10 +57,13 @@ module Fellowship
57
57
  return array_of_hashes
58
58
  end
59
59
 
60
- def self.find_route(start_id,end_id,segments,endpoint1, endpoint2)
60
+ def self.find_shortest_route(start_id, end_id, segments, endpoint1, endpoint2, distance)
61
61
  possible_routes = []
62
62
  directions = []
63
+ successful_routes = []
64
+ # p segments
63
65
  starts = segments.select{ | segment | segment[endpoint1] == start_id || segment[endpoint2] == start_id }
66
+ # p starts
64
67
  starts.each do |start|
65
68
  if start[endpoint1] == start_id
66
69
  first_tail = start[endpoint2]
@@ -68,9 +71,9 @@ module Fellowship
68
71
  first_tail = start[endpoint1]
69
72
  end
70
73
  if first_tail == end_id
71
- return {route: [start.id], tail: first_tail}
74
+ return {route: [start[:id]], tail: first_tail}
72
75
  end
73
- possible_routes << {route: [start.id], tail: first_tail}
76
+ possible_routes << {route: [start[:id]], tail: first_tail}
74
77
  end
75
78
  route_achieved = false
76
79
  while possible_routes != []
@@ -86,8 +89,78 @@ module Fellowship
86
89
  if next_tail == end_id
87
90
  route_achieved = true
88
91
  end
89
- if !curr_route[:route].index(next_step.id) || curr_route[:route].index(next_step.id) < 0
90
- updated_route = {route: curr_route[:route] << next_step.id, tail: next_tail}
92
+ if !curr_route[:route].index(next_step[:id]) || curr_route[:route].index(next_step[:id]) < 0
93
+ updated_route = {route: curr_route[:route] << next_step[:id], tail: next_tail}
94
+ if route_achieved == true
95
+ successful_routes << updated_route
96
+ route_achieved = false
97
+ else
98
+ new_possible_routes << updated_route
99
+ curr_route[:route] = curr_route[:route][0..-2]
100
+ end
101
+ end
102
+ end
103
+ end
104
+ possible_routes = new_possible_routes
105
+ end
106
+
107
+ if successful_routes.length == 0
108
+ return "No route found."
109
+ end
110
+
111
+ # find distances
112
+ successful_routes.each_with_index do |route, index|
113
+ d = 0
114
+ sr_segments = route[:route]
115
+ sr_segments.each do |sr_segment|
116
+ d += segments.select{ | segment | segment[:id] == sr_segment }[0][distance].to_f
117
+ end
118
+ successful_routes[index][distance] = d
119
+ end
120
+
121
+ # find shortest distance
122
+ shortest_distance = successful_routes[0][distance]
123
+ shortest = successful_routes[0]
124
+ successful_routes.each do |sr|
125
+ if sr[distance] < shortest_distance
126
+ shortest_distance = sr[distance]
127
+ shortest = sr
128
+ end
129
+ end
130
+ return shortest
131
+ end
132
+
133
+ def self.find_a_route(start_id, end_id, segments, endpoint1, endpoint2)
134
+ possible_routes = []
135
+ directions = []
136
+ starts = segments.select{ | segment | segment[endpoint1] == start_id || segment[endpoint2] == start_id }
137
+ starts.each do |start|
138
+ if start[endpoint1] == start_id
139
+ first_tail = start[endpoint2]
140
+ else
141
+ first_tail = start[endpoint1]
142
+ end
143
+ if first_tail == end_id
144
+ return {route: [start[:id]], tail: first_tail}
145
+ end
146
+ possible_routes << {route: [start[:id]], tail: first_tail}
147
+ end
148
+ route_achieved = false
149
+ while possible_routes != []
150
+ new_possible_routes = []
151
+ possible_routes.each do |curr_route|
152
+ nexts = segments.select{ | segment | segment[endpoint1] == curr_route[:tail] || segment[endpoint2] == curr_route[:tail] }
153
+ nexts.each do |next_step|
154
+ if next_step[endpoint1] === curr_route[:tail]
155
+ next_tail = next_step[endpoint2]
156
+ else
157
+ next_tail = next_step[endpoint1]
158
+ end
159
+ if next_tail == end_id
160
+ route_achieved = true
161
+ end
162
+ if !curr_route[:route].index(next_step[:id]) || curr_route[:route].index(next_step[:id]) < 0
163
+ updated_route = {route: curr_route[:route] << next_step[:id], tail: next_tail}
91
164
  if route_achieved == true
92
165
  return updated_route
93
166
  else
@@ -1,3 +1,3 @@
1
1
  module Fellowship
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fellowship
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - joshsarna
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-05 00:00:00.000000000 Z
11
+ date: 2019-03-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A collection of methods to help solving technical exercises in ruby
14
14
  email:
@@ -28,6 +28,11 @@ files:
28
28
  - Rakefile
29
29
  - bin/console
30
30
  - bin/setup
31
+ - fellowship-0.0.1.gem
32
+ - fellowship-0.0.2.gem
33
+ - fellowship-0.0.3.gem
34
+ - fellowship-0.0.4.gem
35
+ - fellowship-0.0.5.gem
31
36
  - fellowship.gemspec
32
37
  - lib/fellowship.rb
33
38
  - lib/fellowship/version.rb