Fold an array
Description:
In this kata you have to write a method that folds a given array of integers by the middle x-times.
An example says more than thousand words:
Fold 1-times:
[1,2,3,4,5] -> [6,6,3]
A little visualization (NOT for the algorithm but for the idea of folding):
Step 1 Step 2 Step 3 Step 4 Step5
5/ 5| 5\
4/ 4| 4\
1 2 3 4 5 1 2 3/ 1 2 3| 1 2 3\ 6 6 3
----*---- ----* ----* ----* ----*
Fold 2-times:
[1,2,3,4,5] -> [9,6]
As you see, if the count of numbers is odd, the middle number will stay. Otherwise the fold-point is between the middle-numbers, so all numbers would be added in a way.
The array will always contain numbers and will never be null. The parameter runs will always be a positive integer greater than 0 and says how many runs of folding your method has to do.
If an array with one element is folded, it stays as the same array.
The input array should not be modified!
Have fun coding it and please don't forget to vote and rank this kata! :-)
I have created other katas. Have a look if you like coding and challenges.
Similar Kata:
Stats:
Created | Sep 27, 2016 |
Published | Sep 27, 2016 |
Warriors Trained | 31054 |
Total Skips | 8073 |
Total Code Submissions | 52903 |
Total Times Completed | 11407 |
C# Completions | 805 |
C++ Completions | 2015 |
Java Completions | 1940 |
F# Completions | 85 |
JavaScript Completions | 2560 |
TypeScript Completions | 757 |
Python Completions | 2144 |
CoffeeScript Completions | 15 |
Haskell Completions | 259 |
Ruby Completions | 329 |
Swift Completions | 507 |
Rust Completions | 160 |
COBOL Completions | 3 |
Go Completions | 99 |
C Completions | 47 |
Total Stars | 583 |
% of votes with a positive feedback rating | 93% of 1872 |
Total "Very Satisfied" Votes | 1628 |
Total "Somewhat Satisfied" Votes | 223 |
Total "Not Satisfied" Votes | 21 |
Total Rank Assessments | 12 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 7 kyu |