7 kyu

Split The Bill

3,849 of 8,648otajor

Description:

You're in a restaurant with your friends and it's time to go, but there's still one big problem...the bill. Who will pay what? Lucky for you, you've got your computer handy! One simple function and the bill is paid——fairly, too!

The function should take one parameter: an object/dict with two or more name-value pairs that represent the members of the group and the amount spent by each.

Your function should return an object/dict with the same names, showing how much money the members should pay or receive.

Further points:

  • The values should be positive numbers if the person should receive money from the group, negative numbers if they owe money to the group.
  • If value is a decimal, round to two decimal places.

Translations and comments (and upvotes!) welcome.

Example

3 friends go out together: A spends £20, B spends £15, and C spends £10. The function should return an object/dict showing that A should receive £5, B should receive £0, and C should pay £5.

var group = {
    A: 20, 
    B: 15, 
    C: 10
}

splitTheBill(group) // returns {A: 5, B: 0, C: -5}
group = {
    'A': 20, 
    'B': 15, 
    'C': 10
}

split_the_bill(group) # returns {'A': 5, 'B': 0, 'C': -5}
group = {
    'A'=>20, 
    'B'=>15, 
    'C'=>10
}

split_the_bill(group) # returns {'A'=>5, 'B'=>0, 'C'=>-5}
group = %{
  :A => 20,
  :B => 15,
  :C => 10
}

split_the_bill(group) # returns %{:A => 5, :B => 0, :C => -5}

Good Luck and Happy Eats!

Fundamentals
Data Structures

Similar Kata:

More By Author:

Check out these other kata created by otajor

Stats:

CreatedNov 9, 2015
PublishedNov 10, 2015
Warriors Trained14514
Total Skips787
Total Code Submissions68269
Total Times Completed8648
JavaScript Completions3894
Python Completions3849
Ruby Completions611
Elixir Completions203
TypeScript Completions226
Total Stars225
% of votes with a positive feedback rating88% of 949
Total "Very Satisfied" Votes774
Total "Somewhat Satisfied" Votes126
Total "Not Satisfied" Votes49
Ad
Contributors
  • otajor Avatar
  • GiacomoSorbi Avatar
  • NaMe613 Avatar
  • Chrono79 Avatar
  • kazk Avatar
  • user5036852 Avatar
  • misayre Avatar
  • hobovsky Avatar
  • anusha.j Avatar
  • 4500zenja1 Avatar
  • saudiGuy Avatar
Ad