Skip to content

Commit 2158eb4

Browse files
authored
Merge pull request #787 from fartem/468_Validate_IP_Address
2024-12-02 v. 7.1.6: added "468. Validate IP Address"
2 parents aa3fe6e + 326b383 commit 2158eb4

File tree

6 files changed

+52
-4
lines changed

6 files changed

+52
-4
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ Lint/DuplicateBranch:
5656
Enabled: false
5757
Lint/UselessSetterCall:
5858
Enabled: false
59+
60+
Layout/LineLength:
61+
Enabled: false

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
581581
| 453. Minimum Moves to Equal Array Elements | [Link](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/) | [Link](./lib/medium/453_minimum_moves_to_equal_array_elements.rb) | [Link](./test/medium/test_453_minimum_moves_to_equal_array_elements.rb) |
582582
| 456. 132 Pattern | [Link](https://leetcode.com/problems/132-pattern/) | [Link](./lib/medium/456_132_pattern.rb) | [Link](./test/medium/test_456_132_pattern.rb) |
583583
| 462. Minimum Moves to Equal Array Elements II | [Link](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [Link](./lib/medium/462_minimum_moves_to_equal_array_elements_ii.rb) | [Link](./test/medium/test_462_minimum_moves_to_equal_array_elements_ii.rb) |
584+
| 468. Validate IP Address | [Link](https://leetcode.com/problems/validate-ip-address/) | [Link](./lib/medium/468_validate_ip_address.rb) | [Link](./test/medium/test_468_validate_ip_address.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '7.1.5'
8+
s.version = '7.1.6'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/easy/938_range_sum_of_bst.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
def range_sum_bst(root, low, high)
99
return 0 if root.nil?
1010

11-
if root.val >= low && root.val <= high
12-
return root.val + range_sum_bst(root.left, low, high) + range_sum_bst(root.right, low, high)
13-
end
11+
return root.val + range_sum_bst(root.left, low, high) + range_sum_bst(root.right, low, high) if root.val >= low && root.val <= high
1412

1513
return range_sum_bst(root.right, low, high) if root.val < low
1614

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/validate-ip-address/
4+
# @param {String} query_ip
5+
# @return {String}
6+
def valid_ip_address(query_ip) = ipv4(query_ip) || ipv6(query_ip) || 'Neither'
7+
8+
private
9+
10+
def ipv4(ip) = ip =~ /\A((1[0-9]{2}|25[0-5]|2[0-4][0-9]|[1-9][0-9]|[0-9])\.){3}(1[0-9]{2}|25[0-5]|2[0-4][0-9]|[1-9][0-9]|[0-9])\Z/ ? 'IPv4' : nil
11+
12+
def ipv6(ip) = ip.upcase =~ /\A([0-9A-F]{1,4}:){7}([0-9A-F]{1,4})\Z/ ? 'IPv6' : nil
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/468_validate_ip_address'
5+
require 'minitest/autorun'
6+
7+
class ValidateIPAddressTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
'IPv4',
11+
valid_ip_address(
12+
'172.16.254.1'
13+
)
14+
)
15+
end
16+
17+
def test_default_two
18+
assert_equal(
19+
'IPv6',
20+
valid_ip_address(
21+
'2001:0db8:85a3:0:0:8A2E:0370:7334'
22+
)
23+
)
24+
end
25+
26+
def test_default_three
27+
assert_equal(
28+
'Neither',
29+
valid_ip_address(
30+
'256.256.256.256'
31+
)
32+
)
33+
end
34+
end

0 commit comments

Comments
 (0)