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 8968261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:25:48+00:00 2026-06-15T17:25:48+00:00

I have a 2D array in Python (version 3.2) that looks like this: …AAA….

  • 0

I have a 2D array in Python (version 3.2) that looks like this:

...AAA....
...AAABB..
..BBBBBCC.
.....CCCC.
.DDD..CC..
.DDD......

It represents a kind of map with areas painted different colors. The above example shows four distinct regions, A, B, C, and D.

Here’s an example of indexing the array:
map[1][5] == ‘A’ would return True.

I’m trying to write a function that takes in an array like this, and a row/col index, and returns the number of adjoining spaces that are of the same “color”. So using that example above, here are some return values (the arguments are the array, row, and column number respectively:

6 <-- countArea(map, 5, 2)
8 <-- countArea(map, 2, 8)

I’d like to implement this as a recursive function, but I can’t figure it out. Here’s what I have so far:

def countArea(map, row, col):

    key = map[row][col]

    if (map[row-1][col] == key):
        return 1 + countArea(map, row-1, col)
    elif (map[row+1][col] == key):
        return 1 + countArea(map, row+1, col)
    elif (map[row][col+1] == key):
        return 1 + countArea(map, row, col+1)
    elif (map[row][col-1] == key):
        return 1 + countArea(map, row, col-1)
    else:
        return 1

I know I’m missing something basic here. I’m basically saying “here is the current character, now look in each direction to see if it has the same character.”

My question is, what am I missing in this recursive definition?

Thanks for your help.

  • 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-06-15T17:25:49+00:00Added an answer on June 15, 2026 at 5:25 pm

    My question is, what am I missing in this recursive definition?

    Once a grid square has been counted, it must not be counted again (this includes counting by recursive invocations of countArea()!)

    Your current algorithm goes as far north as it can, and then keeps taking one step to the south followed by one step to the north. This two-step sequence repeats until you run out of stack space.

    If you like, you could read up on algorithms for this problem in Wikipedia.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does python have a function like call_user_func() in PHP? PHP Version: call_user_func(array($object,$methodName),$parameters) How do
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
I have an array in python that contains a set of values, some of
if I have this Python array: mac_tags = [ global_rtgn, global_mogn ] And I
I have a global variable array in c that I'd like to pull into
I have some board numpy arrays like that: array([[0, 0, 0, 1, 0, 0,
I have the following array(in python): arr= ['\n', ' a#B\n', ' c#D\n'] Now I
I have a 3D array in Python and I need to iterate over all
I have a char array in Python, which is read from a file e.g.

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.