patternpythonModerate
Python code to check if an array has a sequence (1,3,4)
Viewed 0 times
arraysequencehaspythoncodecheck
Problem
I recently applied for a job as a Python coder but was rejected.
This was the problem:
Write a python code to check if an array has a sequence (1,3,4)
Assuming they were looking for expert Python programmers, what could I have done better?
This was the problem:
Write a python code to check if an array has a sequence (1,3,4)
Assuming they were looking for expert Python programmers, what could I have done better?
# Tested with Python 2.7
import unittest
# Runtime: O(n)
def doesSeqAppear(int_arr):
#check if input is a list
if not isinstance(int_arr, list):
raise TypeError("Input shall be of type array.")
# check all elements are of type int
if not all(isinstance(item, int) for item in int_arr) :
raise ValueError("All elements in array shall be of type int.")
arr_len = len(int_arr)
if arr_len < 3:
return False
# Loop through elements
for i in range(arr_len-2):
if int_arr[i] == 1 and \
int_arr[i+1] == 3 and \
int_arr[i+2] == 4 :
return True
return False
class TestMethodDoesSeqAppear(unittest.TestCase):
def test_only_single_seq(self):
#Single time
assert doesSeqAppear([1,3,4]) == True
def test_multiple_seq(self):
#multiple
assert doesSeqAppear([2,2,1,3,4,2,1,3,4]) == True
def test_neg_seq(self):
#multiple
assert doesSeqAppear([9,-1,1,3,4,-4,4]) == True
def test_only_empty_seq(self):
#empty
assert doesSeqAppear([]) == False
def test_only_single_elem_seq(self):
#Single element
assert doesSeqAppear([1]) == False
def test_input_is_none(self):
self.assertRaises(TypeError, doesSeqAppear, None)
def test_raises_type_error(self):
self.assertRaises(TypeError, doesSeqAppear, "string")
def test_raises_value_error(self):
self.assertRaises(ValueError, doesSeqAppear, [1,2,'a', 'b'])
if __name__ == '__main__':
unittest.main()
#Solution
By PEP 8,
Your
Your
Checking
After cutting all that excess, you should drop the
doesSeqAppear should be does_seq_appear. You used the right naming convention for your unit tests, though. Personally, I would prefer def contains_seq(arr, seq=[1, 3, 4]).Your
arr_len < 3 test is superfluous and should therefore be eliminated. Don't write a special case when the regular case works correctly and just as quickly.Your
all(isinstance(item, int) for item in int_arr) check was not specified in the problem, and is therefore harmful. The question does not say that doesSeqAppear([3.1, 1, 3, 4]) should return False, nor does it say that it should fail with an exception. In fact, by my interpretation, it does contain the magic sequence and should therefore return True. In any case, you have wasted a complete iteration of the list just to perform a check that wasn't asked for.Checking
isinstance(int_arr, list) is un-Pythonic, since duck-typing is the norm in Python. In any case, the code would likely fail naturally if it is not a list.After cutting all that excess, you should drop the
# Loop through elements comment as well.Context
StackExchange Code Review Q#149867, answer score: 10
Revisions (0)
No revisions yet.