7
Specifications
For this challenge, you will be given a matrix of some sort in any reasonable format for a 2-D array. For each row except the last, in order from top (first) to bottom (last), rotate the row below it by x
to either direction (you choose a consistent direction to use) where x
is the first element in the current row.
Reference
A reference implementation in Python can be found here
Example
Let's walk through an example. Take the following matrix for example:
1 5 8
4 7 2
3 9 6
Let's say we're rotating right. First we rotate [4 7 2]
to the right by 1 because the first element of [1 5 8]
is 1. Thus, we get [2 4 7]
as the second row. The matrix is now like this:
1 5 8
2 4 7
3 9 6
Then, we rotate [3 9 6]
to the right by 2 because the first element of the second row is 2. Thus we get:
1 5 8
2 4 7
9 6 3
Challenge
Implement the above
Test Cases
Test cases are given as a list of lists in Python style.
Input -> Output
[[6, 7, 10, 1, 10, 7], [7, 3, 7, 8, 9, 2], [6, 8, 3, 9, 3, 1], [6, 3, 8, 6, 4, 1], [7, 5, 2, 9, 7, 2], [2, 10, 9, 9, 7, 9], [8, 8, 10, 10, 8, 4]] -> [[6, 7, 10, 1, 10, 7], [7, 3, 7, 8, 9, 2], [1, 6, 8, 3, 9, 3], [1, 6, 3, 8, 6, 4], [2, 7, 5, 2, 9, 7], [7, 9, 2, 10, 9, 9], [4, 8, 8, 10, 10, 8]]
[[8, 10, 4, 3, 9, 4, 6], [8, 8, 8, 8, 6, 7, 5], [6, 2, 2, 4, 8, 9, 6], [1, 7, 9, 9, 10, 7, 8]] -> [[8, 10, 4, 3, 9, 4, 6], [5, 8, 8, 8, 8, 6, 7], [2, 4, 8, 9, 6, 6, 2], [7, 8, 1, 7, 9, 9, 10]]
[[6, 2, 4, 4, 4], [5, 9, 10, 5, 4], [3, 5, 7, 2, 2], [1, 5, 5, 10, 10], [8, 2, 3, 2, 1], [3, 3, 9, 5, 10], [9, 5, 9, 7, 2]] -> [[6, 2, 4, 4, 4], [4, 5, 9, 10, 5], [5, 7, 2, 2, 3], [1, 5, 5, 10, 10], [1, 8, 2, 3, 2], [10, 3, 3, 9, 5], [9, 5, 9, 7, 2]]
[[3, 6, 3, 7, 4], [8, 8, 1, 5, 8], [7, 5, 1, 9, 4], [3, 9, 10, 8, 6]] -> [[3, 6, 3, 7, 4], [1, 5, 8, 8, 8], [4, 7, 5, 1, 9], [9, 10, 8, 6, 3]]
[[5, 6, 1, 2, 6], [1, 10, 10, 1, 4], [6, 2, 7, 1, 7], [9, 5, 8, 6, 3], [4, 5, 1, 2, 1], [8, 6, 1, 1, 3]] -> [[5, 6, 1, 2, 6], [1, 10, 10, 1, 4], [7, 6, 2, 7, 1], [6, 3, 9, 5, 8], [1, 4, 5, 1, 2], [3, 8, 6, 1, 1]]
[[10, 1, 2], [9, 9, 10], [2, 4, 7]] -> [[10, 1, 2], [10, 9, 9], [7, 2, 4]]
[[4, 5, 6], [6, 9, 4], [9, 3, 2], [3, 5, 9], [5, 5, 3], [9, 1, 4]] -> [[4, 5, 6], [4, 6, 9], [2, 9, 3], [5, 9, 3], [5, 3, 5], [1, 4, 9]]
[[8, 7, 6, 5], [9, 8, 6, 6], [7, 9, 7, 10]] -> [[8, 7, 6, 5], [9, 8, 6, 6], [10, 7, 9, 7]]
[[10, 2, 8, 8, 1], [2, 10, 1, 4, 10], [3, 2, 5, 3, 8], [5, 1, 8, 1, 8], [6, 1, 3, 1, 2], [7, 9, 1, 1, 2]] -> [[10, 2, 8, 8, 1], [2, 10, 1, 4, 10], [3, 8, 3, 2, 5], [8, 1, 8, 5, 1], [3, 1, 2, 6, 1], [1, 1, 2, 7, 9]]
[[9, 1, 3, 2, 7], [3, 8, 10, 3, 3], [8, 10, 7, 9, 5], [8, 1, 4, 9, 9], [6, 8, 4, 10, 10], [4, 7, 6, 2, 2], [8, 5, 3, 7, 6]] -> [[9, 1, 3, 2, 7], [8, 10, 3, 3, 3], [7, 9, 5, 8, 10], [9, 9, 8, 1, 4], [8, 4, 10, 10, 6], [6, 2, 2, 4, 7], [6, 8, 5, 3, 7]]
1
we rotate [4 7 2] to the right by 1 ... we get [2 7 4]
Wait, what? Shouldn't it be[2 4 7]
? Or am I misunderstanding "rotation"? – totallyhuman – 2017-09-07T21:58:38.8401
For each row except the last
Shouldn't that beexcept the first
? – totallyhuman – 2017-09-07T22:00:54.003@icrieverytim
rotate the row below it
technically this is inconsistent with my title but eh – HyperNeutrino – 2017-09-07T22:03:55.420@icrieverytim no you're right, i'm just being dumb whoops. prolly should've used my program – HyperNeutrino – 2017-09-07T22:04:11.140
2Are we guaranteed that the matrix is positive integers and that the matrix has at least two rows? – Giuseppe – 2017-09-07T22:14:18.023
Exactly what I was about to ask, @Giuseppe. – Shaggy – 2017-09-07T22:18:06.850
1Since
x
can be larger than the length of the row below (according to the test cases), what exactly is[2, 5, 8]
rotated by4
? The second test case says it's[2, 5, 8]
which doesn't make a lot of sense to me... – totallyhuman – 2017-09-07T22:20:49.200The reference implementation is broken, the
– Jonathan Allan – 2017-09-07T22:43:36.037rotate
method currently concatenates two slices to yield the same asarray
whatever theamount
is. I think this fixes it.@JonathanAllan Yup that seems to fix it, thanks – HyperNeutrino – 2017-09-07T22:53:29.990
@Giuseppe Yup, you can – HyperNeutrino – 2017-09-07T22:53:45.647
@Shaggy ^^^^^^^ – HyperNeutrino – 2017-09-07T22:53:52.030
@icrieverytim I'll fix that; I need to do
%
which I forgot – HyperNeutrino – 2017-09-07T22:54:04.127