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?

2
  • Can you provide an example of hmap and v which causes this to fail? Commented Jun 24, 2018 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. Commented Jun 25, 2018 at 23:37

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

1 Comment

I'm not sure how this would work in my code. It may cause issues.
-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.

6 Comments

Python for statements can have else blocks though.
Wow, thanks. Learn something new everyday. book.pythontips.com/en/latest/for_-_else.html#else-clause
Also, requests for improvement in the question should normally be in the comments on it, not in the answers.
And while and try statements can have an else clause too by the way.
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 :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.