19
2
Several months ago I had this question as a pre-screening puzzle for an interview. Recently when thinking about blog material, it popped in my head as a good example to use for solving a problem functionally. I'll post my solution to this as soon as I'm done writing my blog post.
NOTE: This question was asked on StackOverflow a year ago, and was downvoted after a few (incorrect) answers. I assume it was downvoted for being an obvious interview or homework question. Our answers here should be code golfed deep enough for someone not to think about using them!
In a race, you bet using the following strategy. Whenever you lose a bet, you double the value of the bet for the next round. Whenever you win, the bet for the next round will be one dollar. You start the round by betting one dollar.
For example, if you start with 20 dollars, and you win the bet in the first round, lose the bet in the next two rounds and then win the bet in the fourth round, you will end up with 20+1-1-2+4 = 22 dollars.
You are expected to complete the function, g
, which takes two arguments:
- The first argument is an integer
a
which is the initial money we amount we have when we start the betting. - The second argument is a string
r
. The ith character of outcome will be either 'W' (win) or 'L' (lose), denoting the result of the ith round.
Your function should return the amount of money you will have after all the rounds are played.
If at some point you don't have enough money in your account to cover the value of the bet, you must stop and return the sum you have at that point.
Sample run
1st round - Loss: 15-1 = 14
2nd round - Loss: 14-2 = 12 (Bet doubles)
3rd round - Loss: 12-4 = 8
4th round - Win: 8 + 8 = 16
5th round - Loss:16-1 = 15 (Since the previous bet was a win, this bet has a value of 1 dollar)
6th round - Loss: 15-2 = 13
7th round - Loss: 13-4 = 9
8th round - Loss: 9-8 = 1
The function returns 1
in this case
The winner is determined by least number of characters INSIDE of the implied function definition. Cooperate by language if desired. I know mine can be improved!
All questions on this site must have an objective winning criterion. You didn't mention what determines the winner of the challenge.
– Howard – 2014-04-24T05:37:09.2371Moreover, you state that Your function should return the amount of money you will have after all the rounds are played. although you show a much more detailed information in the Expected output section. Which is the desired behavior of the function? – Howard – 2014-04-24T05:38:05.397
Also, if every tag you are using is one that you created just for the question, something is wrong. – Justin – 2014-04-24T05:59:43.753
1Judging from "Our answers here should be code golfed deep enough for someone not to think about using them!", I think you wanted a [code-golf], so I tagged it as such. Also, I adjusted the "Example Output" to be a "Sample Run" to better match what it seems you wanted. – Justin – 2014-04-24T06:08:39.907
@quincunx sorry, I've never posted here but interestingly posted many of the original questions that migrated here when it was created. In a way, I'm one of the reasons this stackexchange was made. I thought it was all code golf, and my laptop battery was dying so I was in a hurry to finish up. Sorry. Long night. – TheSoftwareJedi – 2014-04-24T06:27:37.357
No problem. If you want a better reception, you can send your challenges through the sandbox first.
– Justin – 2014-04-24T06:29:01.560What should happen if the function receives invalid input, e.g.,
f(-42,"LOL")
? Can we assume that the initial amount of money will be positive? – Dennis – 2014-04-24T19:08:39.147If after successive losses, I have
20
as an amount, and the value of the bet is32
. Now I win a bet. So, according to "If at some point you don't have enough money in your account to cover the value of the bet, you must stop and return the sum you have at that point", should I stop and return20
or add up32
(due to the win) to my account and continue with more losses/wins ? – Gaurang Tandon – 2014-04-25T04:25:01.787@GaurangTandon You cannot play a round if you cannot afford to lose. If you have 20 and the bet is 32, you return 20. – TheSoftwareJedi – 2014-04-25T11:59:57.350
What you should really output, given a seed, is how many rounds it takes you to lose all your money =P. (i.e. Martingale betting doesn't work in the long run) – Claudiu – 2014-04-28T22:58:19.623
@TheSoftwareJedi so in the case of your sample output, the string parameter was "LLLWLLLL" correct? – WallyWest – 2014-06-05T23:35:18.527
@wallywest correct – TheSoftwareJedi – 2014-06-06T15:09:21.030