Sponsored

Collapse

Announcement

Collapse
No announcement yet.

Adjusting Your Algorithm - Swift Playgrounds

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Adjusting Your Algorithm - Swift Playgrounds

    The goal of this level is to adjust your algorithm to navigate around extra blocks.

    This puzzle is similar to the previous one, but it has additional blocks around the walls.

    1. Use pseudocode to plan a workable algorithm.
    2. Before tweaking the code below, walk through the pseudocode in your mind to be sure your algorithm will work.
    3. Write code to implement the algorithm.

    Click image for larger version

Name:	Screen Shot 2017-01-01 at 8.29.19 PM.png
Views:	7933
Size:	212.9 KB
ID:	1244

    Hint: Take the existing code form the previous exercise, and see if you can make it work to solve this slightly different puzzle. Notice that the walls now twist and turn a little more, so you'll need to account for that change in your algorithm.

    The complete solution is shown below.

    Code:
    func navigateAroundWall() {
     if isBlocked && isBlockedRight{
      turnLeft()
     } else if isBlockedRight {
      moveForward()
     } else {
      turn Right()
      moveForward()
     }
    }
    
    while !isOnOpenSwitch {
     while !isOnGem && !isOnClosedSwitch }
      navigateAroundWall()
     }
     if isOnGem {
      collectGem()
     } else {
      toggleSwitch()
     }
     turnLeft()
     turnLeft()
    }
    Super Impressive! You've tweaked an existing algorithm to make it work in different circumstances. This is the power of code, creating programs that solve a problem in many different situations. for example, search engine algorithms can filter over a billion websites to give you the results that you'll be most interested in, no matter what you search for.

    The next lesson we will be looking at is Conquering a Maze.
Apple Swift Programming Language

About this Group

Discussion group for Swift programming language by apple for Xcode
Type: Public
Topics: 112
Comments: 96

Owner

Sponsored Box

Collapse

Latest Group Topics

Collapse

There are no results that meet this criteria.

Working...
X