Python Forum
nested for loops to recursion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nested for loops to recursion
#3
You don't need recursion for this, you just need another loop.

start = [x, y]
all_data = []
for direction in [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)]:
    data = []
    current = start[:]
    for step in depth:
        data.append(self.grid[current [0]][current [1]]
        current = [current[0] + direction[0], current[1] + direction[1]]
        if not (0 <= current[0] < len(self.grid)) or not (0 <= current[1] < len(self.grid[0])):
            break
    all_data.append(data)
I didn't test the above code, but that's the basic format you want. Loop through the directions. For each direction go back to the start and step in that direction depth times. For each step, record the data at that point and check that you haven't gone off the edge of the grid.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
nested for loops to recursion - by ashkea26 - Nov-02-2018, 04:36 AM
RE: nested for loops to recursion - by Larz60+ - Nov-02-2018, 09:22 AM
RE: nested for loops to recursion - by ashkea26 - Nov-02-2018, 04:26 PM
RE: nested for loops to recursion - by ichabod801 - Nov-02-2018, 01:04 PM
RE: nested for loops to recursion - by ichabod801 - Nov-02-2018, 05:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with nested loops robert5502001 7 3,668 Aug-01-2022, 06:26 AM
Last Post: stevensanders
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 6,572 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  GCF function w recursion and helper function(how do i fix this Recursion Error) hhydration 3 2,595 Oct-05-2020, 07:47 PM
Last Post: deanhystad
  computing average in nested loops cap510 5 5,232 Sep-11-2020, 12:33 PM
Last Post: deanhystad
  Capitalize first letter of names in nested loops student_of_python 9 4,833 Oct-27-2019, 07:51 AM
Last Post: Larz60+
  Nested loops in openCV JimmyJangle 1 4,858 Apr-17-2018, 04:10 AM
Last Post: Mekire
  Nested for loops with numbers Liquid_Ocelot 7 5,942 Aug-15-2017, 06:38 AM
Last Post: nilamo
  Nested loops, lists and if statements Liquid_Ocelot 10 9,088 Apr-23-2017, 02:02 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

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