Skip to main content

Reading Code Sequences to Choose a Preferred Solution

As your class worked on the Play section, you probably noticed that there were many different solutions to the same problem that was presented. That is something that happens often in the world of programming. While there can be different ways to do something, everyone will have a preferred method or solution, and that could be based on a number of factors. In this activity, your group will look at 3 possible code block solutions to a problem, figure out if they work in the intended fashion, then choose a preferred solution from the group and explain why you chose it.

The problem: Create a sequence that, when the robot begins at the Start, will travel into and out of all 3 classrooms (in any order) and return to the End point. The robot must navigate around the stairs, and can go through the doorways marked by the small rectangles on the map image.

Sketched layout showing the Start at the bottom left corner and the End at the bottom right corner. Above the Start is Room A with a doorway facing the start and one to the right, facing the stairs in the center. Directly to the right of the stairs are Room B and Room C stacked above the End. There are doorways to Rooms B and C facing the stairs, and one connecting the two.

  • For each solution below, answer these 3 questions in your engineering notebook:
    • Does this code accomplish the task? Why or why not?
    • If it does work, what are the advantages of this code over the others?
    • What are the disadvantages of this code over the others?
  • Most of the time, programming is evaluated on its efficiency, and the preferred solution is one that accomplishes the goal in the most efficient way possible.

Click on the solutions below to see a larger image.

Example solution for the challenge consisting of 19 Drive to and Turn for blocks connected to the When Started block. In order, the blocks will make the robot Turn right 90 degrees, drive forward 10 inches, turn left 90 degrees drive forward 3 inches, then reverse 6 inches, then turn left 90 degrees, drive forward 10 inches. Then turn right 90 degrees, drive forward 8 inches and reverse 4 inches, turn right 90 degrees, drive forward 10 inches. Then turn left 90 degrees, drive forward 1 inch, turn right 90 degrees, drive forward 3 inches and reverse 4, then turn right 90 degrees and drive forward 5 inches.
Solution A (inches)

An example solution with 11 Drive for and Turn for blocks connected to a When started block. In order the blocks will make the robot drive forward 10 inches, turn left 90 degrees, drive forward 1 inch, turn left 90 degrees, drive forward 1 inch, turn right 90 degrees, drive forward 10 inches, turn right 90 degrees, then drive reverse 5 inches, turn right 90 degrees and drive forward 10 inches.
Solution B (inches)
An example solution with 10 Drive for and Turn for blocks connected to the When started block. In order, the blocks will make the robot drive forward for 8 inches then 4 inches, turn right 90 degrees, drive forward 13 inches, turn left 90 degrees, drive forward 8 inches, turn left 90 degrees, drive forward 4 inches, turn right 90 degrees, and drive reverse for 5 inches.
Solution C (inches)
Example solution for the challenge consisting of 19 Drive to and Turn for blocks connected to the When Started block. In order, the blocks will make the robot Turn right 90 degrees, drive forward 254mm, turn left 90 degrees drive forward 76mm, then reverse 152mm, then turn left 90 degrees, drive forward 254mm. Then turn right 90 degrees, drive forward 203mm and reverse 101mm, turn right 90 degrees, drive forward 254mm. Then turn left 90 degrees, drive forward 25mm, turn right 90 degrees, drive forward 76mm and reverse 101mm, then turn right 90 degrees and drive forward 127mm.
Solution A (metric)
An example solution with 11 Drive for and Turn for blocks connected to a When started block. In order the blocks will make the robot drive forward 254mm, turn left 90 degrees, drive forward 25mm, turn left 90 degrees, drive forward 25mm, turn right 90 degrees, drive forward 254mm, turn right 90 degrees, then drive reverse 127mm, turn right 90 degrees and drive forward 254mm..
Solution B (metric)
An example solution with 10 Drive for and Turn for blocks connected to the When started block. In order, the blocks will make the robot drive forward for 203mm then 101mm, turn right 90 degrees, drive forward 330mm, turn left 90 degrees, drive forward 203mm, turn left 90 degrees, drive forward 10mm, turn right 90 degrees, and drive reverse for 127mm..
Solution C (metric)

What is Code Efficiency?

If you needed to get 3 things from your kitchen, you would probably go get all 3 things in one trip, instead of making 3 separate trips. Why? Because it would be more efficient. Efficient means “achieving maximum productivity with minimum wasted effort or expense.” So how does that connect to programming?

Code efficiency means that a program is written to work well and quickly, using the least amount of work, or steps, properly, while still doing its job. It is important because it reduces the risk of a program not functioning well, and in turn, causing problems for other parts of a program that are running around it. The efficiency of a code can be a measure of quality, and when we write projects in VEXcode IQ block, we are often looking for the most “efficient” project as the preferred solution.

Basically, code efficiency is a principle that means you want to write a code that accomplishes your goal using the fewest commands, or blocks, possible.

In your Engineering Notebook, answer the following questions:

  • With code efficiency in mind, which is your preferred solution and why?
  • Do you think that is the best solution, why or why not?