diff --git a/README.md b/README.md index 50a18973..bc19ef38 100644 --- a/README.md +++ b/README.md @@ -435,3 +435,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 2432. The Employee That Worked on the Longest Task | [Link](https://leetcode.com/problems/the-employee-that-worked-on-the-longest-task/) | [Link](./lib/easy/2432_the_employee_that_worked_on_the_longest_task.rb) | | 2441. Largest Positive Integer That Exists With Its Negative | [Link](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/) | [Link](./lib/easy/2441_largest_positive_integer_that_exists_with_its_negative.rb) | | 2446. Determine if Two Events Have Conflict | [Link](https://leetcode.com/problems/determine-if-two-events-have-conflict/) | [Link](./lib/easy/2446_determine_if_two_events_have_conflict.rb) | +| 2455. Average Value of Even Numbers That Are Divisible by Three | [Link](https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/) | [Link](./lib/easy/2455_average_value_of_even_numbers_that_are_divisible_by_three.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 1ad48636..9845c7cd 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.7.4' + s.version = '5.7.5' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE] s.executable = 'leetcode-ruby' diff --git a/lib/easy/2455_average_value_of_even_numbers_that_are_divisible_by_three.rb b/lib/easy/2455_average_value_of_even_numbers_that_are_divisible_by_three.rb new file mode 100644 index 00000000..1fdc5700 --- /dev/null +++ b/lib/easy/2455_average_value_of_even_numbers_that_are_divisible_by_three.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/ +# @param {Integer[]} nums +# @return {Integer} +def average_value(nums) + count = 0 + sum = 0 + + nums.each do |num| + next unless (num % 6).zero? + + count += 1 + sum += num + end + + count.positive? ? sum / count : 0 +end diff --git a/test/easy/test_2455_average_value_of_even_numbers_that_are_divisible_by_three.rb b/test/easy/test_2455_average_value_of_even_numbers_that_are_divisible_by_three.rb new file mode 100644 index 00000000..704ad647 --- /dev/null +++ b/test/easy/test_2455_average_value_of_even_numbers_that_are_divisible_by_three.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/easy/2455_average_value_of_even_numbers_that_are_divisible_by_three' +require 'minitest/autorun' + +class AverageValueOfEvenNumbersThatAreDivisibleByThreeTest < ::Minitest::Test + def test_default + assert_equal(9, average_value([1, 3, 6, 10, 12, 15])) + assert_equal(0, average_value([1, 2, 4, 7, 10])) + end +end