Skip to content

Instantly share code, notes, and snippets.

@julianbonilla
Last active September 16, 2019 17:53
Show Gist options
  • Save julianbonilla/6d1aa091c0e1748c935f2903ed40d89b to your computer and use it in GitHub Desktop.
Save julianbonilla/6d1aa091c0e1748c935f2903ed40d89b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a function that takes in a (x,y) coordinate and a grid and returns the sum \n",
"of all the neighbors. A neighbor is defined as up/down/left/right/diagonal."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def generate_grid(rows, cols):\n",
" return [[random.randint(0,9) for i in range(rows)] for j in range(cols)]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 4, 6, 1, 4]\n",
"[1, 6, 4, 2, 8]\n",
"[6, 9, 1, 3, 8]\n",
"[6, 2, 8, 1, 8]\n",
"[8, 7, 9, 9, 7]\n"
]
}
],
"source": [
"my_grid = generate_grid(5, 5)\n",
"for row in my_grid:\n",
" print(row)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solution"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def sum_neighbors(coordinate, grid):\n",
" # A neighbor is defined as up/down/left/right/diagonals\n",
" neighbors = [(0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]\n",
" return sum([find_neighbor(coordinate, grid, neighbor) for neighbor in neighbors])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def find_neighbor(coordinate, grid, neighbor):\n",
" x, y = coordinate[0] + neighbor[0], coordinate[1] + neighbor[1] \n",
" # Don't allow negative indices\n",
" if x < 0 or y < 0:\n",
" return 0\n",
" \n",
" try:\n",
" return grid[x][y]\n",
" except:\n",
" return 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tests"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"grid = [\n",
" [0, 3, 6, 8, 1],\n",
" [2, 7, 5, 0, 3],\n",
" [1, 0, 3, 4, 9],\n",
" [7, 3, 8, 4, 5],\n",
" [2, 3, 9, 5, 7]\n",
"]\n",
"\n",
"test_cases = [\n",
" {'coordinate': (0,0), 'expected': 12},\n",
" {'coordinate': (1,1), 'expected': 20},\n",
" {'coordinate': (2,2), 'expected': 31},\n",
" {'coordinate': (3,3), 'expected': 50},\n",
" {'coordinate': (4,4), 'expected': 14}\n",
"]\n",
"\n",
"for case in test_cases:\n",
" actual = sum_neighbors(case['coordinate'], grid)\n",
" expected = case['expected']\n",
" assert actual == expected, f\"Actual: {actual} / Expected: {expected}\""
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment