Questions tagged [array]

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

Filter by
Sorted by
Tagged with
35 votes
35 answers
7k views

Random Golf of the Day #1: Shuffle an Array

About the Series I will be running a little series of code-golf challenges revolving around the theme of randomness. This will basically be a 9-Hole Golf Course, but spread out over several questions....
Martin Ender's user avatar
39 votes
54 answers
4k views

Boustrophedonise

Related but very different. A boustrophedon is a text where every other line of writing is flipped or reversed, with reversed letters. In this challenge, we will just reverse every other line, but ...
Adám's user avatar
  • 30.1k
39 votes
53 answers
11k views

Determine the depth of an array

A simple challenge for your Monday evening (well, or Tuesday morning in the other half of the world...) You're given as input a nested, potentially ragged array of positive integers: ...
Martin Ender's user avatar
68 votes
61 answers
7k views

N-dimensional N^N array filled with N

In: Enough memory and a positive integer N Out: N-dimensional N^N array filled with N, where N^N means N terms of N-by-N-by-N-by... Examples: 1: [1] ...
Adám's user avatar
  • 30.1k
26 votes
31 answers
3k views

Different Way Forward

Given a list of integers produce a Forward Difference at a specified order/depth. For the list of integers: (10, 18, -12, 4, 8, -3, -5, 67, 9, 14) The Forward ...
MickyT's user avatar
  • 12.2k
48 votes
16 answers
6k views

Zigzagify a Matrix

As part of its compression algorithm, the JPEG standard unrolls a matrix into a vector along antidiagonals of alternating direction: Your task is to take a matrix (not necessarily square) and return ...
Martin Ender's user avatar
37 votes
39 answers
4k 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 ...
user avatar
28 votes
47 answers
4k 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 ...
beatgammit's user avatar
19 votes
1 answer
721 views

Advent Challenge 1: Help Santa unlock his present vault!

Next >> Descriptive Keywords (for searching): Make Two Matrices Equivalent, Overlap, Array, Find Challenge Santa has had a history of elves stealing presents from his vault in the past, so this ...
hyper-neutrino's user avatar
  • 41.4k
11 votes
25 answers
3k views

Cartesian product of a list with itself n times

When given a a list of values and a positive integer n, your code should output the cartesian product of the list with itself n ...
JoshM's user avatar
  • 447
69 votes
62 answers
4k views

Count trailing truths

Inspired by, and in memory of, my dear friend and colleague, Dan Baronet, 1956 – 2016. R.I.P. He found the shortest possible APL solution to this task: Task Given a Boolean list, count the number of ...
Adám's user avatar
  • 30.1k
32 votes
46 answers
8k views

Mode (most common element) of a list

Write a snippet to calculate the mode (most common number) of a list of positive integers. For example, the mode of d = [4,3,1,0,6,1,6,4,4,0,3,1,7,7,3,4,1,1,2,8] ...
garg10may's user avatar
  • 709
24 votes
26 answers
3k views

Diamondize a Matrix

Given a matrix, output a representation of the matrix where the top left element is on top, the anti-diagonal is the central row and the bottom right element is at the bottom. For example, consider ...
Fatalize's user avatar
  • 38.5k
21 votes
16 answers
2k views

Discrete Convolution or Polynomial Multiplication

Given two non empty lists of integers, your submission should calculate and return the discrete convolution of the two. Interestingly, if you consider the list elements as coefficients of polynomials, ...
flawr's user avatar
  • 43.8k
17 votes
105 answers
8k views

Turn an integer n into a list containing it n times [closed]

Given an integer n as input, return a list containing n, repeated n times. For example, the ...
Bruno E's user avatar
  • 425
42 votes
45 answers
4k views

Skip like a rabbit!

Given a list of non-negative integers in any reasonable format, iterate over it, skipping as many elements as every integer you step on says. Here is a worked example: ...
Erik the Outgolfer's user avatar
34 votes
9 answers
3k views

