Rock and Roll

Suppose you owned a food delivery business and in order to make it more profitable you have decided to replace the delivery person with a robot. Your robot can execute the following instructions:

Rock: Climb one step.

Roll: Roll one meter.

Turn: Rotate 90 degrees in the specified direction (left or right). Door: Use one of the robotic arms to either open or knock on the door in front.

Charge: Take the specified amount of money from the customer.

Deliver: Give the specified goods to the customer.

Write an algorithm that prints the exact instructions that the robot should follow in order to deliver one pizza to the floors floor. The floor length is length, the number of steps is steps. After climbing the steps, one has to turn left. Each floor has only one apartment, situated at the end of the floor, to the right. The robot should charge pizzaPrice for one pizza. There is an entrance door to the building, which the robot has to open first. Then it should roll for the entire length of the floor in order to reach the stairway. Take a look at the example input and output to understand better what the robot is supposed to do.

Example

Input:

let floors = 3
let length = 3
let steps = 4
let pizzaPrice = 60

Expected value/output:

Door(Open)
Roll
Roll
Roll
Turn(Left)
Rock
Rock
Rock
Rock
Turn(Left)
Roll
Roll
Roll
Turn(Left)
Rock
Rock
Rock
Rock
Turn(Left)
Roll
Roll
Roll
Turn(Left)
Rock
Rock
Rock
Rock
Turn(Left)
Roll
Roll
Roll
Turn(Right)
Door(Knock)
Charge(60)
Deliver(Pizza)

[collapse]
Hint

Do something along the idea of …

[collapse]
Solution

let floors = 3
let length = 3
let steps = 4
let pizzaPrice = 60

func Rock(_ steps : Int) {
    for _ in 0..<steps {
        print("Rock")
    }
}

func Roll(_ meters : Int) {
    for _ in 0..<meters {
        print("Roll")
    }
}

func Turn(_ side : String) {
    print("Turn(\(side))")
}

func Door(_ action : String) {
    print("Door(\(action))")
}

func Charge(_ amount : Int) {
    print("Charge(\(amount))")
}

func Deliver(_ item : String) {
    print("Deliver(\(item))")
}

func traverseFloor(_ length : Int, _ steps : Int) {
    Roll(length)
    Turn("Left")
    Rock(steps)
    Turn("Left")
}

func deliverPizza(_ floors : Int, _ length : Int, _ steps : Int, _ pizzaPrice : Int) {
    Door("Open")
    for _ in 0..<floors {
        traverseFloor(length, steps)
    }
    Roll(length)
    Turn("Right")
    Charge(pizzaPrice)
    Deliver("Pizza")
}

deliverPizza(floors, length, steps, pizzaPrice)

[collapse]
1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 1.00 out of 5)
Loading...
Subscribe
We send about one email per week with our latest tutorials and updates
Never display this again :)