Questions tagged [array-manipulation]

A competition to solve a particular problem through the usage and manipulation of arrays.

Filter by
Sorted by
Tagged with
17
votes
23answers
1k views

Distinct Sieves

Given a list of integers, create a boolean mask such that the true indices can be used to filter the distinct values from the list. Which index is selected as the true one does not matter as long as ...
17
votes
2answers
647 views

Rubik-sorting a matrix (a.k.a. the torus puzzle)

The idea for this code-challenge is simple: given a matrix of integers, let's sort it by applying Rubik-style movements. This means that you can select a single row or column and rotate its elements ...
23
votes
24answers
5k views

Count the number of triangles

Given a list of positive integers, find the number of triangles we can form such that their side lengths are represented by three distinct entries of the input list. (Inspiration comes from CR.) ...
1
vote
5answers
369 views

String Array to JSON String

Introduction I've seen throughout the code-golf challenges several simple tasks involving JSON, such as interpreting JSON strings with RegExs, and almost every other manipulation I could think of. ...
19
votes
16answers
1k views

Bucket and Minimize

The challenge - given a numeric list L and an integer N as inputs, write a function that: finds the bucket sizes for L such that it is split into N whole buckets of equal or near-equal size, and ...
28
votes
29answers
2k views

Equalize the array

Challenge You are given an array \$a\$ of integers. With a move you can increase or decrease an element of the array by 1. Your task is to equalize the array, that is make all the elements of the ...
19
votes
47answers
2k views

Create an array with repeated numbers

Challenge Your task in this question is to write a program or a named function which takes a positive integer n (greater than 0) as input via STDIN, ARGV or ...
53
votes
40answers
6k views

The forbidden built-in

In the standard loopholes, the following is forbidden: Claiming that your answer is written in "MyOwnLanguage", where the command x means "read a sequence of ...
1
vote
27answers
738 views

Don't get your head in a spin!

Guidelines Task Write a function that takes in a sentence (a string, or list of characters) and reverses all the words that are greater than or equal to 5 in length. Examples ...
-3
votes
13answers
338 views

Make a search engine

You need to make a program, that when giving a list and a string, searches on the list for items that contains the string. Examples: ...
21
votes
39answers
3k views

Create chunks from an array

Your task is to write a program which given an array and a number, you need to split the array into chunks with size is number. Rules Your program will receive an array ...
28
votes
28answers
3k views

Bit-Reversal Permutations

Your goal is to create a function or a program to reverse the bits in a range of integers given an integer n. In other words, you want to find the bit-reversal permutation of a range of 2n items, zero-...
4
votes
15answers
4k views

Convert JSON string to Key / Value Arrays

Convert JSON (key/value pairs) to two native arrays, one array of keys and another of values, in your language. ...
34
votes
37answers
3k views

Determinant of an Integer Matrix

Given a square integer matrix as input, output the determinant of the matrix. Rules You may assume that all elements in the matrix, the determinant of the matrix, and the total number of elements in ...
21
votes
15answers
850 views

Cartesian LCM of two arrays

Given two nonempty arrays of natural numbers \$a\$ and \$b\$, return the shortest nonempty array of pairs of natural numbers \$q\$ such that the sequence of first elements of \$q\$ consists of a whole ...
9
votes
14answers
810 views

Arrays Start at \$\pi\$

Your task is to take an array of numbers and a real number and return the value at that point in the array. Arrays start at \$\pi\$ and are counted in \$\pi\$ intervals. Thing is, we're actually going ...
31
votes
22answers
3k views

The Three 'R's: Reverse, Reorder, Repeat

While doodling around with numbers, I found an interesting permutation you can generate from a list of numbers. If you repeat this same permutation enough times, you will always end up back at the ...
-2
votes
7answers
325 views

Remove Duplicates From One List

Task: Remove all duplicates from one list of integers. A list is simply a sequence of connected values that allows the same values to be stored at different positions in this sequence. If an item ...
21
votes
18answers
2k views

Score a Game of Boggle

Background In Boggle, a round is scored by adding up the points for each unique word a player has found (i.e. any word that more than one player has found is worth 0 points). The points are ...
62
votes
62answers
8k views

Lossy Sorting (Implement Dropsort)

Dropsort, designed by David Morgan-Mar, is an example of a linear-time "sorting algorithm" that produces a list that is, in fact, sorted, but contains only some of the original elements. Any element ...
15
votes
17answers
921 views

Alternatively shift columns and rows of a 2D array

Objective Given a 2D array of any size, write a program or function to shift alternatively the columns and rows Example a b c d e f g h i j k l m n o All ...
35
votes
64answers
6k views

P Pr Pre Pref Prefi Prefix Prefixe Prefixes

