diff --git a/problems/1108-defanging-an-ip-address/main.rb b/problems/1108-defanging-an-ip-address/main.rb index f3c0532..801139d 100644 --- a/problems/1108-defanging-an-ip-address/main.rb +++ b/problems/1108-defanging-an-ip-address/main.rb @@ -1,6 +1,5 @@ # @param {String} address # @return {String} def defang_i_paddr(address) - new_addr = address.gsub(".", "[.]") - return new_addr + address.gsub('.', '[.]') end diff --git a/problems/1108-defanging-an-ip-address/test.rb b/problems/1108-defanging-an-ip-address/test.rb index 1447087..370b6d3 100644 --- a/problems/1108-defanging-an-ip-address/test.rb +++ b/problems/1108-defanging-an-ip-address/test.rb @@ -1,11 +1,9 @@ -require_relative "main" -require "test/unit" - +require_relative 'main' +require 'test/unit' + class TestDefangIpAddr < Test::Unit::TestCase - def test_simple - assert_equal("1[.]1[.]1[.]1", defang_i_paddr("1.1.1.1") ) - assert_equal("255[.]100[.]50[.]0", defang_i_paddr("255.100.50.0") ) + assert_equal('1[.]1[.]1[.]1', defang_i_paddr('1.1.1.1')) + assert_equal('255[.]100[.]50[.]0', defang_i_paddr('255.100.50.0')) end - end diff --git a/problems/14-longest-common-prefix/main.rb b/problems/14-longest-common-prefix/main.rb index 2ec1842..f2fcd05 100644 --- a/problems/14-longest-common-prefix/main.rb +++ b/problems/14-longest-common-prefix/main.rb @@ -1,21 +1,18 @@ # @param {String[]} strs # @return {String} def longest_common_prefix(strs) - # Find min length - min_length = strs.map{|s| s.length}.min() - - res = "" - for i in 0..min_length - 1 do + min_length = strs.map(&:length).min + + res = '' + (0...min_length).each do |i| + # Compare characters strs.each do |st| - # If characters doesn't match - if st[i] != strs[0][i] - return res - end + return res if st[i] != strs[0][i] end - + # All characters match res += strs[0][i] end - return res + res end diff --git a/problems/14-longest-common-prefix/test.rb b/problems/14-longest-common-prefix/test.rb index 7332c24..5562c73 100644 --- a/problems/14-longest-common-prefix/test.rb +++ b/problems/14-longest-common-prefix/test.rb @@ -1,18 +1,16 @@ -require_relative "main" -require "test/unit" - +require_relative 'main' +require 'test/unit' + class TestLCP < Test::Unit::TestCase - def test_simple - assert_equal("fl", longest_common_prefix(["flower","flow","flight"]) ) + assert_equal('fl', longest_common_prefix(%w[flower flow flight])) end def test_empty_prefix - assert_equal("", longest_common_prefix(["dog","racecar","car"]) ) + assert_equal('', longest_common_prefix(%w[dog racecar car])) end def test_empty_string - assert_equal("", longest_common_prefix([""]) ) + assert_equal('', longest_common_prefix([''])) end - end