7
2
I came across this challenge from codewars. I wish to know how to solve this problem. The problem description is given below.
Its elements are in range[1..n^2]. The matrix is filled by rings, from the outwards the innermost. Each ring is filled with numbers in the following way:
- The first number is written in the top left corner;
- The second number is written in the top right corner;
- The third number is written in the bottom right corner;
- The fourth number is written in the bottom left corner;
- Each next number is written next to the number written 4 steps before it, until the ring is filled.
The matrix is constructed when all numbers are written.```
Given the size of the hole, return the number written at (a, b
) - you may use any consistent indexing, but please specify if (a, b
) is anything other than 0-indexed and row-major.
Example
For n = 4, a = 1, b = 1
, the output should be 13
.
The hole looks like this:
[ [ 1, 5, 9, 2 ]
[ 12, 13, 14, 6 ]
[ 8, 16, 15, 10 ]
[ 4, 11, 7, 3 ]
]
The element at (1, 1) is 13
Test cases
(a, b
) is 0-indexed and row-major here.
n a b result
1 0 0 1
2 0 0 1
3 0 0 1
4 0 0 1
2 0 1 2
3 0 1 5
3 1 1 9
4 1 1 13
4 2 1 16
5 2 3 22
1Welcome to the site. As it stands your question is lacking a crucial component that we require of all questions, an "objective winning criterion". That is, an objective way to score answers. The most common one is [tag:code-golf]. – Post Rock Garf Hunter – 2019-03-09T14:17:50.860
@SriotchilismO'Zaic, Hi When I heard about Programming Puzzles and Code gulf, I thought it's about asking a programming challenge and getting a solution to it. Am sorry, am new to this site. I don't know 'Objective winning criterion' – s326280 – 2019-03-09T14:59:37.580
If you want an easy winning criterion, the majority of the questions on this site are tagged as [tag:code-golf], meaning that the winning criterion is the least bytes. – senox13 – 2019-03-09T15:48:22.773
@senox, Yes. Thanks for helping me. I would go with least bytes as winning criterion – s326280 – 2019-03-09T15:50:04.140
May we instead take 1-indexed indices
[a,b]
(i.e. top-left is 1st row, 1st column, so[1,1]
...and your example13
would be at[2,2]
)? – Jonathan Allan – 2019-03-09T19:11:01.797Please ping me when you've addressed @JonathanAllan's question and I'll cast my vote to reopen. Welcome to PPCG, by the way :) Nice first challenge. For future reference, we have a Sandbox where you can post challenges for community feedback and iron out any issues before posting them to Main.
– Shaggy – 2019-03-09T20:48:12.5776Does codewars allow their challenges to be posted on other sites? – Jo King – 2019-03-10T00:38:39.877
@JoKing. Am sorry, I dont know. – s326280 – 2019-03-10T14:06:31.687
@JonathanAllan Sounds good. – s326280 – 2019-03-10T14:07:06.153
Thanks, I made an update - it's best to edit the post accordingly rather than leaving specifications in the comment section - please check you agree with it. – Jonathan Allan – 2019-03-10T15:26:57.707
@JonathanAllan. Now I agree with 1-indexed array. So do I need to edit the (a,b) values ?. Since it is 1-indexed array, [a,b] starts from [1,1]. – s326280 – 2019-03-10T15:51:22.613
I edited to allow other indexing, but kept zero-indexing and row-major (top-bottom; left-right) as the default (hence why I added the sentence above the test cases too) if you click "edited ... mins ago" you can see the diff. – Jonathan Allan – 2019-03-10T16:05:01.227
4If you're going to accept an answer at all, then at least accept the one that is actually winning by the winning criterion – Jo King – 2019-03-14T08:48:18.827