Given some finite list, return a list of all its prefixes, including an empty list, in ascending order of their length. (Basically implementing the Haskell function ...
40
votes
32answers
3k views

Remove surrounding zeroes of a 2d array

This is a 2-dimensional version of this question. Given a non-empty 2-dimensional array/matrix containing only non-negative integers: $$ \begin{bmatrix} {\color{Red}0} & {\color{Red}0} & {\...
18
votes
18answers
2k views

Clockwise matrix rotation

The title pretty much describes it all. Given as input a \$n \times m\$ matrix and an integer \$i\$ create a complete function/program that returns the matrix \$i\$ times clockwise rotated by \$90^\...
82
votes
43answers
7k views

2048-like array shift

Assume we want to shift an array like it is done in the 2048 game: if we have two equal consecutive elements in array, merge them into twice the value element. Shift must return a new array, where ...
2
votes
3answers
189 views

Simulate move left for 2048 [duplicate]

I have a fixed length vector of 4 elements. The inputs of which are always 0 or 2^n for n = 1 to 15 (so n = 0 excluded i.e. 2^0 = 1 will not appear) ...
27
votes
27answers
3k views

Print a Block-Diagonal Matrix

Here is a simple, bite-sized (byte-sized?) code golf: given a non-empty list of positive integers less than 10, print a block-diagonal matrix, where the list specifies the size of the blocks, in order....
11
votes
6answers
917 views

Group Integers by Originality

Introduction: I collect twisty puzzles. Most twisty puzzles are produced and sold by Chinese companies. Most well-known companies ask permission from puzzle designers to produce their designs and ...
17
votes
14answers
2k views

Unique magic triplets

Description Your input is a list of integers \$[a_1, \dots, a_n]\$. A magic triplet is a triplet \$(a_i, a_j, a_k)\$ of values from this list, such that the indices \$\{i, j, k\}\$ are all distinct,...
10
votes
8answers
370 views

1D Shikaku Validation

Shikaku is a 2D puzzle. The basic rundown of it is that a rectangular grid has some numbers in it, and you want to partition the grid into rectangular components such that each component contains ...
26
votes
29answers
2k views

Rotation summation

Take a square matrix containing positive integers as input, and calculate the "rotated sum" of the matrix. Rotated sum: Take the sum of the original matrix and the same matrix rotated 90, 180 and ...
0
votes
1answer
128 views

Shooting gallery Puzzle!

Have you been shooting gallery? We are recently. In our shooting gallery cans and aluminum cans from under various drinks hang and stand. More precisely, they hung and stood. From our shots, banks ...
10
votes
6answers
745 views

Consistent Overhead Byte Stuffing (COBS)

I'm surprised this hasn't been posted before! The Consistent Overhead Byte Stuffing (COBS) algorithm is used to delimit byte streams. We choose a frame marker (we'll use 0x00) and wherever 0x00 ...
15
votes
43answers
3k views

Find the percentage

We haven't had any nice, easy challenges in a while, so here we go. Given a list of integers each greater than \$0\$ and an index as input, output the percentage of the item at the given index of the ...
1
vote
1answer
142 views

Minimum number of changes required [closed]

Given an array where each number represent a color. After iterating each item in the array (for each item in the iteration the pointer can also point to the second-item-to-the-right), and the two ...
11
votes
18answers
2k views

It's as easy as 1,2,3 (except you have to add them up)

Let's consider a list \$L\$ (initially empty) and a pointer \$p\$ into this list (initialized to \$0\$). Given a pair of integers \$(m,n)\$, with \$m\ge 0\$ and \$n>0\$: We set all uninitialized ...
22
votes
42answers
3k views

Shortest power set implementation

Problem definition Print out the powerset of a given set. For example: [1, 2, 3] => [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]] Each element is to ...
60
votes
18answers
4k views

There are two new sheriffs in town – Identifying DJMcMego pairs!

We have some new sheriffs moderators in town, Mego and DJMcMayhem. We need a challenge to properly honour them for their new positions, so there we go. Here's something that has caught my attention ...
-19
votes
29answers
2k views

Reverse a 1-dimensional array

Note: This challenge is not the same. Challenge Believe it or not, we haven't got ONE challenge for reversing one-dimensional arrays (although we've got one for n-dimensional ones)! This should ...
5
votes
6answers
274 views

Explore a Klarner-Rado sequence [duplicate]

One of the Klarner-Rado sequences is defined as follows: the first term is \$1\$ for all subsequent terms, the following rule applies: if \$x\$ is present, so are \$2x+1\$ and \$3x+1\$ the sequence ...
41
votes
39answers
3k views

An Array of Challenges #1: Alternating Arrays

Alternating Arrays An alternating array is a list of any length in which two (not necessarily different) values are alternating. That is to say, all even-indexed items are equal, and all odd-indexed ...
18
votes
24answers
2k views

Check my tunneling arrays

Imagine you have an array of integers, whose non-negative values are pointers to other positions in the same array, only that those values represent tunnels, so if the value in position A is positive ...
4
votes
7answers
189 views

Replace by most recent nonzero [duplicate]

Given a non-empty list of decimal digits (0, 1, ..., 9), replace each zero by the most ...
41
votes
42answers
2k views

Cover up zeroes in a list

Inspired by this SO question As input you will be given a non-empty list of integers, where the first value is guaranteed to be non-zero. To construct the output, walk from the start of the list, ...
45
votes
18answers
6k views

Into how many pieces can you cut this string?

Consider a piece of string (as in "rope", not as in "a bunch of characters"), which is folded back and forth on the real line. We can describe the shape of the string with a list of points it passes ...
3
votes
1answer
179 views

Highest cardinality grouping of contiguous integers with minimum-sum threshold

Challenge Given an array of positive integers and a threshold, the algorithm should output a set of consecutive-element-groupings (subarrays) such that each group/subarray has a sum greater than the ...
17
votes
23answers
1k views

Enumerate Derangements

Given some positive integer \$n\$ generate all derangements of \$n\$ objects. Details A derangement is a permutation with no fixed point. (This means in every derangement number \$i\$ cannot be in ...
-6
votes
11answers
209 views

Return the nth element of an array even if its out of bound [closed]

The challenge Write a function that returns the nth element of an array even if its out of bound. ...
28
votes
19answers
2k views

Rotate a column

Given an integer k and either a block of text, or a 2d array that the inner arrays may have unequal lengths (that resembles a block of text), rotate every character or element in the k-th column up or ...
34
votes
11answers
1k views

There's an echo in my array… echo in my array… my array

Help! I seem to have an annoying echo in some of my arrays, and I'd like to get rid of it. When this occurs, the original array repeats itself somewhere in the middle causing the values to be added ...

1 2 3 4 5 11