Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
recursion
#1
Write a program that, given a starting location in a 2D grid of characters, will count how many contiguously connected @ symbols there are. Cells have to be direct neighbors either horizontally or vertically to count as contiguous. However, the right/left and top/bottom edges ARE considered to be connected – you can “wrap around” from one side of the matrix to the opposite one.

The grid of chars will always be 10x10. Your program should load the grid of chars from a file called “grid.txt”. A sample grid.txt is included.

Your program MUST make use of a recursive function for the core problem.
You may use loops to read in the data from the file.

Do NOT use a global variable to keep track of the count.

Sample Runs – User input in BOLD – all row/col are zero-indexed

See the image below for an example of how the characters are counted.

Run 1:
Enter row and column:
0 0
5 cells

Run 2:
Enter row and column:
1 7
5 cells

Run 3:
Enter row and column:
3 1
11 cells

Run 4:
Enter row and column:
2 0
0 cells

Sample Grid:
@-@--@-@@-
@@@-@@-@--
---@------
@@-@@@-@@@
-@-@-@-@--
@@-@@@-@@-
----------
-@@@-@----
-@@@@@-@-@
-------@-@


this is what i have so far, I have spent hours trying to figure out where to go from here and I am lost. I am currently working on the check neighbors function
def import_grid(filename):    #takes file and puts in into lists for grid
    with open(filename, "r") as temp_file:
        full_grid = []
        temp_grid = []
        for gridLines in temp_file.readlines():
          temp_grid.append(gridLines.rstrip().split("[]"))
        for line in temp_grid:
          for i in line:
            full_grid.append(i)
        return full_grid


def get_input():
    user_input_list = []
    user_input = input("Enter row and column:")
    for char in user_input:
        user_input_list.append(char)
    return user_input_list


def check_neighbors(grid, row, col):
    row_size = 10
    col_size = 10
    start = grid[row-1][col-1]  #this is the starting point
    print(start)



def main():
  file = "grid.txt"
  location = get_input()
  grid = import_grid(file)
  row = int(location[0])
  col = int(location[2])
  number_connected = check_neighbors(grid, row, col)

if __name__ == '__main__':
    main()
Reply
#2
You read the grid from a file, grid.txt. You then get input for the row and column to identify the source cell. Now you need to find the number of cells with a @ that surround the cell and chain off that, with wraparound.

Start by skipping the wraparound part. You will need to check the cells (row-1,col-1), (row-1,col), (row-1,col+1), (row,col-1), etc. and add them up.

For the wraparound, you will need a fancy way to avoid negative numbers and positives over 9 in your checking. Adding 10 and then doing it mod(10) will work nicely.

Then, for the recursion part, you need to do the same count function on each cell that is contiguous. And, you can't count cells twice (if a cell has already been counted don't count again and don't go back to that cell to restart the process).

I would change the @ to something else, like a * when it has been counted, then change back when complete.

Start, and when stuck, ask for more help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  GCF function w recursion and helper function(how do i fix this Recursion Error) hhydration 3 3,497 Oct-05-2020, 07:47 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020