[PYTHON] Code wars kata starting from zero Sudoku, Nampre

At codewars kata you can learn programming and practical English at the same time. It is a genius site. If you have a github account, you can get started in 30 seconds If you are interested, start now start here

By the way, you can challenge in many languages other than python.

Today's code

The assignment is Sudoku (Sudoku), the name of the site is kata (type), the grade notation is kyu (class), this site seems to like Japan By the way, there seems to be a service called Kumite.

1. Sudoku Did I Finish my Sudoku? [5kyu]

Judge whether Sudoku is over on the Q input board

Sudoku rules It's a guy who arranges 1 to 9 in vertical, horizontal, 3 * 3 areas.

Example 1

Input: Board
[[1, 3, 2, 5, 7, 9, 4, 6, 8]
,[4, 9, 8, 2, 6, 1, 3, 7, 5]
,[7, 5, 6, 3, 8, 4, 2, 1, 9]
,[6, 4, 3, 1, 5, 8, 7, 9, 2]
,[5, 2, 1, 7, 9, 3, 8, 4, 6]
,[9, 8, 7, 4, 2, 6, 5, 3, 1]
,[2, 1, 4, 9, 3, 5, 6, 8, 7]
,[3, 6, 5, 8, 1, 7, 9, 2, 4]
,[8, 7, 9, 6, 4, 2, 1, 5, 3]]
Output:'Finished!'

agvz

Example 2

Input: Board
[[1, 3, 2, 5, 7, 9, 4, 6, 8]
,[4, 9, 8, 2, 6, 1, 3, 7, 5]
,[7, 5, 6, 3, 8, 4, 2, 1, 9]
,[6, 4, 3, 1, 5, 8, 7, 9, 2]
,[5, 2, 1, 7, 9, 3, 8, 4, 6]
,[9, 8, 7, 4, 2, 6, 5, 3, 1]
,[2, 1, 4, 9, 3, 5, 6, 8, 7]
,[3, 6, 5, 8, 1, 7, 9, 2, 4]
,[8, 7, 9, 6, 4, 2, 1, 3, 5]]
             #5 #3  #When viewed vertically 5,3 is missing
Output:'Try again!'

My Answer

