Questions tagged [array]

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

Filter by
Sorted by
Tagged with
11 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 ...
Simd's user avatar
  • 379
16 votes
6 answers
858 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 ...
lesobrod's user avatar
  • 3,001
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....
lesobrod's user avatar
  • 3,001
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 ...
solid.py's user avatar
  • 1,527
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}^...
emirps's user avatar
  • 1,666
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 ...
Yousername's user avatar
  • 3,100
5 votes
2 answers
353 views

Shortening this Code to process nested tuple even further without the use of max()

I have the code: ...
Dark Programmer's user avatar
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 ...
Wheat Wizard's user avatar
  • 94.5k
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 ...
mousetail's user avatar
  • 9,204
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 ...
chunes's user avatar
  • 18.1k
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 ...
lesobrod's user avatar
  • 3,001
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 (...
Aitzaz Imtiaz's user avatar
6 votes
24 answers
448 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 ...
Aitzaz Imtiaz's user avatar
10 votes
3 answers
576 views

Voronoi-Lloyd ASCII art [closed]

Voronoi diagram is a partition of a plane (or part of plane) into regions close to each of a given set of objects ("seeds"). Here we’ll be dealing with discrete arrays or even rather with ...
lesobrod's user avatar
  • 3,001
26 votes
47 answers
3k views

Shortest Code to Find the Smallest Missing Positive Integer

Description Given an unsorted array of integers, find the smallest positive integer that does not appear in the array. Your task is to write the shortest code possible to solve this problem. Input A ...
Aitzaz Imtiaz's user avatar
12 votes
6 answers
601 views

Number Clusters

Your task is to create a program or function, that when given an input list of nonnegative integers of length \$l \ge 2\$ and a nonnegative integer \$c\$ where \$2 \le c \le l\$, group the list into \$...
Yousername's user avatar
  • 3,100
7 votes
18 answers
501 views

Find the largest sum of any contiguous subarray of the array [duplicate]

Let's say you are given an integer array. Create a function to determine the largest sum of any array's adjacent subarrays. For instance, the contiguous subarray with the largest sum is ...
user avatar
11 votes
26 answers
2k views

Arbitrary Apple Dilemma

I have encountered this type of puzzle, that usually involves apples, several times. It goes as follows: There is a certain number of apples in a basket. Ava takes a third of the apples. Bob takes a ...
math scat's user avatar
  • 7,607
14 votes
8 answers
1k views

Change the subject

Sometimes I feel like the conversation is going down the wrong path and take it upon myself to steer it in another direction. And this conversation is going wrong real fast, so I need to change the ...
Jacob's user avatar
  • 2,426
17 votes
16 answers
724 views

Form a subset that is a continuous range

Given a list of lists of positive integers, output a subset of them so their union forms a continuous non-empty range with no numbers missing. For example, consider this input: ...
mousetail's user avatar
  • 9,204
13 votes
17 answers
2k views

Remove duplicates from my academic transcript

Consider a list of subject, grade pairs. E.g. ...
Simd's user avatar
  • 379
8 votes
31 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 ...
U13-Forward's user avatar
  • 1,979
13 votes
33 answers
646 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. ...
U13-Forward's user avatar
  • 1,979
18 votes
22 answers
823 views

Generate recursive "arrays" up to a certain depth

Given a positive integer n. Generate a JSON array (can be a string, or your language's built-in JSON representation as long as we can get valid JSON, (your code ...
f478ccf2's user avatar
  • 183
13 votes
10 answers
655 views

CGAC2022 Day 8: Fen The Wicked, Part 2

Part of Code Golf Advent Calendar 2022 event. See the linked meta post for details. Fen is a magician Elf. He can cast spells on an array of numbers to produce another number or array of numbers. One ...
Bubbler's user avatar
  • 70.2k
20 votes
25 answers
1k views

CGAC2022 Day 7: Fen The Wicked

Part of Code Golf Advent Calendar 2022 event. See the linked meta post for details. Fen is a magician Elf. He can cast spells on an array of numbers to produce another number or array of numbers. One ...
Bubbler's user avatar
  • 70.2k
11 votes
6 answers
430 views

Conversions Galore!

Imagine that there are \$n\$ different types of objects \$O_1,O_2,O_3,\ldots,O_n\$ and they each have a conversion factor \$k_1,k_2,k_3,\ldots,k_n\$. You can, for any \$1\le i\le n\$, convert \$k_i\$ ...
Aiden Chow's user avatar
  • 11.6k
6 votes
5 answers
874 views

What is the longest trampoline?

In this challenge we are going to consider lists of integers such that for every member \$x\$ at index \$i\$ then the indexes \$i+x\$ and \$i-x\$ have the value \$x+1\$ or are out of bounds for the ...
Wheat Wizard's user avatar
  • 94.5k
22 votes
15 answers
2k views

Construct a diverse array

Given an integer \$ n \ge 1 \$, your task is to output an array \$ a \$ consisting of \$ n^2 + 1 \$ integers, such that all possible pairs of integers \$ (x, y) \$ satisfying \$ 1 \le x, y \le n \$ ...
dingledooper's user avatar
  • 20.4k
17 votes
9 answers
1k views

Longest alternating subsequence

inspired by Longest Increasing Substring Task Given a list of non-negative integers, output the length of its longest alternating subsequence. An alternating sequence is a sequence of numbers which ...
Bubbler's user avatar
  • 70.2k
22 votes
43 answers
2k views

Repeat Values In Array

Given two arrays of non-negative integers \$A = [A_1,A_2,\ldots,A_n]\$ and \$R = [R_1,R_2,\ldots,R_n]\$ which are equal in length, return an array which has the element \$A_1\$ repeated \$R_1\$ times, ...
97.100.97.109's user avatar
0 votes
0 answers
66 views

Is this a refinement? [duplicate]

A partition of a list \$A\$ is a way of splitting \$A\$ up into smaller parts, concretely it is list of lists that when concatenated gives back \$A\$. For example ...
Wheat Wizard's user avatar
  • 94.5k
24 votes
16 answers
2k views

Reconstruct Matrix from its diagonals

Given the diagonals of a matrix, reconstruct the original matrix. The diagonals parallel to the major diagonal (the main diagonals) will be given. Diagonals: ...
math scat's user avatar
  • 7,607
10 votes
1 answer
281 views

Optimally pop all the bubbles

In a given 2d grid of positive integers (representing different types of "bubbles"), there are one of two actions that you can do each "step". You can either: Pop an island (a ...
Aiden Chow's user avatar
  • 11.6k
19 votes
21 answers
2k views

Multiply numbers by their depth

Given a ragged list, we can define an element's depth as the number of arrays above it, or the amount that it is nested. For example, with the list ...
emanresu A's user avatar
  • 33.6k
10 votes
3 answers
370 views

Tile stacking with ■ and □

In this challenge you will receive as input a list of binary lists. The list represents a game board with each element representing a location on the board. The list at each location represents the ...
Wheat Wizard's user avatar
  • 94.5k
21 votes
23 answers
2k views

Add in the blanks

In this challenge you will be given a list of positive integers which represents some range of integers which has been truncated for display. Your job is to find the missing bits and insert ellipses ...
Wheat Wizard's user avatar
  • 94.5k
11 votes
12 answers
728 views

Get multi-dimensional indices in a list

Given a matrix of non-negative numbers (of arbitrary dimension) and a number, get all multi-dimensional indices of the number in that list. For example, let's say we have the list ...
naffetS's user avatar
  • 11.9k
18 votes
11 answers
956 views

Word stays a word after taking away a letter

This is a repost of this challenge Challenge There is an old, popular riddle: Find an English word with 8 letters that, taken away one letter, creates a new valid word. Repeat that until there are ...
math scat's user avatar
  • 7,607
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 ...
thejonymyster's user avatar
16 votes
11 answers
2k views

Flood fill by distance

Say I have a grid with some obstacles: ---- | X | | | |XX | | | ---- I can fill this with numbers, as distance from the top left corner. But, because ...
emanresu A's user avatar
  • 33.6k
19 votes
16 answers
2k views

Check a mushroom forest

In this challenge you will receive a list of pairs of non-negative integers representing a mushroom forest. Each pair represents a mushroom whose center is at that horizontal coordinate. The only ...
Wheat Wizard's user avatar
  • 94.5k
21 votes
9 answers
2k views

How tall are the mushrooms?

In this challenge you will receive a list of non-negative integers. Each one represents a mushroom with a cap of that radius centered at that location. So a 0 ...
Wheat Wizard's user avatar
  • 94.5k
16 votes
12 answers
2k views

Find the walls!

This is a simpler variant of my previous challenge Don't touch the walls!, as suggested by Jonah. Given a multi-dimensional rectangular array of integers between 0 and 9, output all the elements ...
pxeger's user avatar
  • 22.9k
17 votes
12 answers
2k views

Extend a matrix in all directions

Given a multidimensional, rectangular array and a list of dimensions, such as: [ [1, 2], [3, 4] ] [3, 4] Your challenge is to extend the matrix to the ...
emanresu A's user avatar
  • 33.6k
30 votes
5 answers
5k views

Can the 🐸 visit all the 🪷?

In this challenge you will be simulating a frog jumping from lily-pad to lily-pad in a pond. A frog's jump distance is uniquely determined by the size of the lily pad it jumps from. So for example ...
Wheat Wizard's user avatar
  • 94.5k
15 votes
5 answers
2k views

Don't touch the walls!

Given a multi-dimensional rectangular array of non-negative integers, pad it with the minimal number of zeroes so that only zeroes are "touching the edges" of the array, in every dimension. ...
pxeger's user avatar
  • 22.9k
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 ...
math scat's user avatar
  • 7,607
1 vote
0 answers
136 views

Figure out the least amount of duplicate data to fill a quota [closed]

You are given 100 arrays/sets of 5-8 numbers between 1-20. If you combine all the arrays you will always get at least one of each number (1-20), so don't worry about an "unsolvable" case. ...
Revel Carlberg West's user avatar
19 votes
28 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 ...
Rydwolf Programs's user avatar

1
2 3 4 5
16