Python Exercise Help on Citizen Code Site
Solvedlienalo Posted messages 1 Registration date Status Membre Last intervention -
Hello,
I'm stuck on this exercise:

As you can see, I need to finish in 25 lines with the following commands:
- right()
- left()
- take()
- put()
- for loop in range()
But at the moment, I can only arrange column 2 with the following code that is 24 lines:
from robot import * right() for loop in range(4): take() left() put() right() take() for loop in range(4): right() put() for loop in range(5): left() for loop in range(4): take() right() put() left() for loop in range(5): right() take() for loop in range(4): left() put()
Can you help me understand where I'm making a mistake? Thank you.
4 réponses
Hello,
- Thank you for taking care of the spelling.
- Think about a strategy before coding (how you would explain it in words).
- Specify how the bricks should be placed. If I understood correctly, the castle is viewed from the side, the bricks are stacked, and you would like to swap the bricks from the bottom row with those from the top row.
- Are we allowed to rebuild the castle anywhere other than in the middle of the grid?
- It should also clarify what "placing" does (can we drop a brick in the air, if we place a box, does the claw return to its place, etc...)
Assuming I understood the exercise correctly, the simplest strategy is to:
If we are allowed to shift the castle one space to the left, we repeat the reasoning by moving column i+1 to column i, for each value of i from 2 to 5 inclusive. This can be expressed as suggested by yg_be with two nested for loops.
If we are not allowed to shift the castle one space to the left, we need to add an additional step that will allow us to transfer column 1 to column 5 while preserving the order. Indeed, the brick at the top of column 1 is the top of the castle and we want to keep it at the top. So, we set it aside in column 6. We transfer the rest of column 1 into column 5. Then we bring back the remaining brick in column 6 to the top of column 5.
Good luck
Hello
Could you detail your code a bit?
What do the different instructions take left right put mean?
What do the values 4 and 5 you use in your loops refer to?
.
Best regards,
Jordane
It is an exercise in nested loops, and you have made no nesting.
Hello! Personally, I managed to do it in just 25 blocks like this:
from robot import * for loop in range (4): for loop in range (5): right() take() left() place() right() for loop in range (4): left() take() for loop in range (5): right() place() left() for loop in range (4): for loop in range (4): left() take() for loop in range (4): right() place() right() take() left() place()
But there is potentially a way to do it in fewer blocks