IT

day 5 - algorithms 본문

카테고리 없음

day 5 - algorithms

손님번호 2022. 5. 7. 00:27

A algorithm is a set of rules and instructions you use solve the problem. For example, a navigation app uses an algorithm to figure out the fastest path to where you want to go. 

the skill you are adding now is the process of writing and tweaking code so you can use it in different situations.

 

this puzzle has many different solutions, so trust your instincts and try different ideas until one works. A good way to begin is to think through (or write down) how you want to solve the puzzle, and then translate your thought into codes. 

 

 

solution) 

func navigateAroundWall() {

    if isBlockedRight {

        moveForward()

    }  else {

        turnRight()

        moveForward()

    }

}

    

while !isOnClosedSwitch {

    navigateAroundWall()

    if isOnGem {

        collectGem()

        turnLeft()

        turnLeft()

    }

    }

toggleSwitch()

 

comments ) it is not easy work especially, at the end of this process. 

 

 

 

 

 

 

Quest 2 

 

func navigateAroundWall() {

    if isBlockedRight && isBlocked {

        turnLeft()

        moveForward()

    } else if isBlockedRight  {

        moveForward()

    } else {

        turnRight()

        moveForward()

    }

    }

while !isOnClosedSwitch {

    navigateAroundWall()

    

    if isOnGem {

        collectGem()

        turnLeft()

        turnLeft()

    }

}

toggleSwitch() 

    

 

 

 

 

Quest 3

 

 

Solution> 

func navigateAroundWall() {

    if isBlockedRight && isBlocked{

        turnLeft()

        moveForward()

    } else if isBlockedRight{

        moveForward()

    } else {

        turnRight()

        moveForward()

    }

}

 

while !isOnGem {

    navigateAroundWall()

}

collectGem()

 

 

 

 

Quest 4

 

 

my solution > 

 

func navigate() {

    if isOnClosedSwitch && !isBlocked {

        toggleSwitch()

        turnRight()

        moveForward()

    } else if isOnClosedSwitch && isBlocked {

        toggleSwitch()

        turnLeft()

        moveForward()

    } else {

        moveForward()

    }

}

 

while !isOnGem {

    navigate()

}

collectGem()

 

 

comment> figure out the pattern. this is the first thing I have to do. 

 

 

 

 

Quest 5 

 

 

func navigate() {

if isBlocked && isOnGem {

        turnRight()

        collectGem()

        moveForward()

        collectGem()

        

    } else if isBlocked && isBlockedRight {

        turnLeft()

        moveForward()

        if isOnClosedSwitch {

            turnLeft()

            toggleSwitch()

            moveForward()

            toggleSwitch()

            moveForward()

            

        }

        

    } else if isBlocked && isBlockedLeft {

        turnRight()

        

    } else {

        moveForward()

        }

}

 

while !isOnOpenSwitch{

    navigate()

}

 

turnLeft()

 

 

just what I did. it was not easy 

 

 

Comments