Skip to content

Instantly share code, notes, and snippets.

@tyler-austin
Last active January 9, 2018 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tyler-austin/b28e54d3ec6ef3e524bb184b76c8323a to your computer and use it in GitHub Desktop.
Save tyler-austin/b28e54d3ec6ef3e524bb184b76c8323a to your computer and use it in GitHub Desktop.
Code Fights - areSimilar

Two arrays are called similar if one can be obtained from another by swapping at most one pair of elements in one of the arrays.

Given two arrays a and b, check whether they are similar.

Example

For a = [1, 2, 3] and b = [1, 2, 3], the output should be
areSimilar(a, b) = true.

The arrays are equal, no need to swap any elements.

For a = [1, 2, 3] and b = [2, 1, 3], the output should be
areSimilar(a, b) = true.

We can obtain b from a by swapping 2 and 1 in b.

For a = [1, 2, 2] and b = [2, 1, 1], the output should be
areSimilar(a, b) = false.

Any swap of any two elements either in a or in b won't make a and b equal.

Input/Output

  • [time limit] 4000ms (py3)

  • [input] array.integer a

    • Array of integers.

    Guaranteed constraints:

    3 ≤ a.length ≤ 105
    1 ≤ a[i] ≤ 1000
    
  • [input] array.integer b

    • Array of integers of the same length as a.

    Guaranteed constraints:

    b.length = a.length
    1 ≤ a[i] ≤ 1000
    
  • [output] boolean

    • true if a and b are similar, false otherwise.
from typing import List
from copy import deepcopy
class AreSimilar:
@classmethod
def are_similar(cls, a: List[int], b: List[int]):
if a == b:
return True
elif cls._single_swap(a, b):
return True
else:
return False
@classmethod
def _same_digits(cls, a: List[int], b: List[int]):
x, y = deepcopy(a), deepcopy(b)
x.sort()
y.sort()
return x == y
@classmethod
def _single_swap(cls, a: List[int], b: List[int]):
if not cls._same_digits(a, b):
return False
differences = 0
for i in range(len(a)):
if a[i] != b[i]:
differences += 1
if differences > 2:
break
return differences <= 2
from copy import deepcopy
def areSimilar(a: list, b: list):
if a == b:
return True
elif _single_swap(a, b):
return True
else:
return False
def _same_digits(a: list, b: list):
x, y = deepcopy(a), deepcopy(b)
x.sort()
y.sort()
return x == y
def _single_swap(a: list, b: list):
if not _same_digits(a, b):
return False
differences = 0
for i in range(len(a)):
if a[i] != b[i]:
differences += 1
if differences > 2:
break
return differences <= 2
import unittest
from are_similar import AreSimilar
class TestAreSimilar(unittest.TestCase):
def test_1(self):
a = [1, 2, 3]
b = [1, 2, 3]
result = AreSimilar.are_similar(a, b)
self.assertTrue(result)
def test_2(self):
a = [1, 2, 3]
b = [2, 1, 3]
result = AreSimilar.are_similar(a, b)
self.assertTrue(result)
def test_3(self):
a = [1, 2, 2]
b = [2, 1, 1]
result = AreSimilar.are_similar(a, b)
self.assertFalse(result)
def test_4(self):
a = [1, 1, 4]
b = [1, 2, 3]
result = AreSimilar.are_similar(a, b)
self.assertFalse(result)
def test_5(self):
a = [1, 2, 3]
b = [1, 10, 2]
result = AreSimilar.are_similar(a, b)
self.assertFalse(result)
def test_6(self):
a = [2, 3, 1]
b = [1, 3, 2]
result = AreSimilar.are_similar(a, b)
self.assertTrue(result)
def test_7(self):
a = [2, 3, 9]
b = [10, 3, 2]
result = AreSimilar.are_similar(a, b)
self.assertFalse(result)
def test_8(self):
a = [4, 6, 3]
b = [3, 4, 6]
result = AreSimilar.are_similar(a, b)
self.assertFalse(result)
def test_9(self):
a = [832, 998, 148, 570, 533, 561, 894, 147, 455, 279]
b = [832, 998, 148, 570, 533, 561, 455, 147, 894, 279]
result = AreSimilar.are_similar(a, b)
self.assertTrue(result)
def test_10(self):
a = [832, 998, 148, 570, 533, 561, 894, 147, 455, 279]
b = [832, 570, 148, 998, 533, 561, 455, 147, 894, 279]
result = AreSimilar.are_similar(a, b)
self.assertFalse(result)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment