-
Notifications
You must be signed in to change notification settings - Fork 1
/
bunny_prisoner_locating_readme.txt
82 lines (64 loc) · 2.34 KB
/
bunny_prisoner_locating_readme.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Bunny Prisoner Locating
=======================
Keeping track of Commander Lambda's many bunny prisoners is starting to get tricky. You've been tasked with writing a program to match bunny prisoner 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 prison blocks have an unusual layout. They are
stacked in a triangular shape, and the bunny prisoners 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 prisoner at (1, 1) has ID 1, the bunny prisoner at (3, 2) has ID 9, and the bunny prisoner at (2,3) has ID 8. This pattern of numbering continues
indefinitely (Commander Lambda has been taking a LOT of prisoners).
Write a function answer(x, y) which returns the prisoner 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 prisoner ID can be very large, return your answer as a string representation of the number.
Languages
=========
To provide a Python solution, edit solution.py
To provide a Java solution, edit solution.java
Test cases
==========
Inputs:
(int) x = 3
(int) y = 2
Output:
(string) "9"
Inputs:
(int) x = 5
(int) y = 10
Output:
(string) "96"
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.
Notes
==========
(1,1) 1
(2,1) 3
(3,1) 6
(4,1) 10
(2,1) 2
(2,2) 5
(3,2) 9
(1,3) 4
(2,3) 8
(1,4) 7
(5,10) 96
|16
|11 17 24 32 41
| 7 12 18 25 31
| 4 8 13 19 26
| 2 5 9 14 20
| 1 3 6 10 15
def answer(x, y):
cell_id = 0
for i in range(x):
cell_id += (i + 1)
for j in range(y):
cell_id += j
return cell_id
https://programmium.wordpress.com/2017/06/08/solving-foo-bars-my-experience/
def answer(x, y):
z = ((x+y-1)*(x+y-2))/2 + x
return str(z);
https://codereview.stackexchange.com/questions/200535/finding-the-position-in-a-triangle-for-the-given-challenge