A competition to solve a particular problem through the usage and manipulation of arrays.
13
votes
20answers
2k views
Flatten the Array!
In this challenge, your task is to create a program which takes in a nested array and returns a single-dimensional flattened array. For Example [10,20,[30,[40]],50] should output [10,20,30,40,50].
...
10
votes
11answers
655 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, ...
6
votes
6answers
168 views
Find how many pair elements sums up to N [closed]
Based on a recent question in StackOverflow
Write a program/function that takes in one ARRAY/list and returns the number of pair-values which sums up to a given TOTAL.
Explaining
TOTAL=4, ...
21
votes
20answers
2k 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
int foo[5][10];
...
13
votes
8answers
521 views
Find a maximal rectangle of 1s
Background
I want to buy an plot of land and build my house on it.
My house should be rectangular, and as large as possible; however, the available plots have lots of rocky areas that I cannot build ...
17
votes
15answers
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:
'a', 'b', 'c' ... 'y', 'z', 'A', 'B', 'C' ... 'Y', 'Z', '0', '1', 2' ...
'8', '9', 'aa', 'ab', 'ac' ...
-20
votes
15answers
262 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 ...
13
votes
11answers
559 views
Smallest groups in an array
Introduction
Let's observe the following array:
[1, 1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1]
A group consists of the same digits next to each other. In the above array, there are 5 different ...
13
votes
18answers
700 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
116 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
27answers
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
329 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.
...
16
votes
15answers
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
772 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
597 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
320 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
408 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
801 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
513 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
178 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
170 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
720 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
429 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
647 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
895 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
985 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
391 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 ...
-3
votes
12answers
246 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
919 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
302 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
323 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
597 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:
...