Bunny Worker Locations

Instructions

Keeping track of Commander Lambda's many bunny workers is starting to get tricky. You've been tasked with writing a program to match bunny worker IDs to cell locations.

The LAMBCHOP doomsday device takes up much of the interior of Commander Lambda's space station, and as a result the work areas have an unusual layout. They are stacked in a triangular shape, and the bunny workers are given numerical IDs starting from the corner, as follows:

| 7
| 4 8
| 2 5 9
| 1 3 6 10

Each cell can be represented as points (x, y), with x being the distance from the vertical wall, and y being the height from the ground.

For example, the bunny worker at (1, 1) has ID 1, the bunny worker at (3, 2) has ID 9, and the bunny worker at (2,3) has ID 8. This pattern of numbering continues indefinitely (Commander Lambda has been adding a LOT of workers).

Write a function solution(x, y) which returns the worker ID of the bunny at location (x, y). Each value of x and y will be at least 1 and no greater than 100,000. Since the worker ID can be very large, return your solution as a string representation of the number.

Languages

To provide a Java solution, edit Solution.java

To provide a Python solution, edit solution.py

Test cases

Your code should pass the following test cases. Note that it may also be run against hidden test cases not shown here.

-- Java cases -- Input: Solution.solution(3, 2)

Output: 9

Input: Solution.solution(5, 10)

Output: 96

-- Python cases --

Input: solution.solution(5, 10)

Output: 96

Input: solution.solution(3, 2)

Output: 9

Use verify [file] to test your solution and see how it does. When you are finished editing your code, use submit [file] to submit your answer. If your solution passes the test cases, it will be removed from your home folder.

Solving

The answer will always be equal to the nth triangle number of the sum of x and y minus 1 (to adjust for the fact that the first row is 1, not 0) plus x.

Python

# Return the ID of the bunny at the specified position
def solution(x, y):
    # Gets the nth triangle number and adds the offset for the column number
    return str(sum(range(x + y - 1)) + x)

# Test cases
print(solution(3, 2)) # 9
print(solution(5, 10)) # 96

Tags

  1. foobar (Private)
  2. google (Private)
  3. python (Private)
  4. level-2 (Private)