Abit of Python-to-C help.

This is a discussion on Abit of Python-to-C help. within the C Programming forums, part of the General Programming Boards category; Hey guys, Im writing a sudoku game following this article: http://norvig.com/sudoku.html Im getting close, but Im having a problem with ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    69

    Abit of Python-to-C help.

    Hey guys,

    Im writing a sudoku game following this article:
    http://norvig.com/sudoku.html

    Im getting close, but Im having a problem with the "search" function.
    Its:
    Code:
    def search(values):
        "Using depth-first search and propagation, try all possible values."
        if values is False:
            return False ## Failed earlier
        if all(len(values[s]) == 1 for s in squares): 
            return values ## Solved!
        ## Chose the unfilled square s with the fewest possibilities
        _,s = min((len(values[s]), s) for s in squares if len(values[s]) > 1)
        return some(search(assign(values.copy(), s, d)) 
    		for d in values[s])
    Im trying to implement this in C but I dont get it :/ I see there is a recursive thing and the 2 ifs in the beginning check if the board is already sorted. But the last 3 lines. What do they do?

    (I know its not the best place to ask this :/ )

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by +Azazel+ View Post
    Hey guys,

    Im writing a sudoku game following this article:
    http://norvig.com/sudoku.html

    Im getting close, but Im having a problem with the "search" function.
    Its:
    Code:
    def search(values):
        "Using depth-first search and propagation, try all possible values."
        if values is False:
            return False ## Failed earlier
        if all(len(values[s]) == 1 for s in squares): 
            return values ## Solved!
        ## Chose the unfilled square s with the fewest possibilities
        _,s = min((len(values[s]), s) for s in squares if len(values[s]) > 1)
        return some(search(assign(values.copy(), s, d)) 
    		for d in values[s])
    Im trying to implement this in C but I dont get it :/ I see there is a recursive thing and the 2 ifs in the beginning check if the board is already sorted. But the last 3 lines. What do they do?

    (I know its not the best place to ask this :/ )
    The last few lines try to choose the next cell to take a guess at it's value. The cell with the lowest number of candidate values, is chosen, and one of the candidate values (usually in the order of lowest to highest digit), is chosen for the next guess.

    My solver, also in C, uses a small array of structs which the cells remaining unsolved are put into, along with their row and col, and the number of candidate values it has remaining after the logic portion is done with it.

    Then there's an index array that is used to sort the small array of structs, so they don't move from their original position.

    Now I go through the remaining cells, just by accessing the small array of structs, through it's index. The code looks quite like stew, but some of that is because I did the DFS iteratively, rather than recursively.

    If you'll post up your C code search function, I'll be able to help you more.

    For more Sudoku programming help, you may want to also check out the Sudoku Programmers Forum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. python c api
    By ralu. in forum C Programming
    Replies: 0
    Last Post: 03-01-2009, 12:19 PM
  2. C and Python
    By alexnb185 in forum Tech Board
    Replies: 5
    Last Post: 04-11-2008, 11:11 PM
  3. Python Embedding Help...
    By Rendered in forum C Programming
    Replies: 2
    Last Post: 11-17-2007, 09:08 AM
  4. python in c++ application
    By l2u in forum C++ Programming
    Replies: 5
    Last Post: 04-18-2007, 07:50 AM
  5. Python
    By Xterria in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 05-05-2002, 04:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21