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

feat: Add ruby codes - chapter "Heap" #1300

Merged
merged 11 commits into from
Apr 28, 2024

Conversation

bluebean-cloud
Copy link
Contributor

Because there is no heap or priority queue in the Ruby standard library, "heap.rb" should not exist, and I used "my_heap.rb" to finish "top_k.rb", refering to the C version.


If this pull request (PR) is associated with coding or code transpilation, please attach the relevant console outputs to the PR and complete the following checklist:

  • I have thoroughly reviewed the code, focusing on its formatting, comments, indentation, and file headers.
  • I have confirmed that the code execution outputs are consistent with those produced by the reference code (Python or Java).
  • The code is designed to be compatible on standard operating systems, including Windows, macOS, and Ubuntu.
    image
    image

Copy link
Contributor

@khoaxuantu khoaxuantu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @bluebean-cloud, I have some comments:

codes/ruby/chapter_heap/my_heap.rb Outdated Show resolved Hide resolved
# 将列表元素原封不动添加进堆
@max_heap = nums.to_a
# 堆化除叶节点以外的其他所有节点
for i in (0...parent(size)).reverse_each
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This traverses in range (parent(size), 0]. It is different from the reference codes, which traverse in range [parent(size - 1), 0].
I would not recommend to use reverse_each here due to the readability (you have to imagine a reverse range when looking at it). Instead, we can use

parent(size - 1).downto(0)

end

### 获取堆的数组形式 ###
def to_a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a getter attr_reader for this method and put it on top of the class. Make sure you change the method references.

### 获取堆的数组形式 ###
attr_reader :max_heap

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for review! I have fix them in aa99844

codes/ruby/chapter_heap/top_k.rb Outdated Show resolved Hide resolved
bluebean-cloud and others added 3 commits April 27, 2024 11:14
Co-authored-by: khoaxuantu <68913255+khoaxuantu@users.noreply.github.com>
Co-authored-by: khoaxuantu <68913255+khoaxuantu@users.noreply.github.com>
Copy link
Contributor

@khoaxuantu khoaxuantu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Owner

@krahets krahets left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

@krahets krahets merged commit a3950e1 into krahets:main Apr 28, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants