Skip to content

Instantly share code, notes, and snippets.

@well1791
Last active April 3, 2024 10:25
Show Gist options
  • Save well1791/62256bc57770bd57274c4ebdb79b4a28 to your computer and use it in GitHub Desktop.
Save well1791/62256bc57770bd57274c4ebdb79b4a28 to your computer and use it in GitHub Desktop.
spiral operations
import unittest
###
# Utility functions
###
def fromIndex(i, arr):
try:
return arr[i]
except:
return None
def byDefault(default, value):
return default if value is None else value
def head(arr):
return fromIndex(0, arr)
def last(arr):
return fromIndex(len(arr) - 1, arr)
def init(arr):
return arr[:-1]
def tail(arr):
return arr[1:]
def take(n, arr):
return arr[:n]
def drop(n, arr):
return arr[n:]
def reverse(arr):
return list(reversed(arr))
###
# Task
###
def spiral(matrix):
'''
Given a matrix, returns a flatten list starting from top left position to
the right, then down, then left, then up, in spiral order.
Parameters:
matrix (List[List[int]]) = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
Returns:
result (List[int]) = [1, 2, 3, 6, 9, 8, 7, 4, 5]
'''
if not matrix:
return []
left_column = []
right_column = []
new_matrix = []
for row in init(tail(matrix)): # mid rows
left_column.append(head(row))
right_column.append(last(row))
new_matrix.append(init(tail(row)))
return byDefault([], head(matrix)) + \
reverse(right_column) + \
reverse(byDefault([], last(tail(matrix)))) + \
left_column + \
spiral(new_matrix)
def operands(arr):
'''
Returns the result of applying a list of operations (+ - *) to a list ex:
=> [1, 2, 3, 4, 5]
=> ((((1 + 2) - 3) * 4) + 5)
=> 5
Parameters:
arr (List[int]) = [1, 2, 3, 4, 5]
Returns:
result (int) = 5
'''
if len(arr) < 2:
return byDefault(0, head(arr))
adds = sum(take(2, arr))
subs = adds - byDefault(0, fromIndex(2, arr))
mult = subs * byDefault(1, fromIndex(3, arr))
return operands([mult] + drop(4, arr))
def spiral_operations(matrix):
'''
Given a matrix, returns the result of applying a list of operations (+ - *)
to its values, starting from top left position to the right (→), then
down (↓), then left (←), then up (↑), in spiral order, ex:
=> [
[7, 8, 9],
[4, 5, 6],
[1, 2, 3],
]
=> → 7, 8, 9 ↓ 6, 3 ← 2, 1 ↑ 4 → 5
=> (((((((((7 + 8) - 9) * 6) + 3) - 2) * 1) + 4) - 5))
=> 36
Parameters:
matrix (List[List[int]]) = [
[5, 4, 7],
[1, 2, 3],
[3, 2, 1],
]
Returns:
result (int) = 14
'''
return operands(spiral(matrix))
############
class TestSpiralOperations(unittest.TestCase):
def test_case_1(self):
data = [
[5,4,7],
[1,2,3],
[3,2,1],
]
self.assertEqual(spiral_operations(data), 14)
def test_case_2(self):
data = [
[1,2,3],
[1,2,3],
[1,2,3],
]
self.assertEqual(spiral_operations(data), 0)
def test_case_3(self):
data = [
[2,2,2],
[2,2,2],
[2,2,2],
]
self.assertEqual(spiral_operations(data), 8)
def test_case_4(self):
data = [
[7, 8, 9],
[4, 5, 6],
[1, 2, 3],
]
self.assertEqual(spiral_operations(data), 36)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment