Today, we’ll be looking at the following problem:
682. Baseball Game
Prompt
You are given a list of strings called operations and each string or operation adhere to a specific rule that you must configure in your algorithm. The rules are as follows:
- If operations[i] is an integer, record a new score of operations[i].
- If operations[i] is ‘+’, record a new score that is the sum of the previous 2 scores.
- If operations[i] is ‘D’, record a new score that is the double of the previous score.
- If operations[i] is ‘C’, invalidate or remove the previous or latest recorded score from the record.
Finally, return the sum of all the scores on the record after all the operations have been executed.
Another important detail to note is listed within the constraints where there will always be at least 2 previous scores on the record when operations[i] is ‘+’ and the same applies to when operations[i] is equal to ‘C’ and ‘D’ where there will always be at least 1 previous score on the record.
Therefore, you don’t need to account for the edge cases where you need to check whether if there are sufficient records when you execute the operations of ‘+’, ‘C’, or ‘D’.
Approach
The approach for this problem is quite straightforward as you would create a conditional statement accounting for each scenario listed above of what operations[i] can equate to. We’ll record all the scores in an array called record. We should make sure though that when we append the scores generated from running any conditional statements in our logic that they are being added to our record in the form of an integer because we will want to calculate a total sum using each of the elements (or integers) of the record array.
Lastly, one other detail to note is how we will check if operations[i] is an integer for the first conditional statement above. The way I decided to approach this in the code below is not the only method but one way of doing so. I decided to create a separate helper function that takes in a string as a parameter and within the function, utilizes a try & except block and checks if the parameter can be converted to an integer. If so, return true; otherwise, return false.
This helper function will allow us to utilize its boolean output in the parent function so we can check whether if operations[i] can be converted to an integer and only if so, we can append it to the result array.
def representsInt(self, string):
try:
int(string)
return True
except ValueError:
return False
Code
When tallying each score into a final sum that you’ll return at the end, you can use the built-in sum function and pass in your record array. I just used a for-loop here to be more explicit and display what’s occurring behind the scenes.
Thank you for reading!
In case if you haven’t yet joined my Discord server where I host daily sessions on data structures & algorithms for free, please join via the link shared below.
If you have any questions or comments, please don’t be afraid to connect with me on any of the platforms below or feel free to send me an email at cloudiosx@gmail.com