# Questions tagged [array]

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

785 questions
Filter by
Sorted by
Tagged with
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 ...
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. ...
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 ...
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 ...
1k views

Given a list, L, of sets of numbers like this: [, [0,2,3], [3,1], [1,2,3]] Output a single list of numbers such that 2 ...
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 ...
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 ...
3k views

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 ...
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 ...
1k views

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....
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: ...
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 ...
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 ...
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 ...
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 (...
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 ...
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 ...
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 ...
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. ...
4k views

### Shortest power set implementation

Problem definition Print out the powerset of a given set. For example: [1, 2, 3] => [[], , , , [1, 2], [1, 3], [2, 3], [1, 2, 3]] Each element is to ...
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 ...
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-...
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 ...
2k views

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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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} & {\...
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 ...
1k views

### Equal numbers in sub-array

Given an array of numbers with length >=3 and length % 3 == 0 [1, 2, 3, 4, ...] You ...
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 ...
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 ...
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 ...
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 ≥...
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 ...
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-...
2k views

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 ...
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 ...
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 ...
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 ...
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 ...
2k views

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 ...
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 ...