[Python] Trying to debug my program

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
8 replies · 2K views
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:
Physics 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.
 
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.