Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Last active June 14, 2020 02:09
Show Gist options
  • Save ZechCodes/a305f974a3b076875b84bb16e357f8a5 to your computer and use it in GitHub Desktop.
Save ZechCodes/a305f974a3b076875b84bb16e357f8a5 to your computer and use it in GitHub Desktop.

Challenge #27 - Shift & Multiple Validators

For this task, you will write two validators.

  • Shift Validator: Determines if each element is translated (added or subtracted) by a constant.
  • Multiple Validator: Determines if each element is multiplied by a positive or negative constant.

A few examples to illustrate these respective functions:

Examples

is_shifted([1, 2, 3], [2, 3, 4]) ➞ True
# Each element is shifted +1

is_shifted([1, 2, 3], [-9, -8, -7]) ➞ True
# Each element is shifted -10

is_multiplied([1, 2, 3], [10, 20, 30]) ➞ True
# Each element is multiplied by 10

is_multiplied([1, 2, 3], [-0.5, -1, -1.5]) ➞ True
# Each element is multiplied by -1/2

is_multiplied([1, 2, 3], [0, 0, 0]) ➞ True
# Each element is multiplied by 0

Notes

  • Keep in mind one special case: if the second list is a list of only zeroes, then the first list can be anything (the multiplier will be 0).
import unittest
from typing import List
def is_shifted(list_a: List[int], list_b: List[int]) -> bool:
return False # Replace with your code!
def is_multiplied(list_a: List[int], list_b: List[int]) -> bool:
return False # Replace with your code!
class TestValidators(unittest.TestCase):
def test_1(self):
self.assertEqual(is_shifted([1, 2, 3], [2, 3, 4]), True)
def test_2(self):
self.assertEqual(is_shifted([1, 2, 3], [-9, -8, -7]), True)
def test_3(self):
self.assertEqual(is_multiplied([1, 2, 3], [10, 20, 30]), True)
def test_4(self):
self.assertEqual(is_multiplied([1, 2, 3], [-0.5, -1, -1.5]), True )
def test_5(self):
self.assertEqual(is_multiplied([1, 2, 3], [0, 0, 0]), True )
def test_6(self):
self.assertEqual(is_shifted([1, 2, 3], [2, 3, 5]), False)
def test_7(self):
self.assertEqual(is_shifted([1, 2, 3], [-9, -1, -7]), False)
def test_8(self):
self.assertEqual(is_multiplied([1, 2, 3], [10, 20, 29]), False)
def test_9(self):
self.assertEqual(is_multiplied([1, 2, 3], [-0.5, -1, -2]), False)
def test_10(self):
self.assertEqual(is_multiplied([1, 2, 3], [0, 0, 1]), False)
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment