Python Forum
Help needed for Understanding this code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help needed for Understanding this code
#1
I saw this code for solving sudoku using python on the internet, and I was reading it. I am a beginner python-er, so I don't quite understand some of these lines. If someone could please write an explanation for each line that would be great! Thanks!

Code:

import sys

def same_row(i,j): return (i/9 == j/9) # Can someone explain how this math 
                                      #proves that they are in the same?
def same_col(i,j): return (i-j) % 9 == 0
def same_block(i,j): return (i/27 == j/27 and i%9/3 == j%9/3)

def r(a):
  i = a.find('0')
  if i == -1:
    sys.exit(a)

  excluded_numbers = set()
  for j in range(81):
    if same_row(i,j) or same_col(i,j) or same_block(i,j):
      excluded_numbers.add(a[j])

  for m in '123456789':
    if m not in excluded_numbers:
      r(a[:i]+m+a[i+1:]) # I really need help with this line which I don't get at all.

if __name__ == '__main__':
  if len(sys.argv) == 2 and len(sys.argv[1]) == 81:
    r(sys.argv[1])# I also don't get the use of len(sys.argv) here
  else:
    print 'Usage: python sudoku.py puzzle'
    print '  where puzzle is an 81 character string 
             representing the puzzle read left-to-right,
             top-to-bottom, and 0 is a blank'
Reply
#2
this is terrible code, so non-pythonic...
1. lines 3-6, function definitions on single line
2. use of one-char variable names
3. use of one-char function name

also it is python2 code and as new to python you should use python3
anyway, pizzle is entered as 81-chars string. chars on positions 0-8 are first row, 9-17 - second row, etc. in python2 / is floor division, so in floor division every index from 0 to 8 (first 9 indexes) will return 0, 9-17 floor division by 9 will return 1, etc.
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 9/9
1
>>> 17/9
1
>>> 
len(sys.argv) is the number of arguments supplied when run the script from command line. the first element in sys.argv, i.e. sys.argv[0] is always the script name and in this case sys.argv[1] is the puzzle, as string

a is 81-char string, i.e. 81 digits as string. a[:i]+m+a[i+1:] will replace the char (digit on index i) with m
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Ahh thank you!
Reply
#4
I post by mistake after explaining the first lines. in the meantime I updated the post. check my updated post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Apr-03-2018, 06:49 PM)buran Wrote: a is 81-char string, i.e. 81 digits as string. a[:i]+m+a[i+1:] will replace the char (digit on index i) with m
that is not 100% precise
r(a[:i]+m+a[i+1:]) is calling function r with a string that is derived from a, but the char (digit) on index i is replaced with m
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code understanding: Need help in understanding dictionary code jt123 0 496 Jul-09-2023, 01:13 PM
Last Post: jt123
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 1,836 May-09-2023, 08:47 AM
Last Post: buran
  Code Assistance needed in saving the file MithunT 0 846 Oct-09-2022, 03:50 PM
Last Post: MithunT
  Understanding a piece of code Michael1 4 1,471 Jan-20-2022, 07:14 PM
Last Post: Michael1
  Beginner: I need help understanding few lines of a code. hop_090 1 1,723 Sep-07-2020, 04:02 PM
Last Post: Larz60+
  Extracting Rows From Data Frame and Understanding The Code JoeDainton123 0 1,465 Aug-03-2020, 04:08 PM
Last Post: JoeDainton123
  Explanantion needed in part of code... jayg320 6 3,600 Apr-26-2020, 11:33 AM
Last Post: anbu23
  Help Understanding Code Variables 1 1,944 May-02-2019, 05:53 PM
Last Post: micseydel
  Need help understanding simple Array code. Please. stluwa 1 2,258 Apr-13-2019, 07:16 PM
Last Post: loomski
  Trouble Understanding Why This Code Works crocolicious 2 2,754 Apr-09-2019, 05:24 PM
Last Post: crocolicious

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020