From 326b383101389691ad0cdd77c187de177f6cd7b9 Mon Sep 17 00:00:00 2001 From: fartem Date: Mon, 2 Dec 2024 09:01:37 +0300 Subject: [PATCH] 2024-12-02 v. 7.1.6: added "468. Validate IP Address" --- .rubocop.yml | 3 ++ README.md | 1 + leetcode-ruby.gemspec | 2 +- lib/easy/938_range_sum_of_bst.rb | 4 +-- lib/medium/468_validate_ip_address.rb | 12 ++++++++ test/medium/test_468_validate_ip_address.rb | 34 +++++++++++++++++++++ 6 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 lib/medium/468_validate_ip_address.rb create mode 100644 test/medium/test_468_validate_ip_address.rb diff --git a/.rubocop.yml b/.rubocop.yml index 7126a350..6c3ef322 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -56,3 +56,6 @@ Lint/DuplicateBranch: Enabled: false Lint/UselessSetterCall: Enabled: false + +Layout/LineLength: + Enabled: false diff --git a/README.md b/README.md index 0791828a..2bd8f9d7 100644 --- a/README.md +++ b/README.md @@ -581,3 +581,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 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) | | 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) | | 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) | +| 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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 130f1681..b33f633c 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -5,7 +5,7 @@ require 'English' ::Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.name = 'leetcode-ruby' - s.version = '7.1.5' + s.version = '7.1.6' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/easy/938_range_sum_of_bst.rb b/lib/easy/938_range_sum_of_bst.rb index b8d003db..2ff486f0 100644 --- a/lib/easy/938_range_sum_of_bst.rb +++ b/lib/easy/938_range_sum_of_bst.rb @@ -8,9 +8,7 @@ def range_sum_bst(root, low, high) return 0 if root.nil? - if root.val >= low && root.val <= high - return root.val + range_sum_bst(root.left, low, high) + range_sum_bst(root.right, low, high) - end + return root.val + range_sum_bst(root.left, low, high) + range_sum_bst(root.right, low, high) if root.val >= low && root.val <= high return range_sum_bst(root.right, low, high) if root.val < low diff --git a/lib/medium/468_validate_ip_address.rb b/lib/medium/468_validate_ip_address.rb new file mode 100644 index 00000000..bbd15844 --- /dev/null +++ b/lib/medium/468_validate_ip_address.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/validate-ip-address/ +# @param {String} query_ip +# @return {String} +def valid_ip_address(query_ip) = ipv4(query_ip) || ipv6(query_ip) || 'Neither' + +private + +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 + +def ipv6(ip) = ip.upcase =~ /\A([0-9A-F]{1,4}:){7}([0-9A-F]{1,4})\Z/ ? 'IPv6' : nil diff --git a/test/medium/test_468_validate_ip_address.rb b/test/medium/test_468_validate_ip_address.rb new file mode 100644 index 00000000..896a7330 --- /dev/null +++ b/test/medium/test_468_validate_ip_address.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/468_validate_ip_address' +require 'minitest/autorun' + +class ValidateIPAddressTest < ::Minitest::Test + def test_default_one + assert_equal( + 'IPv4', + valid_ip_address( + '172.16.254.1' + ) + ) + end + + def test_default_two + assert_equal( + 'IPv6', + valid_ip_address( + '2001:0db8:85a3:0:0:8A2E:0370:7334' + ) + ) + end + + def test_default_three + assert_equal( + 'Neither', + valid_ip_address( + '256.256.256.256' + ) + ) + end +end