Skip to content

Commit 40e22ac

Browse files
authored
Merge pull request #768 from fartem/refactoring-9
2024-11-07 v. 6.9.8.1: updated some solutions
2 parents cc49939 + 993454c commit 40e22ac

7 files changed

+9
-51
lines changed

leetcode-ruby.gemspec

+1-1
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 = '6.9.8'
8+
s.version = '6.9.8.1'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/easy/1417_reformat_the_string.rb

+1-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def reformat(s)
77
letters = []
88
digits = []
99
s.each_char do |c|
10-
if _1417_is_letter?(c)
10+
if c =~ /[A-Za-z]/
1111
letters << c
1212
else
1313
digits << c
@@ -37,9 +37,3 @@ def reformat(s)
3737

3838
''
3939
end
40-
41-
# @param {String} s
42-
# @return {Boolean}
43-
def _1417_is_letter?(s)
44-
s =~ /[A-Za-z]/
45-
end

lib/easy/1796_second_largest_digit_in_a_string.rb

+1-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def second_highest(s)
77
max = -1
88
result = -1
99
s.each_char do |c|
10-
next unless _1796_is_digit?(c)
10+
next unless c =~ /\d/
1111

1212
num = c.to_i
1313
if num > max
@@ -20,11 +20,3 @@ def second_highest(s)
2020

2121
result
2222
end
23-
24-
# @param {String} s
25-
# @return {Boolean}
26-
def _1796_is_digit?(s)
27-
code = s.ord
28-
29-
code >= 48 && code <= 57
30-
end

lib/easy/1805_number_of_different_integers_in_a_string.rb

+2-10
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ def num_different_integers(word)
99
while p < word.length
1010
c = word[p]
1111

12-
unless _1805_is_digit?(c)
12+
unless c =~ /\d/
1313
p += 1
1414

1515
next
1616
end
1717

1818
leading_zero = c == '0'
1919
num = []
20-
while _1805_is_digit?(c)
20+
while c =~ /\d/
2121
if !leading_zero
2222
num << c
2323
elsif c != '0'
@@ -38,11 +38,3 @@ def num_different_integers(word)
3838

3939
uniq.length
4040
end
41-
42-
# @param {String} s
43-
# @return {Boolean}
44-
def _1805_is_digit?(s)
45-
code = s.ord
46-
47-
code >= 48 && code <= 57
48-
end

lib/easy/2042_check_if_numbers_are_ascending_in_a_sentence.rb

+1-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
def are_numbers_ascending(s)
77
prev = -1
88
s.split.each do |word|
9-
next unless _2043_is_digit?(word[0])
9+
next unless word[0] =~ /\d/
1010

1111
num = word.to_i
1212

@@ -17,11 +17,3 @@ def are_numbers_ascending(s)
1717

1818
true
1919
end
20-
21-
# @param {String} s
22-
# @return {Boolean}
23-
def _2043_is_digit?(s)
24-
code = s.ord
25-
26-
code >= 48 && code <= 57
27-
end

lib/easy/819_most_common_word.rb

+1-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def most_common_word(paragraph, banned)
1111
counter = 0
1212
words_with_count = {}
1313
paragraph.downcase.each_char do |c|
14-
if _819_is_letter?(c)
14+
if c =~ /[A-Za-z]/
1515
word << c
1616
else
1717
candidate = word.join.strip
@@ -30,9 +30,3 @@ def most_common_word(paragraph, banned)
3030

3131
most_common_word || word.join
3232
end
33-
34-
# @param {String} s
35-
# @return {Boolean}
36-
def _819_is_letter?(s)
37-
s =~ /[A-Za-z]/
38-
end

lib/easy/917_reverse_only_letters.rb

+2-8
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ def reverse_only_letters(s)
99
r = chars.length - 1
1010
while l < r
1111
l_c = chars[l]
12-
unless _917_is_letter?(l_c)
12+
unless l_c =~ /[A-Za-z]/
1313
l += 1
1414

1515
next
1616
end
1717

1818
r_c = chars[r]
19-
unless _917_is_letter?(r_c)
19+
unless r_c =~ /[A-Za-z]/
2020
r -= 1
2121

2222
next
@@ -31,9 +31,3 @@ def reverse_only_letters(s)
3131

3232
chars.join
3333
end
34-
35-
# @param {String} s
36-
# @return {Boolean}
37-
def _917_is_letter?(s)
38-
s =~ /[A-Za-z]/
39-
end

0 commit comments

Comments
 (0)