import numpy as np
def done_or_not(board):
    sets = set([i for i in range(1,10)])
    board = np.array(board)
    if not [1  for j in range(9) if not set(board[j]) == sets or not set(board[:,j]) == sets] ==[]:
        return 'Try again!'    
    if not [1 for i in range(9) if not set(board[i//3*3:i//3*3+3,i%3*3:(i%3*3)+3].flatten()) == sets ] ==[]:
        return 'Try again!' 
    return 'Finished!'

As sets = {sets of 1-9} Are the columns and columns in this set? Is each area this set? It feels like checking each I feel like the code has become Codewars-like ... I don't know if it's good or bad because it's not readable

Best Answer

import numpy as np
def done_or_not(aboard): #board[i][j]
  board = np.array(aboard)

  rows = [board[i,:] for i in range(9)]
  cols = [board[:,j] for j in range(9)]
  sqrs = [board[i:i+3,j:j+3].flatten() for i in [0,3,6] for j in [0,3,6]]
  
  for view in np.vstack((rows,cols,sqrs)):
      if len(np.unique(view)) != 9:
          return 'Try again!'
  
  return 'Finished!'

What is this ... It's really easy to see Arrange vertical and horizontal squares in a two-dimensional array with np.vstack () You see how many kinds of elements there are in np.unique () I've tried to do the same with len (set ()), but there's such a useful feature I will do my best to write a code that is straightforward and easy to read, rather than being cute.

Afterword

I started studying CSS and HTML

Recommended Posts

Code wars kata starting from zero Sudoku, Nampre
Code wars kata starting from zero
Let Code Day58 Starting from Zero "20. Valid Parentheses"
Let Code Day16 Starting from Zero "344. Reverse String"
Let Code Day49 starting from zero "1323. Maximum 69 Number"
Let Code Day89 "62. Unique Paths" Starting from Zero
Let Code Day 55 "22. Generate Parentheses" Starting from Zero
Let Code Table of Contents Starting from Zero
Let Code Day18 starting from zero "53. Maximum Subarray"
Let Code Day 13 "338. Counting Bits" Starting from Zero
Let Code Day71 Starting from Zero "1496. Path Crossing"
Let Code Day 61 "7. Reverse Integer" starting from zero
Let Code Day 82 "392. Is Subsequence" Starting from Zero
Let Code Day51 Starting from Zero "647. Palindromic Substrings"
Let Code Day 50 "739. Daily Temperatures" Starting from Zero
Let Code Day 15 "283. Move Zeroes" starting from zero
Let Code Day14 starting from zero "136. Single Number"
[Python] Django Source Code Reading View Starting from Zero ①
Let Code Day 43 "5. Longest Palindromic Substring" Starting from Zero
Let Code Day74 starting from zero "12. Integer to Roman"
Let Code Day 42 "2. Add Two Numbers" Starting from Zero
Let Code Day57 Starting from Zero "35. Search Insert Position"
Let Code Day47 Starting from Zero "14. Longest Common Prefix"
Let Code Day78 Starting from Zero "206. Reverse Linked List"
ChIP-seq analysis starting from zero
Let Code Day 44 "543. Diameter of Binary Tree" starting from zero
Let Code Day 64 starting from zero "287. Find the Duplicate Number"
Let Code Day 84 starting from zero "142. Linked List Cycle II"
Let Code Day24 Starting from Zero "21. Merge Two Sorted Lists"
Let Code Day12 Starting from Zero "617. Merge Two Binary Trees"
Let Code Day2 Starting from Zero "1108. Defanging an IP Address"
Let Code Day70 Starting from Zero "295. Find Median from Data Stream"
Let Code Day81 "347. Top K Frequent Elements" Starting from Zero
Let Code Day48 Starting from Zero "26. Remove Duplicates from Sorted Array"
Let Code Day87 Starting from Zero "1512. Number of Good Pairs"
Let Code Day67 Starting from Zero "1486. XOR Operation in an Array"
Let Code Day56 Starting from Zero "5453. Running Sum of 1d Array"
Let Code Day86 Starting from Zero "33. Search in Rotated Sorted Array"
Let Code Day92 Starting from Zero "4. Median of Two Sorted Arrays"
Let Code Day5 starting from zero "1266. Minimum Time Visiting All Points"
Let Code Day 35 "160. Intersection of Two Linked Lists" Starting from Zero
Let Code Day83 Starting from Zero "102. Binary Tree Level Order Traversal"
Let Code Day 40 Starting from Zero "114. Flatten Binary Tree to Linked List"
Let Code Day 91 "153. Find Minimum in Rotated Sorted Array" starting from zero
Let Code Day 29 "46. Permutations" starting from scratch
Let Code Day59 starting from zero "1221. Split a String in Balanced Strings"
Let Code Day 11 Starting from Zero "1315. Sum of Nodes with Even-Valued Grandparent"
Let Code Day6 Starting from Zero "1342. Number of Steps to Reduce a Number to Zero"
Let Code Day73 Starting from Zero "1491. Average Salary Excluding the Minimum and Maximum Salary"
Let Code Day1 Starting from Zero "1389. Create Target Array in the Given Order"
Let Code Day10 Starting from Zero "1431. Kids With the Greatest Number of Candies"
Let Code Day21 Starting from Zero "448. Find All Numbers Disappeared in an Array"
Let Code Day 37 Starting from Zero "105. Construct Binary Tree from Preorder and Inorder Traversal"
Let Code Day 19 Starting from Zero "121. Best Time to Buy and Sell Stock"
Let Code Day 27 "101. Symmetric Tree" starting from scratch
Python explosive environment construction starting from zero (Mac)
Let Code Day 41 "394. Decode String" starting from scratch
Let Code Day 25 "70. Climbing Stairs" starting from scratch
Let Code Day69 starting from scratch "279. Perfect Squares"
Let Code Day 34 starting from scratch "118. Pascal's Triangle"
Let Code Day85 starting from scratch "6. ZigZag Conversion"