Python Forum
The problem of the cuts - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: The problem of the cuts (/thread-1682.html)



The problem of the cuts - ics - Jan-19-2017

Hi.
I received this problem as one of my homework problems, and I'm catching my ears with it. I would like some help with it:

Python 2.7

The problem of the cuts: A rectangular metal sheet plate, of Lp x Hp size, has n
holes, in integer coordinates points. Cut a piece of plate of  maximum surface, without holes, knowing that only parallel with the sides of the plate cuts are allowed.
Note: The coordinates of the holes are retained in two vectors vx[i] for the abscises holes
and vy[i] for ordinates (these vectors aren't certainly sorted, wholes being able to be stored in chronologycal order, for example). The initial rectangle, and then
rectangles that appear in the cutting process, are stored through the coordinates of the bottom left corner
(x, y) by length L and height h (each rectangle is identified through a set of 4 variables: x, y, L, h, by with the help of which the 4 corners coordinates are formed). We apply "Divide and conquer" technique.


RE: The problem of the cuts - micseydel - Jan-19-2017

What have you tried? Do you have a specific question?


RE: The problem of the cuts - ics - Jan-19-2017

(Jan-19-2017, 08:21 PM)micseydel Wrote: What have you tried? Do you have a specific question?

I have tried to implement this, but it won't compile


### [1,3] :vx (holes)




def cuts(x,y,L,h):
    for i in range(nrg):
        if vx[i] > x and vx[i] < x+L and vy[i] > y and vy[i] <y+h:
            found = True
            break
    if found:
        1: couts(x,y,L-vx[i],h)
        2: cuts(vx[i],y,L-vx[i]+x,h)
        3: cuts(L-vx[i],y,L-vx[i],h)
        4: cuts(x,h-y,L,h-y)
    else:
        ar = L*h
        if ar > arMax:
            arMax = ar

nrg = 2
vx[1] = [1,3]
vx[2] = [2,3]
[x,y,L,h]
sol[0] = x
sol[1] = y
sol[2] = L
sol[3] = h



RE: The problem of the cuts - micseydel - Jan-19-2017

Could you elaborate on "won't compile"? Python or your IDE should tell you exactly what line it thinks the problem is on. Just looking at it, I'm not sure what you intend for lines like "1: ..." to be doing. That doesn't look like Python; what tutorial or resource have you come across that showed that being used?


RE: The problem of the cuts - wavic - Jan-19-2017

Even if this was a valid Python code, there is no a function call. Just a definition and bunch of variables assignments.


RE: The problem of the cuts - ics - Jan-19-2017

(Jan-19-2017, 08:39 PM)micseydel Wrote: Could you elaborate on "won't compile"? Python or your IDE should tell you exactly what line it thinks the problem is on. Just looking at it, I'm not sure what you intend for lines like "1: ..." to be doing. That doesn't look like Python; what tutorial or resource have you come across that showed that being used?

Well, I'm on my first year on the University, and I have never learned about Python before, so Python programming isn't my skill.
So all that I have learned is from the university courses.
I can't make the code for this execise run.


RE: The problem of the cuts - micseydel - Jan-19-2017

Well, we all started out not knowing Python. You should check this out, and probably start over, testing small amounts of code at a time. As soon as you get stuck, post the minimal code it takes to reproduce it and ask as specific a question as possible.

And again, "can't make the code [...] run" is too vague. Whatever tools you're using should be giving you more specific feedback than that. In this case, it's clear, but for future reference make sure to be more specific. For example, when I try to run your code using IDLE, the colon after the 2 in the "if found" block is highlighted as being a problem. Python happens to be not super helpful in this particular case, since the code is so much not Python, but it's still always the right thing to do to say what Python is telling you.


RE: The problem of the cuts - ics - Jan-19-2017

(Jan-19-2017, 08:53 PM)micseydel Wrote: Well, we all started out not knowing Python. You should check this out, and probably start over, testing small amounts of code at a time. As soon as you get stuck, post the minimal code it takes to reproduce it and ask as specific a question as possible.

And again, "can't make the code [...] run" is too vague. Whatever tools you're using should be giving you more specific feedback than that. In this case, it's clear, but for future reference make sure to be more specific. For example, when I try to run your code using IDLE, the colon after the 2 in the "if found" block is highlighted as being a problem. Python happens to be not super helpful in this particular case, since the code is so much not Python, but it's still always the right thing to do to say what Python is telling you.

[Image: piton.png]


RE: The problem of the cuts - ichabod801 - Jan-19-2017

As micseydel said. "1:" is not valid python code. Are you trying to do some sort of select or case statement? Those do not exist in Python. You have to use if/elif/else:

if i == 1:
    cuts(x, y, L-vx[i], h)
elif i == 2:
    # cuts call for 2
elif i == 3:
    # cuts call for 3
elif i == 4:
    # cuts call for 4
However, you're going to run into other problems. Your function has no return statement. All it will ever return is None. You probably want to return the results of the recursive calls, and something from the final else statement, but that's not entirely clear to me.

More descriptive variable names would be good, too.