6 kyu

Fold an array

2,144 of 11,407user5036852

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.

Fundamentals
Logic
Mathematics
Algorithms

Stats:

CreatedSep 27, 2016
PublishedSep 27, 2016
Warriors Trained31054
Total Skips8073
Total Code Submissions52903
Total Times Completed11407
C# Completions805
C++ Completions2015
Java Completions1940
F# Completions85
JavaScript Completions2560
TypeScript Completions757
Python Completions2144
CoffeeScript Completions15
Haskell Completions259
Ruby Completions329
Swift Completions507
Rust Completions160
COBOL Completions3
Go Completions99
C Completions47
Total Stars583
% of votes with a positive feedback rating93% of 1872
Total "Very Satisfied" Votes1628
Total "Somewhat Satisfied" Votes223
Total "Not Satisfied" Votes21
Total Rank Assessments12
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • user5036852 Avatar
  • jhoffner Avatar
  • suic Avatar
  • Dentzil Avatar
  • imjasonmiller Avatar
  • kazk Avatar
  • JorgeVS Avatar
  • wneumann Avatar
  • Voile Avatar
  • FArekkusu Avatar
  • hobovsky Avatar
  • Meisterschueler Avatar
  • trashy_incel Avatar
  • kirull Avatar
  • akar-0 Avatar
  • mauro-1 Avatar
Ad