Automatic commit performed through alias...

This commit is contained in:
Shaun Setlock
2020-07-22 20:50:32 -04:00
parent dfd2c44405
commit 45f182c3a7

View File

@@ -77,9 +77,37 @@
"metadata": {},
"outputs": [],
"source": [
"def get_c(a,b):\n",
" c = math.sqrt(a*a+b*b)\n",
" return c"
"def check_horizontal(table,solution):\n",
" '''\n",
" Args:\n",
" table: list of lists representing the input table\n",
" solution: a dictionary storing the product, and three tuples of cell coordinates \n",
" '''\n",
" # Let's scan the horizontals first.\n",
" # Acceptable cells would have a root position (on the LHS) which may reside anywhere in the table,\n",
" # except for the last three columns.\n",
"\n",
" # create a list of tuples to store cell coordinates while generating the products\n",
" cells=[(0,0),(0,0),(0,0),(0,0)]\n",
"\n",
" # loop over row dimension\n",
" for row in range(len(table)):\n",
" # loop over column dimension\n",
" for column in range(len(table[row])-4):\n",
" # calculate product\n",
" product = 1\n",
" for i in range(4):\n",
" product *= table[row][column+i]\n",
" cells[i]=(row,column+i)\n",
" #print(\"product=\",product)\n",
" if product>solution['product']:\n",
" solution['product']=product\n",
" solution['cella']=cells[0]\n",
" solution['cellb']=cells[1]\n",
" solution['cellc']=cells[2]\n",
" solution['celld']=cells[3]\n",
" \n",
" return solution"
]
},
{
@@ -88,20 +116,139 @@
"metadata": {},
"outputs": [],
"source": [
"def check_constraint(a,b,c,limit):\n",
" return (a+b+c)==limit"
"def check_vertical(table,solution):\n",
" '''\n",
" Args:\n",
" table: list of lists representing the input table\n",
" solution: a dictionary storing the product, and three tuples of cell coordinates \n",
" '''\n",
" # Let's scan the verticals next.\n",
" # Acceptable cells would have a root position (at the top of column) which may reside anywhere in the table,\n",
" # except for the last three rows.\n",
"\n",
" # create a list of tuples to store cell coordinates while generating the products\n",
" cells=[(0,0),(0,0),(0,0),(0,0)]\n",
"\n",
" # loop over row dimension\n",
" count = 0\n",
" for row in range(len(table)-4):\n",
" # loop over column dimension\n",
" for column in range(len(table[row])):\n",
" # convert data type from string to int\n",
" product = 1\n",
" for i in range(4):\n",
" product *= table[row+i][column]\n",
" cells[i]=(row,column+i)\n",
" #print(\"product=\",product)\n",
" count+=1\n",
" #print(\"count=\",count)\n",
" if product>solution['product']:\n",
" solution['product']=product\n",
" solution['cella']=cells[0]\n",
" solution['cellb']=cells[1]\n",
" solution['cellc']=cells[2]\n",
" solution['celld']=cells[3]\n",
" \n",
" return solution"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def check_upwards_diagonals(table,solution):\n",
" '''\n",
" Args:\n",
" table: list of lists representing the input table\n",
" solution: a dictionary storing the product, and three tuples of cell coordinates \n",
" '''\n",
" # Let's scan the upward (left to right) diagonals next.\n",
" # Acceptable cells would have a root position (at the bottom-left of the diagonal) which may reside anywhere in the table,\n",
" # except for the first three rows, and the last three columns.\n",
"\n",
" # create a list of tuples to store cell coordinates while generating the products\n",
" cells=[(0,0),(0,0),(0,0),(0,0)]\n",
"\n",
" # loop over row dimension\n",
" count = 0\n",
" for row in range(len(table)-3):\n",
" # loop over column dimension\n",
" #print(\"row=\",row)\n",
" for column in range(len(table[row])-3):\n",
" # convert data type from string to int\n",
" product = 1\n",
" for i in range(4):\n",
" product *= table[row+3-i][column+i]\n",
" cells[i]=(row+3-i,column+i)\n",
" #print(\"cell=[\",row+3-i,\"][\",column+i,\"]\")\n",
" #print(\"product=\",product)\n",
" count += 1\n",
" if product>solution['product']:\n",
" solution['product']=product\n",
" solution['cella']=cells[0]\n",
" solution['cellb']=cells[1]\n",
" solution['cellc']=cells[2]\n",
" solution['celld']=cells[3]\n",
" \n",
" return solution"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def check_downwards_diagonals(table,solution):\n",
" '''\n",
" Args:\n",
" table: list of lists representing the input table\n",
" solution: a dictionary storing the product, and three tuples of cell coordinates \n",
" '''\n",
" # Let's scan the downward (left to right) diagonals next.\n",
" # Acceptable cells would have a root position (at the top-left of the diagonal) which may reside anywhere in the table,\n",
" # except for the last three rows, and the last three columns.\n",
"\n",
" # create a list of tuples to store cell coordinates while generating the products\n",
" cells=[(0,0),(0,0),(0,0),(0,0)]\n",
"\n",
" # loop over row dimension\n",
" count = 0\n",
" for row in range(len(table)-3):\n",
" # loop over column dimension\n",
" #print(\"row=\",row)\n",
" for column in range(len(table[row])-3):\n",
" # convert data type from string to int\n",
" product = 1\n",
" for i in range(4):\n",
" product *= table[row+i][column+i]\n",
" cells[i]=(row+i,column+i)\n",
" #print(\"cell=[\",row+i,\"][\",column+i,\"]\")\n",
" #print(\"product=\",product)\n",
" count += 1\n",
" #print(\"count=\",count)\n",
" if product>solution['product']:\n",
" solution['product']=product\n",
" solution['cella']=cells[0]\n",
" solution['cellb']=cells[1]\n",
" solution['cellc']=cells[2]\n",
" solution['celld']=cells[3]\n",
" \n",
" return solution"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Brute Force!"
"### Import the data table above. Let's be lazy and use the nice multi-cursor feature of the code editor."
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"metadata": {
"tags": []
},
@@ -133,9 +280,49 @@
" ]\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Firstly, this data input creates a list-of-lists which have cells of the ```string``` data type."
]
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 7,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# loop over row dimension\n",
"for row in range(len(table)):\n",
" # loop over column dimension\n",
" for column in range(len(table[row])):\n",
" # convert data type from string to int\n",
" table[row][column] = int(table[row][column])\n",
"\n",
"# create a dictionary that stores information about the solution\n",
"solution = {\n",
" 'product':1,\n",
" 'cella':(0,0),\n",
" 'cellb':(0,0),\n",
" 'cellc':(0,0),\n",
" 'celld':(0,0)\n",
"}\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### We know brute force will work...\n",
"### Can we can scan through this list-of-lists, following the accepted pattern, and store the largest product?"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"tags": []
},
@@ -143,17 +330,21 @@
{
"output_type": "stream",
"name": "stdout",
"text": "<class 'list'>\n49\n<class 'numpy.ndarray'>\n[0, 8]\n"
"text": "{'product': 48477312, 'cella': (8, 10), 'cellb': (8, 11), 'cellc': (8, 12), 'celld': (8, 13)}\n{'product': 51267216, 'cella': (6, 15), 'cellb': (6, 16), 'cellc': (6, 17), 'celld': (6, 18)}\n{'product': 70600674, 'cella': (15, 3), 'cellb': (14, 4), 'cellc': (13, 5), 'celld': (12, 6)}\n{'product': 70600674, 'cella': (15, 3), 'cellb': (14, 4), 'cellc': (13, 5), 'celld': (12, 6)}\n"
}
],
"source": [
"print(type(table))\n",
"solution = check_horizontal(table,solution)\n",
"print(solution)\n",
"\n",
"print(table[1][1])\n",
"solution = check_vertical(table,solution)\n",
"print(solution)\n",
"\n",
"array_table = numpy.array(table)\n",
"solution = check_upwards_diagonals(table,solution)\n",
"print(solution)\n",
"\n",
"print(type(array_table))\n"
"solution = check_downwards_diagonals(table,solution)\n",
"print(solution)"
]
},
{