+ 2

How do I DRY up this code?

https://code.sololearn.com/clq489rPgJJJ/?ref=app I brute forced it quite easily, but now I have 3-4 lines' worth of code checking rows, columns and diagonals. It works but it's highly inefficient.

14th Oct 2019, 1:44 PM
BTD Master
BTD Master - avatar
1 Answer
+ 2
A few thoughts from the small to the big: - `not(A) and not(B) and not(C)` can be rearranged to `not(A or B or C)` which is technically shorter. It's called DeMorgan's law! So it turns out that your checks on line 18 and 54 are the exact same but negated. - You could offload those checks into a function: def game_over(board): return (board[0] == board[3] == ...) or (...) ... And then you don't have to repeat yourself twice: while(not game_over(board)): if(game_over(board) and not cheat): - The ideal way to DRY up your code is to write seperate functions for checking a single row/column/diagonal. It will probably make your code *longer* but DRYer for sure. Think about how you would solve a 100x100 tic tac toe puzzle!
14th Oct 2019, 2:24 PM
Schindlabua - avatar