682. Baseball Game


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:

  1. If operations[i] is an integer, record a new score of operations[i].
  2. If operations[i] is ‘+’, record a new score that is the sum of the previous 2 scores.
  3. If operations[i] is ‘D’, record a new score that is the double of the previous score.
  4. If operations[i] is ‘C’, invalidate or remove the previous or latest recorded score from the record.


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.

def representsInt(self, string):
return True
except ValueError:
return False


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.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
John Kim

John Kim


iOS Developer | Full Stack Developer | Software Engineer | LinkedIn: john-kim-developer | GitHub: cloudiosx | Portfolio: cloudiosx.com