adjacency_list_builder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c9ed6e680ef2fbb7f16b00aef327120f47d59d8a9196184e4a992aa32e956d9d
4
+ data.tar.gz: 160debc9b37559cbca6ea54d20671f92aa7ce2b43c4e95f1e4c265861281d0a4
5
+ SHA512:
6
+ metadata.gz: 5ce0830fdacc3d560055f7ec6e4b64e10cd8589e9af84ed309a3be858c1bcc377d59a307e9b843d83e0ad57d6e6f7ca1b48590f1e027bc1d08d67248a1d93224
7
+ data.tar.gz: 030c357e53d3a048ebe80ad3835ab3fc6d24b5279f579721e8924a8b246d5a410f8cdfe6741878214daee8b869996096d1109c47171032262de578ce50a1a55f
@@ -0,0 +1,39 @@
1
+ class AdjacencyListBuilder
2
+ class ExtendedInteger
3
+ def initialize(str)
4
+ @int = str.to_i
5
+ end
6
+
7
+ def options
8
+ @options ||= {}
9
+ end
10
+
11
+ def label=(label)
12
+ options[:label] = label
13
+ end
14
+
15
+ def label
16
+ options[:label]
17
+ end
18
+
19
+ def to_int
20
+ @int
21
+ end
22
+
23
+ def inspect
24
+ @int.inspect
25
+ end
26
+
27
+ def to_s
28
+ @int.to_s
29
+ end
30
+
31
+ def preview
32
+ "#{@int} => #{label}"
33
+ end
34
+
35
+ def method_missing(m, *args, &block)
36
+ @int.send(m, *args, &block)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,37 @@
1
+ require_relative 'adjacency_list_builder/extended_integer'
2
+
3
+ class AdjacencyListBuilder
4
+ def initialize(n, graph_string)
5
+ @n = n # node count
6
+ @graph_string = graph_string
7
+ @adjacency_list = Array.new(n) { [] }
8
+ end
9
+
10
+ def call(directed: false)
11
+ parsed_graph_string.each do |tuple|
12
+ # first element is node number, second which element it connects, third is a numeric label
13
+ index, connected_with, num_label = tuple.map(&:to_i)
14
+
15
+ if connected_with
16
+ if num_label
17
+ connected_with = ExtendedInteger.new(connected_with)
18
+ connected_with.label = num_label
19
+ end
20
+ adjacency_list[index].push connected_with
21
+ adjacency_list[connected_with].push index unless directed
22
+ end
23
+ end
24
+
25
+ adjacency_list
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :n, :graph_string, :adjacency_list
31
+
32
+ def parsed_graph_string
33
+ @parsed_graph_string ||= graph_string.split("\n").map { |tuple| tuple.split(' ') }
34
+ end
35
+ end
36
+
37
+
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adjacency_list_builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Maciej Kempin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-09-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'The string input can be generated here: https://csacademy.com/app/graph_editor/'
14
+ email: maciej.kempin@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/adjacency_list_builder.rb
20
+ - lib/adjacency_list_builder/extended_integer.rb
21
+ homepage:
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.1.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Build an adjacency list from given string
44
+ test_files: []