From 852570688270afb455440d1a4fff5ef04c9943fc Mon Sep 17 00:00:00 2001 From: Muhammad Junaid <34795347+mkhalid1@users.noreply.github.com> Date: Tue, 2 Oct 2018 23:07:49 +0500 Subject: [PATCH] Updated label_queen_conflicts function Shortened it, finding conflicts separately and storing them in different variables has no use later in the notebook; so i believe this looks better. --- csp.ipynb | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/csp.ipynb b/csp.ipynb index fcf8b5867..411d6f55c 100644 --- a/csp.ipynb +++ b/csp.ipynb @@ -2821,19 +2821,13 @@ "def label_queen_conflicts(assignment,grid):\n", " ''' Mark grid with queens that are under conflict. '''\n", " for col, row in assignment.items(): # check each queen for conflict\n", - " row_conflicts = {temp_col:temp_row for temp_col,temp_row in assignment.items() \n", - " if temp_row == row and temp_col != col}\n", - " up_conflicts = {temp_col:temp_row for temp_col,temp_row in assignment.items() \n", - " if temp_row+temp_col == row+col and temp_col != col}\n", - " down_conflicts = {temp_col:temp_row for temp_col,temp_row in assignment.items() \n", - " if temp_row-temp_col == row-col and temp_col != col}\n", + " conflicts = {temp_col:temp_row for temp_col,temp_row in assignment.items() \n", + " if (temp_row == row and temp_col != col\n", + " or (temp_row+temp_col == row+col and temp_col != col)\n", + " or (temp_row-temp_col == row-col and temp_col != col)}\n", " \n", " # Place a 3 in positions where this is a conflict\n", - " for col, row in row_conflicts.items():\n", - " grid[col][row] = 3\n", - " for col, row in up_conflicts.items():\n", - " grid[col][row] = 3\n", - " for col, row in down_conflicts.items():\n", + " for col, row in conflicts.items():\n", " grid[col][row] = 3\n", "\n", " return grid\n",