6 kyu

T.T.T.44: A game of Grab the "red envelope"(抢红包游戏)

Description:

Description & Rule

What's the "红包"(red envelope) means? Let me explain it:

Spring Festival is the most important festival in China. In this festival, young people will visit their elders. They will bless each other. In addition, the elders will give children a "red envelope", there's some money in it. This is a blessing and a reward.

In today's era, with the development of science and technology, the way of giving the red envelope has changed. China has two APP allows you to give a red envelope to other people by phone. The meaning of a red envelope is also changed from blessing to a means of payment. When people in the shopping, eating, watching movies, they can send a red envelope checkout.

In a name called "微信"(WeChat) mobile phone app, send a red envelope and grab a red envelope is to become an interesting game.

Let's see how to play this red envelope game:

This game is in a discussion group, we assume that there are five people involved in the game. First, choose a person to send the first red envelope. The red envelope is set to 5 pieces, the total amount is fixed, but each one is random. That is, five people can click once, to get a random amount of money. For example:

Five person play the red envelope game:
["John","Peter","Tom","Alice","Susan"]
Assuming they have an initial amount of 2000
John send the first red envelope, total amount is $100, 
and then five person click the red envelope and grab a random amount:
[18.5,20,21.5,19,21]

The game is over? No, this is just the beginning. They need to elect a person to continue the process above. In last round, who grabbed the most money in the red envelopes($100), who was called the "lucky man". For example: In round 1 of example testcase 2, five person grabed [44.1,24.4,8,2.4,21.1], so the first person (grabed 44.1) is the "lucky man". That is to say, the choice of the "lucky man" only depends on the input arrays and nothing more.

The "lucky man" needs to send the next red envelope with the same total amount($100) and the same pieces(5).

continue the example above:
Tom grab $21.5, he is the "lucky man"
he will send the next red envelope($100,5 pieces):
[22.3,55,11,8,4.7]
and so on...

For clear the rules, Please look at the following explanation about the lucky man and the game process:

The lucky man in first round should not need to elect,
he always be the first man(element 0). 
In addition to the first round, each round need elect a new lucky man.
In accordance with the maximum number of money that they grabed in this round.

The process of game is:

 +----->  lucky man send $100 
 |  --->  each man grab once,such as [22.3,55,11,8,4.7]  
 |  --->  elect a new lucky man from their grabed money, 
 |        such as 55 in [22.3,55,11,8,4.7], 2nd man will be the new lucky man
 +-----<<<start a new round  

The game continues until one of the money is less than $1000, or the end of a consensus.

Your task is to count their money at the end of the game.

Task

Complete function grabRedEnvelope() that accepts a argument grabInEachRound. It records the money of each time they grabed. You should return an array of their money at the end of the game. the number of result should round to 1 decimal place.

For the sake of simplicity, you can assume that the number of people always be 5, the initial amount always be 2000, the total amount of red envelope always be $100, the fisrt person(the index0 in the array) always be the first "lucky man" to send the first red envelope in round one and we don't need to know their names. If more than one person grabed the same number of the most money, please choose the person that has the small index.

Example

initial amount:
[2000,2000,2000,2000,2000]

grabRedEnvelope([[18.5,20,21.5,19,21],[22.3,55,11,8,4.7],[10,20,30,20,20]])

After round 1:  //1st person is "lucky man"
[2000-100+18.5,2000+20,2000+21.5,2000+19,2000+21]===
[1918.5,2020,2021.5,2019,2021]

After round 2:  //3rd person is "lucky man"
[1918.5+22.3,2020+55,2021.5-100+11,2019+8,2021+4.7]===
[1940.8,2075,1932.5,2027,2025.7]

After round 3:  //2nd person is "lucky man"
[1940.8+10,2075-100+20,1932.5+30,2027+20,2025.7+20]===
[1950.8,1995,1962.5,2047,2045.7]  //this is the result

an example of edge case:
grabInEachRound = [[44.1,24.4,8,2.4,21.1],[18.2,14.5,8.1,21.5,37.7],
[21.6,2.6,6.6,25.4,43.8],[44.2,19.6,10.2,9.2,16.8],[10.8,39.4,21,1.4,27.4],
[5.3,37.2,2.1,5.7,49.7],[11.3,22.9,21.5,19.9,24.4],
[15.3,41.5,15.9,2.9,24.4],[42.8,4.8,5,8.4,39],[34.9,2.2,31.6,5.7,25.6],
[45.7,7.5,19.6,9.8,17.4],[11.9,17.4,3.4,9.2,58.1],[29.8,23.4,18,9.4,19.4],
[13.8,4.8,33.8,17,30.6],[44.1,17.4,13.6,9.7,15.2],[6.6,9,38.8,8.9,36.7],
[2.4,23.9,7.2,16.5,50],[48.5,9.2,21.5,6.1,14.7],[9,34.5,27,9.3,20.2],
[34.2,16.7,3.3,1.1,44.7],[46.3,2.9,21,1.8,28],[20.9,34.8,8.6,1.1,34.6],
[36,18.4,21.6,6.5,17.5],[11.2,13,19.3,21,35.5],[31,4.7,11.8,9.6,42.9],
[49.1,4.4,1.8,20.7,24],[16.2,9.9,10.1,23.3,40.5],[50.4,10.1,12.5,12.1,14.9],
[42.2,22.9,9.3,11.5,14.1],[39.4,21.8,10.2,6,22.6],[10.7,6.1,23.8,19.6,39.8],
[31.9,7.6,15.9,22.6,22],[36.5,25,20.1,2.5,15.9],[49.7,25.1,5.6,2.2,17.4],
[48.7,11.9,4.6,1.4,33.4],[38.1,9.1,13.5,8.5,30.8],[44.6,6.8,7.2,4.2,37.2],
[43.2,10.2,17.8,1.4,27.4],[5.8,19.4,10.6,14.7,49.5],[18.3,1.8,11.2,18,50.7],
[31.4,28.9,13.1,2.9,23.7],[14.8,37.4,18.3,8.8,20.7],
[30.1,19.8,21.1,11.9,17.1],[7.6,43.3,18.1,12.1,18.9],
[48.7,14.9,10.7,8.6,17.1],[12.3,6.3,6.8,33,41.6]] 

grabRedEnvelope(grabInEachRound) 
should return [946.4, 2237, 2361.5, 2390.2, 2064.9]
instead of [909.6,2189.4,2460.8,2485.5,1954.7]
because at the round 38, the first person's money less than $1000
so the game is over in this round.
Puzzles
Games

Stats:

CreatedAug 22, 2016
PublishedAug 22, 2016
Warriors Trained85
Total Skips1
Total Code Submissions171
Total Times Completed28
JavaScript Completions28
Total Stars1
% of votes with a positive feedback rating93% of 14
Total "Very Satisfied" Votes12
Total "Somewhat Satisfied" Votes2
Total "Not Satisfied" Votes0
Total Rank Assessments5
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • kazk Avatar
  • ZED.CWT Avatar
Ad