Implement Array#transpose!
Description:
###RELATED: https://www.codewars.com/kata/implement-string-number-transpose
Many Ruby methods come with bang variants (variants that modify the input). For example, Array#uniq!
is the bang variant of Array#uniq
.
arr = [1, 2, 1, 5, 6]
arr.uniq # returns [1, 2, 5, 6] but arr is still [1, 2, 1, 5, 6]
arr.uniq! # returns [1, 2, 5, 6]
arr # returns [1, 2, 5, 6] as arr was modified with uniq! and not uniq
####Task:
Your job here is to implement Array#transpose!
(which will be the bang variant of Array#transpose
). This function returns the array received from transposing the rows and columns in a twodimensional array, and raises an error if the lengths of the subarrays don't match, or if the array is not twodimensional. The function you create should be able to handle sub-arrays with mixed types. For more info on Array#transpose
, see the Ruby array docs: https://ruby-doc.org/core-2.3.0/Array.html.
####Examples (see example test cases for more):
arr = [[1,2,7],[3,5,6]]
arr.transpose # original method; returns [[1, 3], [2, 5], [7, 6]] but doesn't change arr
arr.transpose! # your method; returns [[1, 3], [2, 5], [7, 6]] and changes arr to [[1, 3], [2, 5], [7, 6]] too
arr = [[1,2,7,8],[3,5,6]]
arr.transpose! # raises an error as subarray lengths don't match
arr = [1,2,3]
arr.transpose! # raises an error as input array is not twodimensional
####Cases you won't get:
[[[1]]].transpose!
[1,[1,2]].transpose!
[[1,2],1,[1,7]].transpose!
[[[1,2,[3,4]]]].transpose!
In other words, you won't get arrays with both arrays and non-arrays, or more-than-twodimensional arrays.
I welcome your feedback! Enjoy!
Similar Kata:
Stats:
Created | Oct 16, 2016 |
Published | Oct 16, 2016 |
Warriors Trained | 224 |
Total Skips | 17 |
Total Code Submissions | 473 |
Total Times Completed | 81 |
Ruby Completions | 81 |
Total Stars | 4 |
% of votes with a positive feedback rating | 86% of 37 |
Total "Very Satisfied" Votes | 30 |
Total "Somewhat Satisfied" Votes | 4 |
Total "Not Satisfied" Votes | 3 |
Total Rank Assessments | 5 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 6 kyu |
Lowest Assessed Rank | 7 kyu |