victory 0.0.4 → 0.0.5
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 +4 -4
- data/USAGE.md +64 -0
- data/lib/io_helpers/reader.rb +38 -0
- data/lib/io_helpers/writer.rb +18 -0
- data/lib/victory/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe96b696eb08e49451fb624e64d2c1f04de540e2cfdc5b655bcf431841d0b603
|
4
|
+
data.tar.gz: 41fd068c0a3175b6546e3d1fd20308e4563f75137f5dda4884d59d5c5fd27a34
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4152e88b2c6201a435b6c2124d8ce7a5202e4fbe5766c156edacd17a2bd1f29a7ad188177c7318b6e69ae436ab3ed86c0d4415f22cc660e911e405b791185835
|
7
|
+
data.tar.gz: 31ee4fc4e2acd3e347c0b7c47c65fccbe90ccf3da35159569883ef904f9197b71dd0b2d9ce4c32875b71bd3aa19f9ece1786955438329c4d2987a591911abe40
|
data/USAGE.md
CHANGED
@@ -11,6 +11,7 @@ In this markdown you will see the usage of all the datastructures and algorithms
|
|
11
11
|
* [OpenStruct](#openstruct)
|
12
12
|
* [Tuple](#tuple)
|
13
13
|
* [Graph](#graph)
|
14
|
+
* [IOHelpers](#io-helpers)
|
14
15
|
|
15
16
|
<a name="array" />
|
16
17
|
|
@@ -174,7 +175,70 @@ dg.to_s
|
|
174
175
|
# => "(1-2)(1-6)(2-3)(2-4)(4-5)(6-4)"
|
175
176
|
```
|
176
177
|
|
178
|
+
### Dijkstra's algorithm
|
179
|
+
```ruby
|
180
|
+
g = Containers::Graph[1,2, 1,3, 2,4, 4,5, 3,5]
|
181
|
+
weights = {[1,2] => 2, [2,4] => 1, [4,5] => 1, [1,3] => 1, [3,5] => 5}
|
182
|
+
g.dijkstra_shortest_paths(weights, 1)
|
183
|
+
# => {1=>[1], 2=>[1, 2], 3=>[1, 3], 4=>[1, 2, 4], 5=>[1, 2, 4, 5]}
|
184
|
+
g.dijkstra_shortest_path(weights, 1, 5)
|
185
|
+
# => [1, 2, 4, 5]
|
186
|
+
```
|
187
|
+
|
188
|
+
### Bellman Ford's algorithm
|
189
|
+
```ruby
|
190
|
+
g = Containers::Graph[1,2, 1,3, 2,4, 4,5, 3,5]
|
191
|
+
weights = {[1,2] => 2, [2,4] => 1, [4,5] => 1, [1,3] => 1, [3,5] => 5}
|
192
|
+
g.bellman_ford_shortest_paths(weights, 1)
|
193
|
+
# => {1=>[1], 2=>[1, 2], 3=>[1, 3], 4=>[1, 2, 4], 5=>[1, 2, 4, 5]}
|
194
|
+
```
|
195
|
+
|
196
|
+
### Topological sort
|
197
|
+
```ruby
|
198
|
+
g = Containers::Graph[1,2, 1,3, 2,4, 4,5, 3,5]
|
199
|
+
g.topsort_iterator.to_a
|
200
|
+
# => [1, 3, 2, 4, 5]
|
201
|
+
```
|
202
|
+
|
203
|
+
<a name="io-helpers" />
|
204
|
+
|
205
|
+
## IO Helpers
|
206
|
+
|
207
|
+
### Reader
|
208
|
+
Can read from `file` or `string`.
|
209
|
+
```ruby
|
210
|
+
Reader.file("a")
|
211
|
+
# => ["1,2,3", "4,5,6", "7,8,9"]
|
212
|
+
Reader.file("a", col_sep: ",")
|
213
|
+
# => [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]]
|
214
|
+
Reader.file("a", col_sep: ",", as: "i")
|
215
|
+
# => [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
216
|
+
Reader.file("a", col_sep: ",", mapper: ->(x) {x.to_i * x.to_i})
|
217
|
+
# => [[1, 4, 9], [16, 25, 36], [49, 64, 81]]
|
218
|
+
Reader.string("0.1#0.2#0.3", line_sep: "#", as: 'r')
|
219
|
+
# => [(1/10), (1/5), (3/10)]
|
220
|
+
```
|
221
|
+
Supported types for `as` are:
|
222
|
+
* `s` for string (default)
|
223
|
+
* `i` for integer
|
224
|
+
* `f` for float
|
225
|
+
* `r` for rational
|
226
|
+
* `c` for complex
|
227
|
+
* `sym` for symbol
|
228
|
+
|
229
|
+
### Writer
|
230
|
+
Can write to `file` or `string`.
|
231
|
+
```ruby
|
232
|
+
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
233
|
+
Writer.string(a, line_sep: '@', col_sep: '.')
|
234
|
+
# => "1.2.3@4.5.6@7.8.9"
|
235
|
+
Writer.string(a, line_sep: "\n", col_sep: ' ', mapper: ->(x) {"0,#{x}"})
|
236
|
+
# => "0,1 0,2 0,3\n0,4 0,5 0,6\n0,7 0,8 0,9"
|
237
|
+
Writer.file("b", a, line_sep: "\n", col_sep: ' ', mapper: ->x {"0,#{x}"})
|
238
|
+
```
|
239
|
+
|
177
240
|
# Other useful links
|
178
241
|
|
179
242
|
* https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/
|
180
243
|
* https://github.com/sagivo/algorithms
|
244
|
+
* https://github.com/SciRuby/rb-gsl
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module IOHelpers
|
2
|
+
class Reader
|
3
|
+
def self.file(path, options = {})
|
4
|
+
line_sep = options[:line_sep] || "\n"
|
5
|
+
lines = File.readlines(path, line_sep, chomp: true)
|
6
|
+
map_lines(lines, options)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.string(str, options = {})
|
10
|
+
line_sep = options[:line_sep] || "\n"
|
11
|
+
lines = str.split(line_sep)
|
12
|
+
map_lines(lines, options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.stream(path)
|
16
|
+
IO.foreach(path).lazy
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.map_lines(lines, options)
|
20
|
+
col_sep = options[:col_sep]
|
21
|
+
as = options[:as] || 's'
|
22
|
+
mapper = options[:mapper]
|
23
|
+
if col_sep
|
24
|
+
lines.map { |line| line.split(col_sep).map { |col| map_element(col, as, mapper) } }
|
25
|
+
else
|
26
|
+
lines.map { |line| map_element(line, as, mapper) }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.map_element(element, as, mapper)
|
31
|
+
if mapper
|
32
|
+
mapper.call(element)
|
33
|
+
else
|
34
|
+
element.method("to_#{as}").call
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module IOHelpers
|
2
|
+
class Writer
|
3
|
+
def self.file(path, data, options = {})
|
4
|
+
File.write(path, string(data, options))
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.string(data, options = {})
|
8
|
+
line_sep = options[:line_sep] || "\n"
|
9
|
+
col_sep = options[:col_sep]
|
10
|
+
mapper = options[:mapper] || ->(x) { x.to_s }
|
11
|
+
if col_sep
|
12
|
+
data.map { |line| line.map(&mapper).join(col_sep) }.join(line_sep)
|
13
|
+
else
|
14
|
+
data.map(&mapper).join(line_sep)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/victory/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: victory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arnold Szederjesi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-03-
|
11
|
+
date: 2019-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -166,6 +166,8 @@ files:
|
|
166
166
|
- lib/containers/trie.rb
|
167
167
|
- lib/containers/tuple.rb
|
168
168
|
- lib/include_rgl.rb
|
169
|
+
- lib/io_helpers/reader.rb
|
170
|
+
- lib/io_helpers/writer.rb
|
169
171
|
- lib/victory.rb
|
170
172
|
- lib/victory/version.rb
|
171
173
|
- victory.gemspec
|