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

learn more… | top users | synonyms

12
votes
18answers
568 views

Run through an array

We all often hear the idiom "walk through the array" to mean "map the function over the following array". However, I need it done (now!), so I want you to run through the array. How do I run? ...
5
votes
1answer
112 views

Swap Pairs or Remove

Specification For this challenge you will: Take an array of positive integers. For each overlapping pair in the array, calculate the difference of it's integers. If the difference is a common ...
21
votes
28answers
1k views

Reverse positive runs

Input Your input is a list of single-digit numbers in any reasonable format, including a string of digits. The input will not be empty. Output Your output shall be the input list, but with each ...
14
votes
1answer
328 views

Generate a looping array

Introduction A pointer array is an array L of nonzero integers where 0 ≤ L[i]+i < len(L) holds for all indices i (assuming 0-based indexing). We say that the index i points to the index L[i]+i. ...
15
votes
14answers
2k 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 ...
12
votes
6answers
1k views

Who's the king of the tournament?

Background Consider a round-robin tournament, in which each contestant plays one game against every other contestant. There are no draws, so every game has a winner and a loser. A contestant A is a ...
13
votes
16answers
767 views

Sorting rectangular matrices

Given a matrix A, we can say that it is "sorted" if each row and column of A is sorted (ascending left-to-right for rows and top-to-bottom for columns). Thus, this matrix is sorted: [ 1 2 3 4 5 ] [ 2 ...
11
votes
9answers
1k views

A classic sorting code-golf question

This is a code-golf question. Input A list of non-negative integers in whatever format is the most convenient. Output The same list in sorted order in whatever format is the most convenient. ...
22
votes
9answers
1k views

Sort a nested list

You must write a program or function that sorts a nested list. Here are the rules for sorting a nested list: Let's take this list as an example: ((5, 2), 2, 7, (2, 1, (3, 4)), 9) Each element in ...
10
votes
5answers
595 views

Print arrays from the middle out

This is a code-golf question. Given integers s and n the task is to output all arrays of length n which take values from -s to s. The only twist is that you must output them in the following order. ...
16
votes
6answers
310 views

Hilbert-Curvify a Matrix

Inspired by this question Another way to unroll a 2D image into a 1D string is to use an Hilbert Curve. There are many version of this curve, depending on the number of iterations used while ...
16
votes
2answers
403 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 ...
33
votes
10answers
3k 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 ...
20
votes
5answers
796 views

Is this a submatrix?

This is the 2-dimensional generalisation of this challenge. For our purposes, one matrix (or 2D array) A is considered a submatrix of another matrix B, if A can be obtained by completely removing a ...
12
votes
8answers
512 views

How many steps did I walk?

A simple pedometer can be modeled by a pendulum with two switches on opposite sides—one at x=0 and one at x=l. When the pendulum contacts the far switch, the ambulator can be assumed to have taken ...
9
votes
2answers
173 views

Rotate every 2x2 block in a matrix

The Challenge Given a n x m matrix with n > 1 and m > 1 filled with integers 1 2 3 4 5 6 and a list of integers with exactly as many values as 2x2 blocks in the matrix ((n-1)*(m-1) if you ...
29
votes
34answers
3k views

Yahtzee Small Straight Detection

In the game Yahtzee, players roll five six-sided dice, and attempt to create certain hands to score points. One such hand is a small straight: four consecutive numbers, not necessarily in order. The ...
18
votes
16answers
2k views

Center of Mass from a list of coordinates and their masses

Here is a quick Monday morning challenge... Write a function or program in the least number of bytes that: Takes as input a list of [x,y] coordinates Takes as input a list of the [x,y] coordinates' ...
13
votes
2answers
169 views

Rotate every row and column in a matrix

The Challenge Given a n x n matrix of integers with n >= 2 1 2 3 4 and a list of integers with exactly 2nelements [1,2,-3,-1] output the rotated matrix. This matrix is contructed in the ...
17
votes
15answers
714 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 is mapping a range through the array, so we first have to create the ...
15
votes
9answers
428 views

Block-diagonal matrix from columns

Inspired by Copied from this question at Stack Overflow. Given a matrix A, create a matrix B such that the columns of A are arranged in a block-diagonal fashion. For example, given 1 2 3 4 5 6 the ...
-5
votes
15answers
643 views

Is the time linear?

I like patterns in time. My favorite is when the time lines up. For example, all of these line up: 3:45 12:34 23:45 3:21 6:54 This is because each digit in the hour/minute is increasing/decreasing ...
27
votes
15answers
892 views

Repair the ranges

Given an input of a list of positive integers with some replaced with 0, output the list with the missing numbers that were changed to 0 replaced. Characteristics of the input list: The list will ...
22
votes
11answers
980 views

Leaving the Nest

Given a non-flat list of integers, output a list of lists containing the integers in each nesting level, starting with the least-nested level, with the values in their original order in the input list ...
19
votes
4answers
387 views

“Finish work” as early as possible

Background Imagine for a moment that you have a mind-numbingly boring job. Every morning, you are given a collection of tasks that you should work on that day. Each task has a certain duration, and ...
18
votes
15answers
1k views

Compute minimax of an array

Inspired by this question Consider an array x such as [1 5 3 4] and a number n, for example 2. Write all length-n sliding subarrays: [1 5], [5 3], [3 4]. Let the minimax of the array be defined as ...
-2
votes
11answers
198 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: Input: th [thing, awesome, potato, example] Output: thing ...
15
votes
13answers
906 views

Get the closest value to number

In this code golf, you need to get the closest number from another one in a list. The output may be the closest number to the input. Example: value: (Input) 5 --- [1,2,3] --- 3 And, the program ...
25
votes
33answers
1k 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 leading zeroes removed. The output ...
17
votes
11answers
499 views

Maximise the squared difference

Consider a permutation of the integer values from 1 to N. E.g. this example for N = 4: [1, 3, 4, 2] We'll consider this list to be cyclic, such that 1 and 2 are treated as adjacent. One quantity we ...
31
votes
27answers
3k 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 ...
24
votes
29answers
2k 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: [1, [[2, 3, ...
11
votes
15answers
2k views

Apply Kirchhoff's law

Kirchhoff's law says that when you sum up all the currents (positive for the currents going to a junction, and negative for current leaving a junction), you will always get as result 0. Look at the ...
27
votes
33answers
3k views

Find the largest and the smallest number in an array

The Task The task is very simple. Given an array containing only integers and strings, output the largest number and the smallest number. Test Cases Input: [1, 2, 3, 4, 5, 6, 7, 8] Output: 1, 8 ...
21
votes
4answers
298 views

Magnetic pull in an array

Background I have a row of powerful magnets and a bunch of metal objects between them. Where will the magnets pull them? Input Your input is an array of non-negative integers, which will contain at ...
14
votes
3answers
322 views

Check work periods

Introduction Here in Germany the ruling on work times is very strict. If you work 6 or more hours a day, you have to take at least a 30 minute break. If you work 9 or more hours, you need to take a ...
24
votes
37answers
2k views

Consolidate an Array

The task is simple: consolidate an array of ints. Consolidating this array consists of the following: All instances of 0 need to be moved to the end of the array. There should be no 0s between the ...
15
votes
12answers
591 views

Remove specified non-numeric rows

In case there's any doubt: Nan = Non-numeric datatype for the purposes of this challenge. Write a program or function that takes a matrix / array as input, as well as a list of column indices. ...
28
votes
14answers
2k views

Show the Top Five Comment Scores on a SE Post

A Stack Exchange script determines which five comments on questions or answers are initially seen on the main page of sites through the number of upvotes on them; the five comments with the highest ...
36
votes
20answers
3k views

Remove every N-th N

The task In this challenge, your input is a non-empty list of positive integers, given in the native format of your language. Your output is that same list, in the same format, with some elements ...
24
votes
7answers
1k views

Bouncing in an array

Introduction Arrays can also be seen as a field for a bouncing ball. This of course sounds very vague, so here is an example of an input: [1, 2, 3, 4, 5, 6, 7, 8, 9] [9, 8, 7, 6, 5, 4, 3, 2, 1] [1, ...
38
votes
32answers
10k 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: ...
36
votes
13answers
4k views

Look, up in the sky! It's a super duper array!

Inspired by this question from our rivals friends over at Code Review. Definitions A super array is an array where each new element in the array is larger than the sum of all the previous elements. ...
9
votes
1answer
209 views

Sort scrambled two-dimensional array filled with numbers by swapping adjacent numbers [closed]

A two-dimensional array of size n×n is filled with n*n numbers, starting from number 1. Those numbers are to be sorted per row in ascending order; the first number of a row must be greater than the ...
18
votes
7answers
883 views

Find the capacity of 2D printed objects

In a fictional 2D world, a set of 2D printing instructions for an object can be represented by a list of integers as follows: 1 4 2 1 1 2 5 3 4 Each number represents the height of the object at ...
25
votes
22answers
1k views

Find ranges of True values in a list

Challenge: Write a function or program that accepts a list of boolean values and returns all of the ranges of True's. Test Cases: f [F] = [] f [T] ...
47
votes
9answers
3k views

Surrounded Countries

Countries own a series of territories on a 1D world. Each country is uniquely identified by a number. Ownership of the territories can be represented by a list as follows: 1 1 2 2 1 3 3 2 4 We ...
18
votes
1answer
368 views

Separate my integers

Introduction In the field of mathematics known as topology, there are things called separation axioms. Intuitively, you have a set X and a collection of subsets of X, which we can think of as ...
84
votes
15answers
3k 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 N, such that the list consisting of bi = ai XOR N is sorted. If ...
14
votes
2answers
216 views

Can the Array be Unshuffled?

Background Very skilled card handlers are capable of a technique whereby they cut a deck perfectly in half, then perfectly interleave the cards. If they start with a sorted deck and perform this ...