0

The following code should print multiple lines of

1
2
3

mixed with lines of

0

However, what it actually prints is multiple lines of

1
1
1
1
3

mixed with lines of

0

Code:

boxes = []
for y in range(len(hmap)):
    for x in range(len(hmap[y])):
        w = 4
        h = 4

        minh = hmap[y][x]
        maxh = hmap[y][x]

        htemp = h
        while True:
            if y + htemp > len(hmap): break

            passes = False
            wtemp = w
            while True:
                if x + wtemp > len(hmap[y]): break

                for c in range(x, x+wtemp):
                    for r in range(y, y+htemp):
                        minh = min(minh,hmap[c][r])
                        maxh = max(maxh,hmap[c][r])

                        if maxh - minh > v:
                            print('1')
                            break
                    else:
                        print('2')
                        break
                else:
                    print('3')
                    break

                print('0')
                passes = True
                wtemp += 1

            if passes:
                boxes.append([x,y,wtemp-1,htemp])

            htemp += 1

            if not passes: break
  • hmap is a 2D array of float values that is passed to the function this code is in.

This segment of code is supposed to generate a series of rectangles for other (irrelevant) parts of code to use later on. Rectangles that "pass" (min/max values don't have a difference greater than v) cause

0

to be printed. Rectangles that don't "pass" should cause

1
2
3

to be printed as the nested for and while loops break. Why doesn't it work?

The Eye
  • 59
  • 12
  • Can you provide an example of `hmap` and `v` which causes this to fail? – bzier Jun 24 '18 at 22:35
  • Pretty much any `hmap` will work, as long as it is an array of equally long arrays (which contain float values). `v` is also a float, so set it to anything as well. – The Eye Jun 25 '18 at 23:37

3 Answers3

1

While attempting to run your code I encountered an IndexError: list index out of range error. It appears that you may have transposed your column and row indices. Try changing the [c][r] subscripts to [r][c]:

# [...]
            for c in range(x, x+wtemp):
                for r in range(y, y+htemp):
                    minh = min(minh,hmap[r][c])
                    maxh = max(maxh,hmap[r][c])
# [...]

I am not sure if this is the cause of the incorrect breaks/prints, but it certainly could make a difference.

bzier
  • 445
  • 3
  • 11
  • haha. The cmd output didn't change, but the function works perfectly in the actual program now. The reason I haven't run into the `IndexError` is because I've been using "square" shaped arrays. – The Eye Jun 25 '18 at 23:41
0

The code could be breaking the wrong loops, I could be wrong. For the while loop, make a Boolean variable and set it to true. Then inside a while loop, use an if statement to make it false when you want to.

top_loop, bottom_loop = True, True
while top_loop:
    # do something
    while bottom_loop:
        # do something
        if condition:
            top_loop = False

I haven't though about the for loops yet. There is answer here on this link with the naming for loops and breaking the for loops. It uses the contextlib library.

Link

entropy32
  • 169
  • 3
  • 11
-2

It looks like the indentation on your code blocks is incorrect. There are else statements aligned with for statements, etc. Python uses indentation to separate blocks of code like this. Double-check that things are aligned properly, either in your code, or in what you copied here. If the indentation is just incorrect in the question here, feel free to edit it.

bzier
  • 445
  • 3
  • 11
  • 3
    Python for statements can have else blocks though. – gilch Jun 24 '18 at 22:17
  • Wow, thanks. Learn something new everyday. http://book.pythontips.com/en/latest/for_-_else.html#else-clause – bzier Jun 24 '18 at 22:19
  • 1
    Also, requests for improvement in the question should normally be in the comments on it, not in the answers. – gilch Jun 24 '18 at 22:22
  • 2
    And while and try statements can have an else clause too by the way. – gilch Jun 24 '18 at 22:24
  • 1
    Right, I was thinking the reason the code was not behaving correctly was due to the indentation, which could have been the answer, but also wanted to give the benefit of the doubt that it could have also been a copy/paste issue in the question. Turned out I just didn't know what I was talking about :) – bzier Jun 24 '18 at 22:24
  • Thanks gilch, appreciate the additional insight – bzier Jun 24 '18 at 22:25