[Python] Trying to debug my program

Click For Summary

Discussion Overview

The discussion revolves around debugging a Python program designed to estimate the average number of stones required for two stones to become adjacent on a 19x19 goban in the game of Go. Participants explore issues related to code functionality, efficiency, and programming style, as well as considerations for translating the code into Common Lisp.

Discussion Character

  • Technical explanation, Debugging, Conceptual clarification, Debate/contested

Main Points Raised

  • One participant describes their goal of calculating the average number of stones required and shares their code, noting unexpected results.
  • Another participant points out that the appending of the counter to the trial list is outside the loop, suggesting that this affects the mean calculation.
  • A participant acknowledges the indentation issue and claims to have resolved the problem by resetting the counter and board within the loop.
  • Some participants suggest using `enumerate` for a more elegant solution instead of manually defining a counter.
  • Discussion shifts to the participant's intention to translate the code into Common Lisp, with one participant expressing skepticism about transliterating code from one language to another.
  • Concerns are raised about the participant's approach to learning a new programming language, emphasizing the importance of understanding idioms rather than direct translation.
  • Another participant critiques the original code's structure, suggesting that the use of an `if` statement in a boolean function could be simplified.

Areas of Agreement / Disagreement

Participants generally agree on the need for code corrections and improvements, but there is disagreement on the approach to learning new programming languages and the best practices for coding style.

Contextual Notes

Participants mention issues related to code indentation and the importance of resetting variables within loops, indicating potential limitations in the original code's structure.

fluidistic
Gold Member
Messages
3,934
Reaction score
286
Hi people,
I'm trying to figure out what's wrong with my code.
My goal is to calculate the average number of stones it takes so that 2 stones become adjascent on a 19x19 goban (the board of the game of go), when we randomly pick an intersection and place a stone on it. The answer should be almost 13 if I remember well.
Here's my code:
Python:
# Program that estimates the average number of stones required so that 2 stones
# become adjascent in an nxn goban. I want to plot the distribution of number
# of stones vs numbers of trial, i.e. an histogram.
import random
import numpy as np

# Size of the board
n = 19

# Number of trials
trials = 1000
# Initialize the board list of lists to 0. 0 means no stone, 1 means stone.
# Note that board[0][0] is the first element and board[20][20] is the last one
# , for when n=19. That's because I define the board
# as n+2 x n+2 for the edges...
board = [[0]*(n+2) for i in range(n+2)]
counter = 0
# Generate random coordinates inside of the 19x19 board
x_coordinates, y_coordinates = random.randint(1, n), random.randint(1, n)

# Now define a boolean function that I'll use as a condition in a while loopdef check_if_stones(x_coordinates, y_coordinates):
  if (board[x_coordinates-1][y_coordinates] == 1
  or board[x_coordinates][y_coordinates-1] == 1
  or board[x_coordinates+1][y_coordinates] == 1
  or board[x_coordinates][y_coordinates+1] == 1):
      return False
  else:
      return True

trial_list = []
for j in range(trials):
    while check_if_stones(x_coordinates, y_coordinates):
        if board[x_coordinates][y_coordinates] == 0:
            # Now replace a random element by 1
             board[x_coordinates][y_coordinates] = 1
             counter += 1
             x_coordinates, y_coordinates = random.randint(1, n), random.randint(1, n)
    x_coordinates, y_coordinates = random.randint(1, n), random.randint(1, n)
    # Update the counter when the boolean function returns false. Do this outside of the while loop.
    counter += 1
    trial_list.append(counter)
    x_coordinates, y_coordinates = random.randint(1, n), random.randint(1, n)
print(np.mean(trial_list))

Now, when trials = 1000 like in the code, one would expect the result to be close to 13. But it returns a number around 622. When trial is worth 5 I get an output of around 17...
So that np.mean(trial_list) depends heavily on "trials" while it shouldn't depend that heavily on it.
I've no idea what's wrong with the code and I'm desperate to fix it. I'd appreciate if you could spot anything weird, thanks!

P.D.:Sorry about the indentation, it seems to be broken when I paste it from Emacs. I've edited it manually but I may have goofed here on PF somewhere.
 
Last edited:
Technology news on Phys.org
The trial_list append is outside the j loop, so append(counter) is executed once. The mean then is meaningless.
 
mheslep said:
The trial_list append is outside the j loop, so append(counter) is executed once. The mean then is meaningless.
My bad... the indentation is really messed up. In my code it is well inside the for j loop.
EDIT: Problem solved! I had to reset both the counter and the board in the for j loop! It now works fine!
 
Last edited:
You should look at using enumerate instead of defining "counter=0" to see if you like it more; it's more elegant to me.
 
  • Like
Likes   Reactions: fluidistic
Arsenic&Lace said:
You should look at using enumerate instead of defining "counter=0" to see if you like it more; it's more elegant to me.
Thanks for the tip. That would look more pythonic indeed. I'll check it out... though my priority for now is to translate this code into common lisp. I'm just starting to learn CL, it looks quite hard.
 
LISP = Lots of Infuriating Superfluous Parentheses o0)
 
  • Like
Likes   Reactions: fluidistic
Chicken scheme is my preferred lisp-dialect frankly, since scheme is less nasty than lisp and chicken's c compiler allows you to write decently fast code.
 
fluidistic said:
Thanks for the tip. That would look more pythonic indeed. I'll check it out... though my priority for now is to translate this code into common lisp. I'm just starting to learn CL, it looks quite hard.
First things first, why? (And why not Haskell, or Erlang, or OCaml?)

Secondly, why are you trying to transliterate? There's a name for the hilarious result that typically ensues when a Fortran program is translated, statement for statement, to C. It's called C-Tran (and that is a derogatory term). Don't transliterate. On trying to learn a new language, learn the language. Jump right in! Learn the language, learn the idioms. Start from scratch. Don't translate.

Thirdly, your code doesn't look pythonic to me. What's with the if statement in def check_if_stones? There's no need for an if. All you need is the boolean expression. Learn to do that and you'll be one step closer to functional programming.
 
  • Like
Likes   Reactions: fluidistic
D H said:
First things first, why? (And why not Haskell, or Erlang, or OCaml?)
One of my dreams is to understand Maxima's code and eventually contribute to it. I'd like to fix bugs, make the program more efficient, etc.

Secondly, why are you trying to transliterate? There's a name for the hilarious result that typically ensues when a Fortran program is translated, statement for statement, to C. It's called C-Tran (and that is a derogatory term). Don't transliterate. On trying to learn a new language, learn the language. Jump right in! Learn the language, learn the idioms. Start from scratch. Don't translate.
Because as of now I don't know almost any lisp and as first program I want to be able to replicate what the Python's code is doing. I never started with the hello world program with python either, I jumped right into problem solving.
Thirdly, your code doesn't look pythonic to me. What's with the if statement in def check_if_stones? There's no need for an if. All you need is the boolean expression. Learn to do that and you'll be one step closer to functional programming.

Ok thanks for the tip. I don't see it now, but I'll think about it.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 15 ·
Replies
15
Views
2K
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
6K