Skip to content

Commit 0200a73

Browse files
authored
Merge pull request #605 from fartem/2303_Calculate_Amount_Paid_in_Taxes
2024-04-17 v. 5.4.8: added "2303. Calculate Amount Paid in Taxes"
2 parents eb51f03 + 9ce02f0 commit 0200a73

4 files changed

+40
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
408408
| 2287. Rearrange Characters to Make Target String | [Link](https://leetcode.com/problems/rearrange-characters-to-make-target-string/) | [Link](./lib/easy/2287_rearrange_characters_to_make_target_string.rb) |
409409
| 2293. Min Max Game | [Link](https://leetcode.com/problems/min-max-game/) | [Link](./lib/easy/2293_min_max_game.rb) |
410410
| 2299. Strong Password Checker II | [Link](https://leetcode.com/problems/strong-password-checker-ii/) | [Link](./lib/easy/2299_strong_password_checker_ii.rb) |
411+
| 2303. Calculate Amount Paid in Taxes | [Link](https://leetcode.com/problems/calculate-amount-paid-in-taxes/) | [Link](./lib/easy/2303_calculate_amount_paid_in_taxes.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
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 = '5.4.7'
8+
s.version = '5.4.8'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/calculate-amount-paid-in-taxes/
4+
# @param {Integer[][]} brackets
5+
# @param {Integer} income
6+
# @return {Float}
7+
def calculate_tax(brackets, income)
8+
result = 0.0
9+
prev = 0
10+
brackets.each do |bracket|
11+
upper = bracket.first
12+
percent = bracket.last
13+
14+
return result + ((income - prev) * percent / 100.0) if income < upper
15+
16+
result += (upper - prev) * percent / 100.0
17+
prev = upper
18+
end
19+
20+
result
21+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/easy/2303_calculate_amount_paid_in_taxes'
5+
require 'minitest/autorun'
6+
7+
class CalculateAmountPaidInTaxesTest < ::Minitest::Test
8+
def test_default
9+
assert_equal(2.65000, calculate_tax([[3, 50], [7, 10], [12, 25]], 10))
10+
assert_equal(0.25000, calculate_tax([[1, 0], [4, 25], [5, 50]], 2))
11+
assert_equal(0.00000, calculate_tax([[2, 50]], 0))
12+
end
13+
14+
def test_additional
15+
assert_equal(0.33000, calculate_tax([[1, 33]], 1))
16+
end
17+
end

0 commit comments

Comments
 (0)