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 code - chapter "Searching" #1262

Merged
merged 12 commits into from
Apr 11, 2024

Conversation

bluebean-cloud
Copy link
Contributor

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.

Ruby_code_searching_run_result

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, thanks for the PR, I have some comments

=end

### 二分查找插入点(存在重复元素) ###
def binary_search_insertion(nums, target)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please import this method from the binary_search_insertion.rb file

require_relative './binary_search_insertion'

Copy link
Contributor Author

@bluebean-cloud bluebean-cloud Apr 10, 2024

Choose a reason for hiding this comment

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

Thank you for reviewing!

If I use require_relative, then the driver Code in binary_search_insertion.rb may be like:

### Driver Code ###
if __FILE__ == $0
  # Code
end

Otherwise the driver code in binary_search_insertion.rb will be executed when executing binary_serch_edge.rb.

So may I make this change? @khoaxuantu

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it is. Actually, at the very first I did not expect the case that we will import another method from an index file. I will create another PR to put all the existing driver code at branch main into the __FILE__ == 0 block.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. I will put my driver code into the __FILE__==0 block.

Thanks for suggestion!

Copy link
Contributor Author

@bluebean-cloud bluebean-cloud Apr 11, 2024

Choose a reason for hiding this comment

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

369f7e3 has fixed all bugs. thank you for the review!

codes/ruby/chapter_searching/hashing_search.rb Outdated Show resolved Hide resolved
codes/ruby/chapter_searching/linear_search.rb Outdated Show resolved Hide resolved
### 方法一:暴力枚举 ###
def two_sum_brute_force(nums, target)
# 两层循环,时间复杂度为 O(n^2)
for i in 0...nums.length
Copy link
Contributor

@khoaxuantu khoaxuantu Apr 10, 2024

Choose a reason for hiding this comment

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

In the python code, the for loop is

for i in range(len(nums) - 1):

It traverse the nums in range [0, nums.length - 1).

In your code, the loop traverses in range [0, nums.length). So please change to this to have a correct range:

suggestion~~ ~~for i in 0..nums.length~~ ~~

Copy link
Contributor Author

Choose a reason for hiding this comment

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

0..nums.lenth seems to traverse the nums in [0, nums.length]. I will repair it, thank you!

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops! I got a mistake in the suggestion code. It should be

Suggested change
for i in 0...nums.length
for i in 0...(nums.length - 1)

Sorry for the misinformation 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have made all the changes in commit
369f7e3

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 9233767 into krahets:main Apr 11, 2024
4 checks passed
@krahets krahets added the code Code-related label Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code Code-related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants