Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby day19 #292

Merged
merged 4 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Exercism Workspace
| July | Jurassic | C (5/5), C++ (5/5), Fortran (0/5) |
| August | Apps | Dart (5/5), Java (0/5), Kotlin (0/5) |
| September | Slimline | Awk (5/5), Bash\* (5/5), jq (0/5), Perl (0/5) |
| October | Object-Oriented | Ruby (3/5), Java (0/5) |
| October | Object-Oriented | Ruby (5/5), Java (0/5) |
| November | Nibbly | |
| December | Diversions | |

Expand Down
2 changes: 2 additions & 0 deletions ruby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@
- [simple-cipher](./simple-cipher/README.md)
- [clock](./clock/README.md)
- [circular-buffer](./circular-buffer/README.md)
- [binary-search-tree](./binary-search-tree/README.md)
- [matrix](./matrix/README.md)
33 changes: 33 additions & 0 deletions ruby/binary-search-tree/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"authors": [
"vosechu"
],
"contributors": [
"budmc29",
"cadwallion",
"dkinzer",
"hilary",
"iHiD",
"Insti",
"kotp",
"kytrinyx",
"pendletons",
"remcopeereboom",
"Ryan1729",
"tryantwit"
],
"files": {
"solution": [
"binary_search_tree.rb"
],
"test": [
"binary_search_tree_test.rb"
],
"example": [
".meta/example.rb"
]
},
"blurb": "Insert and search for numbers in a binary tree.",
"source": "Josh Cheek",
"source_url": "https://twitter.com/josh_cheek"
}
1 change: 1 addition & 0 deletions ruby/binary-search-tree/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"ruby","exercise":"binary-search-tree","id":"fd41235f072844e28632df5ffead0037","url":"https://exercism.org/tracks/ruby/exercises/binary-search-tree","handle":"vpayno","is_requester":true,"auto_approve":false}
54 changes: 54 additions & 0 deletions ruby/binary-search-tree/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Help

## Running the tests

For running the tests provided, you will need the Minitest gem. Open a
terminal window and run the following command to install minitest:

```
gem install minitest
```


Run the tests from the exercise directory using the following command:

```
ruby <snake-case-exercise>_test.rb
```

Please replace `<snake-case-exercise>` with your exercise name in snake_case.

## Color output

You can `require 'minitest/pride'` or run the following command to get colored output:

```
ruby -r minitest/pride <snake-case-exercise>_test.rb
```

## Submitting your solution

You can submit your solution using the `exercism submit binary_search_tree.rb` command.
This command will upload your solution to the Exercism website and print the solution page's URL.

It's possible to submit an incomplete solution which allows you to:

- See how others have completed the exercise
- Request help from a mentor

## Need to get help?

If you'd like help solving the exercise, check the following pages:

