Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Created January 13, 2021 02:15
Show Gist options
  • Select an option

  • Save ZechCodes/f9c2118587393b5d92c28c325047e648 to your computer and use it in GitHub Desktop.

Select an option

Save ZechCodes/f9c2118587393b5d92c28c325047e648 to your computer and use it in GitHub Desktop.
Challenge 164 - Unusual Subtraction

Challenge 164 - Unusual Subtraction

Create a function that subtracts 1 from n (unless it ends in 0) k number of times. If n ends in 0, remove the 0.

To illustrate

n = 22
k = 3

This means our number is 22 and we have to repeat the algorithm three times. 22 does not end in 0 so we continue subtracting 1.

22 - 1 = 21
k = 1
21 - 1 = 20
k = 2

Now 20 ends in 0, so we can remove it. We get 2; k = 3. The algorithm ends there because we applied it three times.

N:  K:
22
21  1
20  2
2   3

Examples

unusual_subtraction(540, 5) ➞ 50

unusual_subtraction(1000000000, 9) ➞ 1

unusual_subtraction(42023110, 10) ➞ 4201
import unittest
def unusual_subtraction(n: int, k: int) -> int:
return 0 # Put your code here!!!
class Test(unittest.TestCase):
def test_1(self):
self.assertEqual(unusual_subtraction(78, 9), 7)
def test_2(self):
self.assertEqual(unusual_subtraction(540, 5), 50)
def test_3(self):
self.assertEqual(unusual_subtraction(1000000000, 9), 1)
def test_4(self):
self.assertEqual(unusual_subtraction(420, 4), 4)
def test_5(self):
self.assertEqual(unusual_subtraction(42023110, 10), 4201)
def test_6(self):
self.assertEqual(unusual_subtraction(88888888, 50), 883)
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment