Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Created June 15, 2020 02:06
Show Gist options
  • Save ZechCodes/c5755aa76a39e45b2627f39e2aa90e9f to your computer and use it in GitHub Desktop.
Save ZechCodes/c5755aa76a39e45b2627f39e2aa90e9f to your computer and use it in GitHub Desktop.

Challenge #28 - Growing & Shrinking Potions

There are two types of potions:

Growing potion: "A" Shrinking potion: "B"

  • If "A" immediately follows a digit, add 1 to the proceeding number.
  • If "B" immediately follows a digit, subtract 1 from the proceeding number.

Create a function that returns a string according to these rules, removing the potions once they've been consumed.

Examples

apply_potions("3A78B51") ➞ "47751"
# 3 grows to 4, 78 shrinks to 77

apply_potions("9999B") ➞ "9998"

apply_potions("9A123") ➞ "10123"

apply_potions("567") ➞ "567"

Notes

  • Numbers that are not followed by a potion on their immediate right should be left alone.
  • A number will always either be followed by zero or exactly 1 potion.
import unittest
def apply_potions(potions: str) -> str:
return "" # Replace with your own code!!!
class TestPotions(unittest.TestCase):
def test_1(self):
self.assertEqual(apply_potions("567"), "567")
def test_2(self):
self.assertEqual(apply_potions("3A78B51"), "47751")
def test_3(self):
self.assertEqual(apply_potions("9999B"), "9998")
def test_4(self):
self.assertEqual(apply_potions("9A123"), "10123")
def test_5(self):
self.assertEqual(apply_potions("1A2A3A4A"), "2345")
def test_6(self):
self.assertEqual(apply_potions("9B8B7B6A"), "8767")
def test_7(self):
self.assertEqual(apply_potions("19A10B99A1000B"), "209100999")
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment