diff --git a/README.md b/README.md index 13289ba3..4182dafc 100644 --- a/README.md +++ b/README.md @@ -447,3 +447,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 2506. Count Pairs Of Similar Strings | [Link](https://leetcode.com/problems/count-pairs-of-similar-strings/) | [Link](./lib/easy/2506_count_pairs_of_similar_strings.rb) | | 2511. Maximum Enemy Forts That Can Be Captured | [Link](https://leetcode.com/problems/maximum-enemy-forts-that-can-be-captured/) | [Link](./lib/easy/2511_maximum_enemy_forts_that_can_be_captured.rb) | | 2515. Shortest Distance to Target String in a Circular Array | [Link](https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array/) | [Link](./lib/easy/2515_shortest_distance_to_target_string_in_a_circular_array.rb) | +| 2520. Count the Digits That Divide a Number | [Link](https://leetcode.com/problems/count-the-digits-that-divide-a-number/) | [Link](./lib/easy/2520_count_the_digits_that_divide_a_number.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index dd9c00d7..6cbad724 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 = '5.8.6' + s.version = '5.8.7' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE] s.executable = 'leetcode-ruby' diff --git a/lib/easy/2520_count_the_digits_that_divide_a_number.rb b/lib/easy/2520_count_the_digits_that_divide_a_number.rb new file mode 100644 index 00000000..919407ee --- /dev/null +++ b/lib/easy/2520_count_the_digits_that_divide_a_number.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/count-the-digits-that-divide-a-number/ +# @param {Integer} num +# @return {Integer} +def count_digits(num) + original = num + result = 0 + while num.positive? + digit = num % 10 + result += 1 if (original % digit).zero? + num /= 10 + end + + result +end diff --git a/test/easy/test_2520_count_the_digits_that_divide_a_number.rb b/test/easy/test_2520_count_the_digits_that_divide_a_number.rb new file mode 100644 index 00000000..3c0d3792 --- /dev/null +++ b/test/easy/test_2520_count_the_digits_that_divide_a_number.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/easy/2520_count_the_digits_that_divide_a_number' +require 'minitest/autorun' + +class CountTheDigitsThatDivideANumberTest < ::Minitest::Test + def test_default + assert_equal(1, count_digits(7)) + assert_equal(2, count_digits(121)) + assert_equal(4, count_digits(1248)) + end +end