Fix some rubocop warnings

This commit is contained in:
Ivan R. 2022-11-26 16:14:11 +05:00
parent bb2ac41656
commit 32f9a1dd8d
No known key found for this signature in database
GPG key ID: 56C7BAAE859B302C
4 changed files with 20 additions and 28 deletions

View file

@ -1,6 +1,5 @@
# @param {String} address # @param {String} address
# @return {String} # @return {String}
def defang_i_paddr(address) def defang_i_paddr(address)
new_addr = address.gsub(".", "[.]") address.gsub('.', '[.]')
return new_addr
end end

View file

@ -1,11 +1,9 @@
require_relative "main" require_relative 'main'
require "test/unit" require 'test/unit'
class TestDefangIpAddr < Test::Unit::TestCase class TestDefangIpAddr < Test::Unit::TestCase
def test_simple def test_simple
assert_equal("1[.]1[.]1[.]1", defang_i_paddr("1.1.1.1") ) 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('255[.]100[.]50[.]0', defang_i_paddr('255.100.50.0'))
end end
end end

View file

@ -1,21 +1,18 @@
# @param {String[]} strs # @param {String[]} strs
# @return {String} # @return {String}
def longest_common_prefix(strs) def longest_common_prefix(strs)
# Find min length min_length = strs.map(&:length).min
min_length = strs.map{|s| s.length}.min()
res = ''
res = "" (0...min_length).each do |i|
for i in 0..min_length - 1 do # Compare characters
strs.each do |st| strs.each do |st|
# If characters doesn't match return res if st[i] != strs[0][i]
if st[i] != strs[0][i]
return res
end
end end
# All characters match # All characters match
res += strs[0][i] res += strs[0][i]
end end
return res res
end end

View file

@ -1,18 +1,16 @@
require_relative "main" require_relative 'main'
require "test/unit" require 'test/unit'
class TestLCP < Test::Unit::TestCase class TestLCP < Test::Unit::TestCase
def test_simple def test_simple
assert_equal("fl", longest_common_prefix(["flower","flow","flight"]) ) assert_equal('fl', longest_common_prefix(%w[flower flow flight]))
end end
def test_empty_prefix def test_empty_prefix
assert_equal("", longest_common_prefix(["dog","racecar","car"]) ) assert_equal('', longest_common_prefix(%w[dog racecar car]))
end end
def test_empty_string def test_empty_string
assert_equal("", longest_common_prefix([""]) ) assert_equal('', longest_common_prefix(['']))
end end
end end