Questions tagged [array]
A competition to solve a particular problem through the usage and manipulation of arrays.
785
questions
38
votes
56
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 ...
25
votes
81
answers
7k
views
Invert a boolean array
A nice simple one
Input
Given a boolean array (Or an acceptable alternative), you can assume the array will never be more than 32 elements long.
...
36
votes
89
answers
4k
views
Implement Takewhile
Introduction and Credit
Today without a fancy prelude: Please implement takewhile.
A variation of this (on a non-trivial data structure) was an assignment at my ...
16
votes
4
answers
568
views
Complete the landscape
Carcassonne is a tile-based game, where the objective is to construct Roads, Cities and Monasteries, in order to score points. The game works by players taking turns to draw and place tiles to ...
13
votes
9
answers
1k
views
Adjacent Items Sorting
Given a list, L, of sets of numbers like this:
[[1], [0,2,3], [3,1], [1,2,3]]
Output a single list of numbers such that 2 ...
8
votes
32
answers
1k
views
Keep elements in sequence that have a letter repeated at least 3 times
Challenge:
Given the input array l with a list of strings, only keep the elements in the sequence that have a letter that's repeated at least 3 times. Like ...
20
votes
29
answers
2k
views
Alternating sums of multidimensional arrays
Given a multidimensional array, find the recursive alternating sum. An alternating sum is simply the sum of an array, where every other item (starting with the second) is negated. For example, the ...
18
votes
20
answers
3k
views
cadaddadadaddddaddddddr - linked list accessing
Create a function (or closest equivalent, or full program) that takes an list of some datatype (your choice) that may be nested and a string (in either order), and generalizes the lisp c[ad]+r ...
47
votes
82
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 ...
24
votes
17
answers
1k
views
Counting fading
Background
Being in a philosophical mood, I remembered one old idea. I think it is suitable for CG.
Duplicates are not found, but it's of course just a kind of totalistic cellular automaton or erosion....
57
votes
52
answers
6k
views
Increment an Array
Given a nonempty array of positive integers, "increment" it once as follows:
If all the array elements are equal, append a 1 to the end of the array. For example:
...
36
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 ...
17
votes
32
answers
2k
views
The Jaccard Index
The Jaccard index / similarity coefficient, also known as the Tanimoto index / coefficient, is a statistic used for gauging the similarity and diversity of finite sample sets. It was developed by ...
98
votes
49
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 ...
10
votes
39
answers
2k
views
Counting Letters in a String
Write a program or function that takes in a string and outputs a count of each modern English alphabet letter in the string, case-insensitive.
Input: A string consisting of printable ASCII characters (...
12
votes
17
answers
2k
views
Compute cumulative means efficiently
Consider a sorted array of positive floating point numbers such as:
input = [0.22, 2.88, 6.35, 7.17, 9.15]
For each integer \$i\$ from 1 up to the last value in ...
21
votes
10
answers
628
views
Split a list into maximal equal-sum sections
Task
Given a non-empty list of positive digits, group them into sublists of equal sum. You may output any subset of solutions with maximal number of sublists (equivalently, minimal sum).
The input may ...
18
votes
24
answers
1k
views
Remove first encountered elements from a second list
Inspired by this question on Stackoverflow.
The task
Let's have two lists/arrays of integers: L1 and L2 of equal length. You ...
13
votes
33
answers
653
views
Create \$n\$ sublists with the powers of two (1, 2, 4, 8, 16...)
Challenge:
Given the input number n. It should give me nested sublists of n layers with the power of two numbers for each level. ...
27
votes
45
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 ...
45
votes
68
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 ...
36
votes
63
answers
3k
views
Is this a Permutation of 1..n
Input a non-empty array with \$n\$ positive integers. Test if the input array contains every integer in \$1\cdots n\$.
In case you prefer 0-indexed numbers, you may choose to input an array of non-...
19
votes
17
answers
1k
views
Smallest slice of array containing a number of items
Take this array:
[1, 2, 6, 4, 4, 1, 0, 0, 2, 0, 4, 1, 4, 2, 4, 8, 1, 2, 4]
There are quite a few places where ...
17
votes
8
answers
2k
views
Scribble Pad for Nerds
aka. implement an easier version of 05ab1e's canvas element.
Description
The canvas element is used to draw ASCII lines on the screen.
The (easier version of the) canvas element takes a list of ...
16
votes
6
answers
862
views
"Signpost" puzzle from Tatham's collection
Simplified version of "Signpost" puzzle from Simon Tatham's Portable Puzzle Collection.
How the puzzle is created
We pick up randomly positive integer N ...
42
votes
61
answers
8k
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 ...
25
votes
19
answers
2k
views
Double trace of a square matrix
Inspired by a question (now closed) at Stack Overflow.
Given a square matrix, let its double trace be defined as the sum of the entries from its main diagonal and its anti-diagonal. These are marked ...
23
votes
26
answers
1k
views
Pairs at every distance
Given a list \$X\$ of 2 or more integers, output whether, for all \$n\$ such that \$0 \leq n < length(X)-2\$, there exists a pair of equal integers in \$X\$ separated by exactly \$n\$ elements.
In ...
5
votes
9
answers
614
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.
In ...
9
votes
11
answers
709
views
Tuples by sequentially stepping through entries in list of lists
Challenge:
Given a list of nonempty lists of integers, return a list of tuples of the following form: First list tuples starting with each element of the first list followed by the first element of ...
41
votes
33
answers
4k
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} & {\...
31
votes
40
answers
3k
views
Most Common Multiple
Not to be confused with Least Common Multiple.
Given a list of positive integers with more than one element, return the most common product of two elements in the array.
For example, the MCM of the ...
20
votes
44
answers
1k
views
Equal numbers in sub-array
Given an array of numbers with length >=3 and length % 3 == 0
[1, 2, 3, 4, ...]
You ...
22
votes
37
answers
4k
views
Am I a golfy array?
Definition and Rules
A golfy array is an array of integers, where each element is higher than or equal to the arithmetic mean of all the previous elements. Your task is to determine whether an array ...
13
votes
7
answers
752
views
Guessing on straws
Dedicated to Martin Gardner, taken from his book
Background
In the old days, the Slavs had a divination method for finding out whether a girl would get married. The girl would clutch six straws in her ...
26
votes
27
answers
2k
views
Hunt for discount
Story
My local pizza delivery introduced new discount. You get 50% discount from every second item on your order.
But being greedy capitalists, they forgot to mention that they will rearrange items ...
52
votes
46
answers
5k
views
Detect heat waves
Background
The Royal Netherlands Meteorological Institute defines a heat wave* as a series of at least 5 consecutive days of ≥25°C weather (“summery weather”), such that at least 3 of those days are ≥...
14
votes
13
answers
1k
views
Make a Custom Bayer Matrix
A Bayer matrix is a threshold map used for ordered dithering that gives the illusion of having more shades of color than actually present by using a crosshatch-like pattern.
Bayer matrices are square ...
16
votes
19
answers
1k
views
Damerau-Damerau distance
Given a list of the integers from \$1\$ to some \$n\$, give the minimum number of adjacent swaps required to put the list in ascending order.
You will receive a list as input and should output a non-...
21
votes
29
answers
2k
views
Cosine similarity of two vectors
The cosine similarity of two vectors \$A\$ and \$B\$ is defined using their dot product and magnitude as:
\$\frac{A\cdot B}{\|A\|\|B\|}\$
Or in other terms
\$\frac{\sum_{i=1}^nA_iB_i}{\sqrt{\sum_{i=1}^...
15
votes
17
answers
2k
views
How long to carry sort?
Carry sort is an \$O(n)\$ "sorting" algorithm. Here's how it works. The algorithm moves left to right along a list. As it traverses a list it "carries" a single item, the largest ...
28
votes
13
answers
3k
views
Is it a valid chemical?
Non-metals typically* have a fixed number of covalent bonds in every chemical they are part of. Given the number of bonds every element requires, output whether it's possible to construct a single ...
29
votes
17
answers
3k
views
Is it traversable?
Imagine that a list of integers describes the heights of some two-dimensional terrain as seen from the side.
Stamina: [ 4 4 4 4 4 4 3 3 3 3 2 2 2 - ]
O
...
66
votes
67
answers
9k
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 ...
6
votes
24
answers
455
views
String with the smallest sum of ASCII values
Description
In this challenge, you need to write a function that takes in a list of strings and returns the smallest string based on their ASCII value. You should assume that the input list contains ...
18
votes
19
answers
2k
views
The tedious array of 7 rules
The rundown
Create a program that generates an array of random length with random numbers, then apply a series of rules that alter the array. After the rules have been applied, print the sum of the ...
25
votes
11
answers
2k
views
Blinking Lighthouses
Lighthouses on an island have blink codes that let ships know which lighthouse they are seeing. Input should be strings of S and L. Here is a (Python) example of codes for 4 lighthouses as a list of ...
14
votes
34
answers
1k
views
All non-ordered pairs between the elements of an array
Task:
Return an array with all possible pairs between the elements of an array.
Example
From a=["a", "b", "c", "d"]; return ...
22
votes
38
answers
1k
views
Find the maximum deviation
This problem is "inspired" from a question that was originally asked on Quora (not for code golfing). I just want to make it a challenge for you guys (and my first problem submission here).
Given an ...
28
votes
36
answers
2k
views
Running second maximum of a list
Given a list of integers, your task is to output the second largest value in the first k elements, for each k between 2 and the length of the input list.
In other words, output the second largest ...