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
# @return {String}
def defang_i_paddr(address)
new_addr = address.gsub(".", "[.]")
return new_addr
address.gsub('.', '[.]')
end

View file

@ -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

View file

@ -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()
min_length = strs.map(&:length).min
res = ""
for i in 0..min_length - 1 do
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

View file

@ -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