Rotate the anti-diagonals

Background In most reasonable programming languages, it's very easy to rotate the rows or columns of a 2D array. In this challenge, your task is to rotate the anti-diagonals instead. Recall that the ...
Zgarb's user avatar
  • 42.6k
29 votes
30 answers
4k 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 \$2^n\$ ...
miles's user avatar
  • 16.9k
26 votes
60 answers
4k views

Non-Unique/Duplicate Elements

Write a program which finds the non-unique elements of an array of signed integers. The resulting array can be in any order. Your answer may be a snippet which assumes the input to be stored in a ...
garg10may's user avatar
  • 709
21 votes
20 answers
1k views

Fluctuating ranges

Given a list with number, output the ranges like this: Input: [0, 5, 0] would become [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]. This ...
Lamaro's user avatar
  • 949
68 votes
69 answers
10k 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 ...
SuperJedi224's user avatar
  • 11.9k
39 votes
58 answers
3k views

Fold a List in Half

We are going to fold a list of integers. The procedure to do so is as follows, If the list is of even length, make a list of half of its length where the nth item of the new list is the sum of the ...
Wheat Wizard's user avatar
  • 97.8k
29 votes
26 answers
5k views

Index of a multidimensional array

Lower level languages, such as C and C++ actually have no concept of multidimensional arrays. (Other than vectors and dynamic arrays) When you create a multidimensional array with ...
DJMcMayhem's user avatar
26 votes
29 answers
2k views

Ordering a list

Summary Given a list of integers, return the index each integer would end up at when sorted. For example, if the list was [0,8,-1,5,8], you should return ...
Nathan Merrill's user avatar
24 votes
4 answers
970 views

Reconstruct a zigzagified matrix

As part of its compression algorithm, the JPEG standard unrolls a matrix into a vector along antidiagonals of alternating direction: Your task is to take the unrolled vector along with the matrix ...
Martin Ender's user avatar
20 votes
19 answers
2k views

Counting in bijective base 62

The task is to generate all the strings from 'a' to '999' including upper case characters like so: ...
Simon Landry's user avatar
17 votes
14 answers
2k views

We do tower hopping

Task Given an array of non-negative integers a, determine the minimum number of rightward jumps required to jump "outside" the array, starting at position ...
0x45's user avatar
  • 800
16 votes
7 answers
3k views

Flood-fill a 2D grid

Challenge description Let's call a two-dimentional, rectangular array (meaning its every subarray has the same length), a grid. Every unit of a grid is either an empty space or a border. In a grid of ...
shooqie's user avatar
  • 5,642
15 votes
3 answers
2k views

How to swap elements in a vector using an anonymous function in Octave?

Swapping two elements in a vector/matrix is very simple in Octave: x='abcde'; x([4,1])=x([1,4]) x = dbcae Unfortunately, I have yet to find a way to do this ...
Stewie Griffin's user avatar
11 votes
17 answers
1k views

Operations with Lists