- The [Ruby track's documentation](https://exercism.org/docs/tracks/ruby)
- The [Ruby track's programming category on the forum](https://forum.exercism.org/c/programming/ruby)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)

Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.

To get help if you're having trouble, you can use one of the following resources:

- [Ruby Documentation](http://ruby-doc.org/)
- [StackOverflow](http://stackoverflow.com/questions/tagged/ruby)
- [/r/ruby](https://www.reddit.com/r/ruby) is the Ruby subreddit.
82 changes: 82 additions & 0 deletions ruby/binary-search-tree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Binary Search Tree

Welcome to Binary Search Tree on Exercism's Ruby Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Instructions

Insert and search for numbers in a binary tree.

When we need to represent sorted data, an array does not make a good data structure.

Say we have the array `[1, 3, 4, 5]`, and we add 2 to it so it becomes `[1, 3, 4, 5, 2]`.
Now we must sort the entire array again!
We can improve on this by realizing that we only need to make space for the new item `[1, nil, 3, 4, 5]`, and then adding the item in the space we added.
But this still requires us to shift many elements down by one.

Binary Search Trees, however, can operate on sorted data much more efficiently.

A binary search tree consists of a series of connected nodes.
Each node contains a piece of data (e.g. the number 3), a variable named `left`, and a variable named `right`.
The `left` and `right` variables point at `nil`, or other nodes.
Since these other nodes in turn have other nodes beneath them, we say that the left and right variables are pointing at subtrees.
All data in the left subtree is less than or equal to the current node's data, and all data in the right subtree is greater than the current node's data.

For example, if we had a node containing the data 4, and we added the data 2, our tree would look like this:

4
/
2

If we then added 6, it would look like this:

4
/ \
2 6

If we then added 3, it would look like this

4
/ \
2 6
\
3

And if we then added 1, 5, and 7, it would look like this

4
/ \
/ \
2 6
/ \ / \
1 3 5 7

## Source

### Created by

- @vosechu

### Contributed to by

- @budmc29
- @cadwallion
- @dkinzer
- @hilary
- @iHiD
- @Insti
- @kotp
- @kytrinyx
- @pendletons
- @remcopeereboom
- @Ryan1729
- @tryantwit

### Based on

Josh Cheek - https://twitter.com/josh_cheek

### My Solution

- [my solution](./binary_search_tree.rb)
- [run-tests](./run-tests-ruby.txt)
29 changes: 29 additions & 0 deletions ruby/binary-search-tree/binary_search_tree.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: false

# https://exercism.org/tracks/ruby/exercises/binary-search-tree
# Binary Search Tree exercise
class Bst
attr_reader :data, :left, :right

def initialize(data = nil)
@data = data
@left = nil
@right = nil
end

def insert(data)
if data <= @data
@left ? @left.insert(data) : @left = Bst.new(data)
else
@right ? @right.insert(data) : @right = Bst.new(data)
end
end

def each(&block)
return enum_for(:each) unless block_given?

@left&.each(&block)
block.call(@data)
@right&.each(&block)
end
end
125 changes: 125 additions & 0 deletions ruby/binary-search-tree/binary_search_tree_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# frozen_string_literal: false

# https://github.com/simplecov-ruby/simplecov
require 'simplecov'

# https://about.codecov.io/blog/getting-started-with-code-coverage-for-ruby/
require 'simplecov-cobertura'
SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter

# line coverage
SimpleCov.start if ENV['COVERAGE'] != 'branch'

# branch coverage
if ENV['COVERAGE'] == 'branch'
SimpleCov.start do
enable_coverage :branch
primary_coverage :branch
end
end

# name the test file/group
SimpleCov.command_name 'test:exercism'

# original exercism tests
require 'minitest/autorun'
require_relative 'binary_search_tree'

class BstTest < Minitest::Test
def test_data_is_retained
assert_equal 4, Bst.new(4).data
end

def test_inserting_less
# skip
four = Bst.new 4
four.insert 2
assert_equal 4, four.data
assert_equal 2, four.left.data
end

def test_inserting_same
# skip
four = Bst.new 4
four.insert 4
assert_equal 4, four.data
assert_equal 4, four.left.data
end

def test_inserting_right
# skip
four = Bst.new 4
four.insert 5
assert_equal 4, four.data
assert_equal 5, four.right.data
end

def test_complex_tree
# skip
four = Bst.new 4
four.insert 2
four.insert 6
four.insert 1
four.insert 3
four.insert 7
four.insert 5
assert_equal 4, four.data
assert_equal 2, four.left.data
assert_equal 1, four.left.left.data
assert_equal 3, four.left.right.data
assert_equal 6, four.right.data
assert_equal 5, four.right.left.data
assert_equal 7, four.right.right.data
end

def record_all_data(bst)
all_data = []
bst.each { |data| all_data << data }
all_data
end

def test_iterating_one_element
# skip
assert_equal [4], record_all_data(Bst.new(4))
end

def test_iterating_over_smaller_element
# skip
four = Bst.new 4
four.insert 2
assert_equal [2, 4], record_all_data(four)
end

def test_iterating_over_larger_element
# skip
four = Bst.new 4
four.insert 5
assert_equal [4, 5], record_all_data(four)
end

def test_iterating_over_complex_tree
# skip
four = Bst.new 4
four.insert 2
four.insert 1
four.insert 3
four.insert 6
four.insert 7
four.insert 5
assert_equal [1, 2, 3, 4, 5, 6, 7], record_all_data(four)
end

def test_each_returns_enumerator_if_no_block
# skip

tree = Bst.new 4
[2, 1, 3, 6, 7, 5].each { |x| tree.insert x }
each_enumerator = tree.each

assert_kind_of Enumerator, each_enumerator

(1..7).each { |x| assert_equal(x, each_enumerator.next) }

assert_raises(StopIteration) { each_enumerator.next }
end
end
5 changes: 5 additions & 0 deletions ruby/binary-search-tree/coverage/.last_run.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"result": {
"line": 100.0
}
}
40 changes: 40 additions & 0 deletions ruby/binary-search-tree/coverage/.resultset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"test:exercism": {
"coverage": {
"/home/vpayno/git_vpayno/exercism-workspace/ruby/binary-search-tree/binary_search_tree.rb": {
"lines": [
null,
null,
null,
null,
1,
1,
null,
1,
33,
33,
33,
null,
null,
1,
35,
18,
null,
17,
null,
null,
null,
1,
20,
null,
19,
19,
19,
null,
null
]
}
},
"timestamp": 1698805764
}
}
Empty file.
Loading