ds_101 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 37b04a30b9345f90077db11dfe947aa34abeb9d5fe5ebceb853791d766aa103c
4
+ data.tar.gz: af1dd45e8659a4f3e95417d5e341d1c8d03c60d65e75409b7737b13d54548417
5
+ SHA512:
6
+ metadata.gz: 6bbbc3b8c7c6383cc668189d63b65e77463bce3d944a4872f1347120801119664dcd9c6f7937d31aae954afc908f79c624dcb4af047226359591087e25eedf59
7
+ data.tar.gz: 85a2d57313a6be4a8058515d21f2d4664b69a9c418491512c04a4b6602b85097961086d2eb2f43b0cb50bc10b197e0e55e5941c6cf803146111c0b72d1a3a905
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-07-29
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 ShroukAbozeid
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Ds101
2
+
3
+ A Simple Implementation of Data Structure in Ruby.
4
+
5
+ Currently Available Data Structures:
6
+
7
+ - Linked List
8
+ - Double Linked List
9
+
10
+ ## Installation
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ after installing the gem, you can use available class and their methods.
22
+ Classes:
23
+ - `Ds101::LinkedList::SingleLinkedList`
24
+ - `Ds101::LinkedList::DoubleLinkedList`
25
+
26
+ Both classes has the following methods:
27
+ - `append`
28
+ - `remove`
29
+ - `remove_head`
30
+ - `clear`
31
+ - `to_a`
32
+ - `find`
33
+ - `find_by`
34
+ - `each`
35
+ - `concat`
36
+ - `init_from_arr`
37
+
38
+
39
+ ## Development
40
+
41
+ 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.
42
+
43
+ To install this gem onto your local machine, run `bundle exec rake install`.
44
+
45
+ To release a new version, update the version number in `version.rb`,
46
+ and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ShroukAbozeid/ds_101.
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ds101
4
+ module LinkedList
5
+ module Common
6
+ def find(value)
7
+ current = head
8
+ current = current.next while current && current.value != value
9
+ current
10
+ end
11
+
12
+ def find_by(&block)
13
+ current = head
14
+ while current
15
+ return current if block.call(current)
16
+
17
+ current = current.next
18
+ end
19
+ nil
20
+ end
21
+
22
+ def each
23
+ current = head
24
+ while current
25
+ yield current
26
+ current = current.next
27
+ end
28
+ end
29
+
30
+ def concat(other_list)
31
+ other_list.each do |node|
32
+ append(node.value)
33
+ end
34
+ end
35
+
36
+ def to_a
37
+ arr = []
38
+ each do |node|
39
+ arr << node.value
40
+ end
41
+ arr
42
+ end
43
+
44
+ def clear
45
+ remove_head while length.positive?
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "common"
4
+ module Ds101
5
+ module LinkedList
6
+ class DoubleLinkedList
7
+ include Ds101::LinkedList::Common
8
+ attr_reader :head, :tail, :length
9
+
10
+ class Node
11
+ attr_accessor :value, :next, :prev
12
+
13
+ def initialize(value:, next_node: nil, prev_node: nil)
14
+ @value = value
15
+ @next = next_node
16
+ @prev = prev_node
17
+ end
18
+ end
19
+
20
+ def initialize
21
+ @head = nil
22
+ @tail = nil
23
+ @length = 0
24
+ end
25
+
26
+ def self.init_from_arr(arr)
27
+ list = DoubleLinkedList.new
28
+ arr.each do |value|
29
+ list.append(value)
30
+ end
31
+ list
32
+ end
33
+
34
+ def append(value)
35
+ node = Node.new(value: value)
36
+ if head.nil?
37
+ @head = node
38
+ else
39
+ tail.next = node
40
+ node.prev = tail
41
+ end
42
+ @tail = node
43
+ @length += 1
44
+ node
45
+ end
46
+
47
+ def remove(value)
48
+ return unless head
49
+ return remove_head if head.value == value
50
+
51
+ current = head
52
+ current = current.next while current&.next && current.next.value != value
53
+ return unless current&.next
54
+
55
+ if current.next == tail
56
+ @tail = current
57
+ @tail.next = nil
58
+ else
59
+ current.next = current.next.next
60
+ current.next.prev = current
61
+ end
62
+ @length -= 1
63
+ end
64
+
65
+ def remove_head
66
+ if head.next.nil?
67
+ @head = nil
68
+ @tail = nil
69
+ else
70
+ head.next.prev = nil
71
+ @head = head.next
72
+ end
73
+ @length -= 1
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "common"
4
+ module Ds101
5
+ module LinkedList
6
+ class SingleLinkedList
7
+ include Ds101::LinkedList::Common
8
+ attr_accessor :head, :tail, :length
9
+
10
+ def initialize
11
+ @head = nil
12
+ @tail = nil
13
+ @length = 0
14
+ end
15
+
16
+ def self.init_from_arr(arr)
17
+ list = SingleLinkedList.new
18
+ arr.each do |value|
19
+ list.append(value)
20
+ end
21
+ list
22
+ end
23
+
24
+ def append(value)
25
+ node = Node.new(value: value)
26
+ if head.nil?
27
+ @head = node
28
+ else
29
+ tail.next = node
30
+ end
31
+ @tail = node
32
+ @length += 1
33
+ node
34
+ end
35
+
36
+ def remove(value)
37
+ return unless head
38
+ return remove_head if head.value == value
39
+
40
+ current = head
41
+ current = current.next while current&.next && current.next.value != value
42
+ return unless current&.next
43
+
44
+ if current.next == tail
45
+ @tail = current
46
+ else
47
+ current.next = current.next.next
48
+ end
49
+ @length -= 1
50
+ end
51
+
52
+ def remove_head
53
+ if head.next.nil?
54
+ @head = nil
55
+ @tail = nil
56
+ else
57
+ @head = head.next
58
+ end
59
+ @length -= 1
60
+ end
61
+
62
+ class Node
63
+ attr_accessor :value, :next
64
+
65
+ def initialize(value:, next_node: nil)
66
+ @value = value
67
+ @next = next_node
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ds101
4
+ VERSION = "0.1.0"
5
+ end
data/lib/ds_101.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ds_101/version"
4
+ require "ds_101/linked_list/single_linked_list"
5
+ require "ds_101/linked_list/double_linked_list"
6
+
7
+ module Ds101
8
+ class Error < StandardError; end
9
+ # Your code goes here...
10
+ end
data/sig/ds_101.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Ds101
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ds_101
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ShroukAbozeid
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-07-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '13.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '13.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.21'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.21'
55
+ description:
56
+ email:
57
+ - shrouk.dev@proton.me
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".rspec"
63
+ - ".rubocop.yml"
64
+ - CHANGELOG.md
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/ds_101.rb
69
+ - lib/ds_101/linked_list/common.rb
70
+ - lib/ds_101/linked_list/double_linked_list.rb
71
+ - lib/ds_101/linked_list/single_linked_list.rb
72
+ - lib/ds_101/version.rb
73
+ - sig/ds_101.rbs
74
+ homepage: https://github.com/ShroukAbozeid/ds_101
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ homepage_uri: https://github.com/ShroukAbozeid/ds_101
79
+ source_code_uri: https://github.com/ShroukAbozeid/ds_101
80
+ changelog_uri: https://github.com/ShroukAbozeid/ds_101/blob/master/CHANGELOG.md
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 3.0.0
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.4.10
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A Simple Implementation of Data Structure in Ruby
100
+ test_files: []