Inspired by this question. Given a list containing numbers, print: The sum and product of the numbers in the list The average and median The differences between each term in the list (e.g. ...
beary605's user avatar
  • 4,310
50 votes
61 answers
3k 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, ...
FryAmTheEggman's user avatar
48 votes
25 answers
6k views

First code golf decathlon [closed]

Tasks All competitors try to solve the following list of 10 tasks: math Read a positive integer n from input and return the sum of the cubes of the first n non-negative integers. For input ...
Dennis's user avatar
  • 210k
41 votes
34 answers
13k views

Cheating a multiple choice test

Introduction While studying, I tried to come up with several ways to cheat a multiple choice test. It basically is a compressed version of the multiple choice answers. The method goes as following: ...
Adnan's user avatar
  • 44.3k
35 votes
33 answers
4k views

Hollow out an array

The task This challenge is very simple. Your input is a rectangular 2D array of integers, sized at least 1×1. It can be taken in any reasonable format. Your output shall be the input array, but ...
Zgarb's user avatar
  • 42.6k
32 votes
36 answers
3k views

Swap the two given indices [duplicate]

Given an array of positive integers and two distinct valid indices, return the array with the two elements corresponding to the two indices swapped. You may choose to use 0-indexing or 1-indexing, ...
Leaky Nun's user avatar
  • 49.7k
31 votes
40 answers
3k views

Iterated partial sums

The partial sums of a list of integers [a1, a2, a3, ..., an] are s1 = a1 s2 = a1 + a2 s3 = a1 + a2 + a3 ... sn = a1 + a2 + ... + an We can then take the list of partial sums [s1, s2, s3, ..., sn] ...
xnor's user avatar
  • 145k
31 votes
33 answers
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....
Martin Ender's user avatar
30 votes
49 answers
3k views

Swap Two Values in a List

Introduction: Although we have a lot of challenges where swapping two items in a list is a subtask, like Single swaps of an array; Swap to Sort an Array; \$n\$ swaps into a nop; etc., we don't have ...
Kevin Cruijssen's user avatar
30 votes
36 answers
3k views

Find the unique twins

You will be given two Arrays / Lists / Vectors of non-negative integers A and B. Your task is to output the highest integer N that appears in both A and B, and is also unique in both A and B. You ...
Mr. Xcoder's user avatar
  • 42.4k
29 votes
13 answers
5k views

Lifetime of a Worm

Terms A worm is any list of nonnegative integers, and its rightmost (i.e., last) element is called the head. If the head is not 0, the worm has an active segment consisting of the longest contiguous ...
r.e.s.'s user avatar
  • 3,112
27 votes
16 answers
3k views

All the single eights

Given a non-empty rectangular array of integers from 0 to 9, output the amount of cells that are ...
Luis Mendo's user avatar
  • 104k
19 votes
27 answers
2k views

Is it a tower permutation?

A tower is made out of layers, each one being one unit shorter than the one below it. Every layer is completely on top of the previous layer. For example, here is a tower along with it's height map: <...
AnttiP's user avatar
  • 7,848
9 votes
16 answers
984 views

Rank a list of scores with "skips"

Given a list of scores (non-negative integers) pre-sorted from greatest to least: [ 10, 10, 6, 6, 4, 0] Assign each score an integer rank, beginning with 1 ...
Jordan's user avatar
  • 9,864
109 votes
18 answers
6k views

Xorting an array

Conceptually, this challenge is really simple. You're given a list of non-negative integers ai. If possible, find a non-negative integer ...
Martin Ender's user avatar
101 votes
51 answers
8k 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 ...
greenwolf's user avatar
  • 1,051
51 votes
92 answers
6k views

The shortest way to find one unique value when all other values are the same

In this question at Code Review they tried to find the fastest way to return the unique element in an array where all the elements are the same except one. But what is the shortest code that ...
Charlie's user avatar
  • 12.9k
47 votes
69 answers
8k views

Am I an insignificant array?

An insignificant array is an array of positive integers, where the absolute differences between consecutive elements are all smaller than or equal to 1. For example, the following array is ...
user avatar
43 votes
46 answers
4k 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 ...
FlipTack's user avatar
  • 14.6k
42 votes
62 answers
9k views

Remove leading and trailing zeroes

Given a non-empty list/array containing only non-negative integers like this: [0, 0, 0, 8, 1, 4, 3, 5, 6, 4, 1, 2, 0, 0, 0, 0] Output the list with trailing and ...
Lamaro's user avatar
  • 949
42 votes
48 answers
6k views

Is it true? Ask Jelly!

Background Inspired by Octave's (and, by extension, MATL's) very convenient interpretation of truthy/falsy matrices, Jelly got the Ȧ (Octave-style all) atom. Ȧ takes an array as input and returns 1 ...
Dennis's user avatar
  • 210k

1
2 3 4 5 6