loading...

Daily Challenge #146 - The Dollar Game

thepracticaldev profile image dev.to staff ・2 min read

Setup

You are given a list of people and their current net worth in U.S. Dollars. Some people might have a positive net worth, others could have a negative net worth (i.e. are in debt), while some are just broke.

There are many friendships across these financial groups. No single person is alone typically, you can draw a friendship line from one person to any other person through x number of people. In this community, each person gives 1 dollar to each of their friends. Additionally, each person can take 1 dollar from each of their friends.

Who should take or give a dollar so that everyone has a non-negative balance?

Input

Your function giveAndTake will pass two arguments

  • people - a hash object with names as keys and net worth as value.
  • friendships - an array of pairs of names that are friends.

Output

Your function should return an array of 'moves'. Each 'move' is an array of two elements: The name of a person and the value 1 or -1. 1 indicates they should take a dollar from each friend. -1 indicates they should give a dollar to each friend.

For example: [['David',1], ['Brian',-1], ['Alice',-1], ['Charlie',1]]

Tests

people: {Adam: 1, Bob: -1, Carol: -2, Donna: 2, Eddie: 3}
friendships: [['Adam','Carol'],['Adam','Eddie'],['Bob','Carol'],['Carol','Eddie'],['Donna','Eddie']] 

people: {Adam: -4, Bob: 1, Carol: 2, Donna: 2, Eddie: 0}
friendships: [['Adam','Donna'],['Adam','Carol'],['Bob','Carol'],['Carol','Eddie'],['Donna','Eddie']]

people:{Adam: 0, Bob: 2, Carol: -2, Donna: 3, Eddie: -2}
friendships:[['Adam','Carol'],['Adam','Donna'],['Bob','Carol'],['Carol','Eddie'],['Donna','Eddie']]

Good luck!


This challenge comes from hufftheweevil on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Discussion

pic
Editor guide
Collapse
idanarye profile image
Idan Arye

Are people allowed to bring themselves and/or their friends into debt? If they are allowed, then the order of moves is not important - only the final number times each person given minus the final number of times they have taken.

Collapse
souk profile image
Souk

My attempt... There is no givers in my cruel world. Just steal/take from friends.

gist.github.com/aiibe/91b8de9c9fde...