ptree 0.0.0
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 +7 -0
- data/lib/ptree.rb +149 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 70590371f382fec81d348127164903019c50b7b9cfca3027276fae74b8ee53af
|
4
|
+
data.tar.gz: 908c808ca4ac861366db6c60ae4d00c3066a5607817563ca0096efaf928a2795
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e32761a9d2c0b15a111394c6c68d7a796c238bcd81221de492b8fb57e122a1bef6da1194664efa19842bd7205dc05e750c9ceb54c699dde44420f574e740139a
|
7
|
+
data.tar.gz: 7fe8c65cb4f5a81667e013e5ce08525ea843ab8478d233ff6eea8de0737369b858cb77a7386f065f14f99123e088d51326c663a6b76f3d71df955eaa06e82347
|
data/lib/ptree.rb
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
$LOAD_PATH << '../lib'
|
5
|
+
|
6
|
+
class Ptree
|
7
|
+
attr_accessor :char, :is_end, :roots, :is_root
|
8
|
+
attr_reader :count
|
9
|
+
|
10
|
+
INVALID_WORD_MSG = "%s does not exist in tree".freeze
|
11
|
+
|
12
|
+
def initialize(root = false)
|
13
|
+
@char = nil
|
14
|
+
@is_end = false
|
15
|
+
@roots = []
|
16
|
+
@is_root = root
|
17
|
+
@count = 1
|
18
|
+
@tree = []
|
19
|
+
end
|
20
|
+
|
21
|
+
def add(word)
|
22
|
+
validate_word(word)
|
23
|
+
have = have_in_childs(word)
|
24
|
+
return nil if word.length.zero?
|
25
|
+
|
26
|
+
if have.empty?
|
27
|
+
no_child_found(word)
|
28
|
+
else
|
29
|
+
child_found(have, word)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def include?(word, is_word = false)
|
34
|
+
str = ''
|
35
|
+
obj = self
|
36
|
+
last = false
|
37
|
+
|
38
|
+
word.chars.each do |chr|
|
39
|
+
return false if obj.nil?
|
40
|
+
|
41
|
+
obj = obj.roots.select { |item| item.to_s == chr }.first
|
42
|
+
str += obj.to_s
|
43
|
+
end
|
44
|
+
|
45
|
+
is_word ? (str == word && obj.is_end == true) : str == word
|
46
|
+
end
|
47
|
+
|
48
|
+
def list(word = '')
|
49
|
+
tree = @tree
|
50
|
+
where = word.empty? ? self : have_in_childs(word)[0]
|
51
|
+
recursive_list(where, word)
|
52
|
+
@tree = []
|
53
|
+
|
54
|
+
tree.sort_by { |s| s.scan(/\d+/).first.to_i }
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
def delete(word)
|
59
|
+
validate_before_delete(word) ? do_delete(word) : invalid_delete(word)
|
60
|
+
end
|
61
|
+
|
62
|
+
def find(string)
|
63
|
+
include?(string, true)
|
64
|
+
end
|
65
|
+
|
66
|
+
def to_s
|
67
|
+
@char
|
68
|
+
end
|
69
|
+
|
70
|
+
def increase_count
|
71
|
+
@count += 1
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def validate_word(word)
|
77
|
+
!word.empty?
|
78
|
+
end
|
79
|
+
|
80
|
+
def invalid_delete(word)
|
81
|
+
puts sprintf(INVALID_WORD_MSG, word)
|
82
|
+
end
|
83
|
+
|
84
|
+
def do_delete(word)
|
85
|
+
last_obj = self
|
86
|
+
last = word[-1]
|
87
|
+
sum_of_childs = 0
|
88
|
+
|
89
|
+
word.chars.each do |chr|
|
90
|
+
break if last_obj.nil?
|
91
|
+
|
92
|
+
last_obj = last_obj.roots.select { |item| item.to_s == chr }.first
|
93
|
+
sum_of_childs += last_obj.count
|
94
|
+
end
|
95
|
+
|
96
|
+
if sum_of_childs == word.length
|
97
|
+
self.roots.delete_if{ |item| item.to_s == word[0] }
|
98
|
+
else
|
99
|
+
last_obj.is_end = false
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def validate_before_delete(word)
|
104
|
+
find(word)
|
105
|
+
end
|
106
|
+
|
107
|
+
def recursive_list(obj, prefix = '', str = '')
|
108
|
+
return false if obj.nil?
|
109
|
+
|
110
|
+
if prefix.length.positive? && include?(prefix)
|
111
|
+
obj = self
|
112
|
+
prefix.chars.each do |chr|
|
113
|
+
obj = obj.roots.select { |item| item.to_s == chr }.first
|
114
|
+
end
|
115
|
+
recursive_list(obj, '', prefix[0..-2])
|
116
|
+
|
117
|
+
return false
|
118
|
+
end
|
119
|
+
str += obj.to_s || ''
|
120
|
+
@tree << str if obj.is_end
|
121
|
+
|
122
|
+
obj.roots.each do |child|
|
123
|
+
recursive_list(child, prefix, str)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def downcase_char(char)
|
128
|
+
char.to_s.downcase
|
129
|
+
end
|
130
|
+
|
131
|
+
def no_child_found(word)
|
132
|
+
obj = Ptree.new
|
133
|
+
obj.is_end = true if word.length == 1
|
134
|
+
obj.char = downcase_char(word[0])
|
135
|
+
obj.add(word.to_s[1..-1])
|
136
|
+
@roots << obj
|
137
|
+
end
|
138
|
+
|
139
|
+
def child_found(have, word)
|
140
|
+
have.first.is_end = true if word.length == 1
|
141
|
+
have.first.char = downcase_char(word[0])
|
142
|
+
have.first.add(word.to_s[1..-1])
|
143
|
+
have.first.increase_count
|
144
|
+
end
|
145
|
+
|
146
|
+
def have_in_childs(word)
|
147
|
+
@roots.select { |item| item.to_s == downcase_char(word[0]) }
|
148
|
+
end
|
149
|
+
end
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ptree
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gia Nebieridze
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-05-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple Prefix Tree
|
14
|
+
email: gnebieridzs@unisens.ge
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/ptree.rb
|
20
|
+
homepage: https://rubygems.org/gems/ptree
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubygems_version: 3.2.3
|
40
|
+
signing_key:
|
41
|
+
specification_version: 4
|
42
|
+
summary: Prefix Tree!
|
43
|
+
test_files: []
|