id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

submarineNavigator


Problem: Submarine Navigator

As part of an underwater research mission, you're piloting a submarine to map the ocean floor. The submarine responds to a series of commands to move through the water. Your goal is to calculate the final horizontal position and depth after following the planned course.

Commands:
forward X: Increases your horizontal position by X units.
down X: Increases your depth by X units.
up X: Decreases your depth by X units (opposite of down).

You start at a horizontal position of 0 and a depth of 0. The goal is to compute the product of the final horizontal position and depth.


submarineNavigator(["forward 5", "down 5", "forward 8", "up 3", "down 8", "forward 2"]) → 150
submarineNavigator(["forward 10", "down 3", "up 1", "down 2", "forward 7", "up 2"]) → 34
submarineNavigator(["down 10", "forward 3", "up 5", "forward 7", "down 3", "forward 5"]) → 120

...Save, Compile, Run (ctrl-enter)

public int submarineNavigator(String[] commands) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Copyright Nick Parlante 2017 - privacy