-
Notifications
You must be signed in to change notification settings - Fork 9.8k
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
Conversation
There was a problem hiding this 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
# 将列表元素原封不动添加进堆 | ||
@max_heap = nums.to_a | ||
# 堆化除叶节点以外的其他所有节点 | ||
for i in (0...parent(size)).reverse_each |
There was a problem hiding this comment.
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)
codes/ruby/chapter_heap/my_heap.rb
Outdated
end | ||
|
||
### 获取堆的数组形式 ### | ||
def to_a |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
Co-authored-by: khoaxuantu <68913255+khoaxuantu@users.noreply.github.com>
Co-authored-by: khoaxuantu <68913255+khoaxuantu@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this 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!
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: