Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6933415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:49:48+00:00 2026-05-27T11:49:48+00:00

I am new to python and programming in general. I have written a function

  • 0

I am new to python and programming in general. I have written a function which will search adjoining elements in an array and look for ones with values within 0.05 of each other much like a floodfill algorithm does. The only difference is I am doing something stupid when it comes to counting how many time the function is run (which I am thinking will also tell me how many elements I found), so my counter values are wrong. The code works when it comes to finding adjoining elements within 0.05 of each other, just the counting is funny.

def floodcount (x,y,array,value,count=0):     #akin to a bucket fill in paint, finds the area instead

    nrows = len(array)-1          #rows of the image
    ncols = len(array[0])-1       #columns of the image
    diff = array[x][y] - value
    if (diff < 0.00) or (diff > 0.05): # the base case, finding a diff more than 0.05 or less than 0 is like finding a boundary
        return 0

    count = count +1
    print 'count1 ',count

    array[x][y] = -5 # so we do no calculate this pixel again
    #print "[",x,",",y,"]"
    if x > 0:
        #print '1'# if not the first elemnet then can go back, this makes sure that x is within the array all the time
        floodcount (x-1,y,array,value,count)
    if y > 0:
        #print '2'
        floodcount (x,y-1,array,value,count) 
    if x < nrows:
        #print '3'
        floodcount (x+1,y,array,value,count)
    if y < ncols:
        #print '4'
        floodcount (x,y+1,array,value,count)
    if x > 0 and y > 0:
        #print '5'
        floodcount (x-1,y-1,array,value,count)
    if x < nrows and y < ncols:
        #print '6'
        floodcount (x+1,y+1,array,value,count)
    if x <nrows and y > 0:
        #print '7'
        floodcount (x+1,y-1,array,value,count)
    if x > 0 and y < ncols:
        #print '8'
        floodcount (x-1,y+1,array,value,count)

    print 'count2 ',count    
    return count

So for a test case

array = [[5,1,1,3,4],[4,5,6,2,5],[5,8,5,5,9]]
x=0 and y=0

OUTPUT

count1 1
count1 2
count1 3
count1 4
count1 5
count2 5
count2 4
count2 3
count1 3
count2 3
count2 2
count2 1

As you can see something is fishy 😛
Can anyone point out what I am doing wrong? Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:49:48+00:00Added an answer on May 27, 2026 at 11:49 am

    In addition to the now solved count problem:

    You can reduce the number of if statements by doing all recursive calls everytime and just checking the array borders at the beginning of the function with if x < 0 or y < 0 or x > nrows or y > ncols.

    #akin to a bucket fill in paint, finds the area instead
    def floodcount (x,y,array,value,count=0): 
        nrows = len(array)-1          #rows of the image
        ncols = len(array[0])-1       #columns of the image
        if x < 0 or y < 0 or x > nrows or y > ncols:
            return count
    
        diff = array[x][y] - value
        # the base case, finding a diff more than 0.05 or less than 0 is like finding a boundary
        if (diff < 0.00) or (diff > 0.05): 
            return count
    
        count = count +1
        print 'count1 ',count
    
        array[x][y] = -5 # so we do no calculate this pixel again
        #print "[",x,",",y,"]"
    
        count = floodcount (x-1,y,array,value,count)
        count = floodcount (x,y+1,array,value,count)
        count = floodcount (x+1,y,array,value,count)
        count = floodcount (x,y-1,array,value,count)
    
        count = floodcount (x-1,y-1,array,value,count)
        count = floodcount (x+1,y+1,array,value,count)
        count = floodcount (x+1,y-1,array,value,count)
        count = floodcount (x-1,y+1,array,value,count)
    
        print 'count2 ',count    
        return count
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to Python and multithreaded programming in general. Basically, I have a
I'm fairly new to Python and programming in general. I have done a few
New to Python and programming in general. I want to install a module from
I'm new to Python and programming in general (a couple of weeks at most).
I'm new to Python (I have been programming in Java for multiple years now
I am somewhat new to Python and programming in general so I apologize. By
I'm new to Squeak and Smalltalk but not programming in general(i've some Python experience).
I am new to C++/Python mixed language programming and do not have much idea
I am new to Python (I dont have any programming training either), so please
I am very new to Python/Django and programming in general. With the limited tools

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.