Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [array-manipulation]

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

15
votes
22answers
1k views

Block-sorting rows and columns in a 2D array

Given a 2D array of integers, let's sort its rows and columns in blocks. This means that you only have to sort a given row or column, but applying the transformations needed for sorting it to every ...
29
votes
23answers
2k views

Find the largest number n positions away from n

A sequel to this question. Task Given an array of positive integers, find the largest element k for which: There exists some positive integer distance n, so that the element in the array located ...
11
votes
6answers
832 views

Group Integers by Originality

Introduction: I collect twisty puzzles. Most twisty puzzles are produced and sold by Chinese companies. Most well-known companies ask permission from puzzle designers to produce their designs and ...
44
votes
38answers
9k views

Weapons of Math Instruction

Last time when I tried to come up with something easy that wasn't a duplicate, it ended up being way too hard.. So hopefully this time it's indeed something newcomers can try as well. Input: An ...
26
votes
35answers
5k views

Mode (most common element) of a list

Write a snippet to calculate the mode (most common number) of a list of positive integers. For example, the mode of d = [4,3,1,0,6,1,6,4,4,0,3,1,7,7,3,4,1,1,2,8] ...
3
votes
3answers
148 views

Find longest consecutive characters in 2D array

Challenge Given a 2D array, find the length and direction of all the longest consecutive characters. If there are ties, output every possible winning combination in any order, (winners can overlap ...
19
votes
19answers
999 views

Extract Local Maxima

Given an array of positive integers, output an array of all the elements that are greater than or equal to the adjacent ones. Most elements will have two adjacent elements; the first and last element ...
23
votes
34answers
1k views

Parity of sum of other elements

Task Given an array of positive integers, replace each element with the parity of the sum of the other elements. The array is guaranteed to have at least 2 elements. Definition Parity: whether a ...
4
votes
16answers
328 views

Make an inversedict

Input A dictionary with string keys and string values. Output The 'inversedict' of this dictionary. How to make an inversedict An inversedict is a dictionary with string keys and string array ...
21
votes
7answers
984 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 ...
14
votes
16answers
778 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 ...
44
votes
18answers
6k views

Into how many pieces can you cut this string?

Consider a piece of string (as in "rope", not as in "a bunch of characters"), which is folded back and forth on the real line. We can describe the shape of the string with a list of points it passes ...
44
votes
76answers
7k views

Implementing a stack

I can't believe we don't have this already.. It's one of the most important data-structures in programming, yet still simple enough to implement it in a code-golf: Challenge Your task is to ...
14
votes
5answers
303 views

Optimal Caching

You will be given a sequence of memory requests and a cache size. You must return the least possible number of cache misses under any cache replacement strategy. An optimal strategy is Belady's ...
10
votes
2answers
289 views

Golf my Ada arrays

Background Ada is a programming language that is not exactly known for its terseness. However, its array literal syntax can in theory allow for fairly terse array specifications. Here is a simple ...
15
votes
22answers
3k views

Deal the cards to the players

Tonight is card game night! You are the dealer and your task is to write a program to deal the cards to the players. Given an array of cards and the number of players, you need to split the array of ...
39
votes
40answers
5k views

Is there a bump?

Given a list of positive integers determine if there is an element that is either greater than its two neighbors or less than its two neighbors (a "bump"). To be clear a bump can never be the first ...
23
votes
20answers
1k views

Maximum summed subsequences with non-adjacent items

Introduction: Inspired by these two SO questions (no doubt from the same class): print the elements in the subarray of maximum sum without adjacent elements java and Maximum sum of non adjacent ...
42
votes
34answers
3k views

At least h with at least h

Input A list of nonnegative integers. Output The largest nonnegative integer h such that at least h of the numbers in the ...
20
votes
21answers
2k views

How to print the below format in the fewest bytes?

This challenge is inspired by this, now deleted question. Take a positive integer N as input, and output a matrix with the numbers 1 .. N2 that follows the pattern below: Fill in the first row with ...
17
votes
24answers
1k views

Get the sequence steps

Challenge Given a sequence of numbers, create a function which returns the sequence steps. Assume a sequence will be N >= 3 Sequence will repeat it steps at ...
20
votes
38answers
3k views

Create chunks from an array

Your task is to write a program which given an array and a number, you need to split the array into chunks with size is number. Rules Your program will receive an array ...
3
votes
4answers
403 views

Intercommunication between threads [closed]

So glad I found this cool site. Wanted to try sharing a coding challenge a friend gave me, an interview question. Usually it is written in java, but you can use whatever language you want, just have ...
16
votes
22answers
910 views

Enumerate Derangements

Given some positive integer \$n\$ generate all derangements of \$n\$ objects. Details A derangement is a permutation with no fixed point. (This means in every derangement number \$i\$ cannot be in ...
57
votes
54answers
7k 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 element ...
28
votes
27answers
2k views

Bit-Reversal Permutations

Your goal is to create a function or a program to reverse the bits in a range of integers given an integer n. In other words, you want to find the bit-reversal permutation of a range of 2n items, zero-...
16
votes
8answers
650 views

Classify a region by its slope

Definitions The kth ring of a square matrix of size N, where 1 ≤ k ≤ ceiling(N/2) is the list formed by the elements of the kth and (N-k+1)th rows and columns, but without the first and last k-1 ...
-2
votes
6answers
238 views

Map values to any multidimensional array [closed]

Codegolf challenge - mapping values in a multidimensional array [ [0, 0, 0], [0, 0, 0], [0, 0, 0] ] This 2D array is sick of being so false, and wants to ...
30
votes
23answers
8k views

Could you please stop shuffling the deck and play already?

Challenge: Input: A list of distinct positive integers within the range \$[1, \text{list-size}]\$. Output: An integer: the amount of times the list is riffle-shuffled. For a list, this means the ...
21
votes
12answers
1k views

Slither Like a Snake

The Idea We've done matrix spirals before, and full rotations, and even diagonal rotations, but not, as far as I can find, snake rotations! What is a snake rotation? Imagine the rows of a matrix ...
28
votes
19answers
2k views

Rotate a column

Given an integer k and either a block of text, or a 2d array that the inner arrays may have unequal lengths (that resembles a block of text), rotate every character or element in the k-th column up or ...
32
votes
9answers
2k views

Rotate the anti-diagonals

Background In most reasonable programming languages, it's very easy to rotate the rows or columns of a 2D array. In this challenge, your task is to rotate the anti-diagonals instead. Recall that the ...
30
votes
33answers
3k views

Rebuild a rectangular array from a corner

I once had a beautiful rectangular array. It was very symmetrical, but unfortunately it has fallen apart and now I only have the top left corner. Your task will be to rebuild the original array. Your ...
19
votes
40answers
2k views

Halve the falses

Task Given a non-empty array of 0 and 1, halve the lengths of the runs of 0. Input An ...
40
votes
30answers
3k views

Print a physical multiplication table

Rectangles have this nice property - an \$n \times m\$ rectangle consists of exactly \$n \times m\$ characters! A.. more interesting property is that the rectangles can be aligned nicely in a ...
10
votes
5answers
479 views

Consistent Overhead Byte Stuffing (COBS)

I'm surprised this hasn't been posted before! The Consistent Overhead Byte Stuffing (COBS) algorithm is used to delimit byte streams. We choose a frame marker (we'll use 0x00) and wherever 0x00 ...
19
votes
10answers
478 views

Reorder a Master List Based on a Reordered Subset

I recently had a problem to solve at work where I had two lists: a master list, and a smaller list that contains a subset of the items in the master list potentially in a different order. I needed to ...
30
votes
69answers
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 ...
20
votes
46answers
2k views

Count repetitions of an array

You will receive an array and must return the number of integers that occur more than once. [234, 2, 12, 234, 5, 10, 1000, 2, 99, 234] This will return 2, since ...
24
votes
35answers
2k 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 ...
17
votes
26answers
837 views

Calculating total slots

Given a list of jobs, which must be done in order, with each taking a slot to do, how long will it take to perform them all if after doing a job the same job cannot be done for the next two slots (...
35
votes
49answers
3k views

Boustrophedonise

Related but very different. A boustrophedon is a text where every other line of writing is flipped or reversed, with reversed letters. In this challenge, we will just reverse every other line, but ...
62
votes
47answers
6k views

N-dimensional N^N array filled with N

In: Enough memory and a positive integer N Out: N-dimensional N^N array filled with N, where N^N means N terms of N-by-N-by-N-by... Examples: 1: [1] ...
21
votes
32answers
2k views

Count My Change

Your task is to sort an array containing the strings "quarter", "dime", "nickel", and "penny" any number of times in no specific order and sort them so that they are in this order: ...
-3
votes
1answer
215 views

Morse sudo-ku-de

Hehe, see what I did with the title? Anyways... Your job is to create a sudoku solver that accepts input in Morse code and creates output in Morse code. Here's the Morse code numbers for reference: <...
18
votes
9answers
1k views

Generate the Rummy Sequence

Your task is to take an input n and output element n of the Rummy Sequence, a sequence which I made (looking on OEIS will not ...
12
votes
28answers
2k views

Array Factorization

Given an array of positive integers, output a stable array of the distinct prime factors of these integers. In other words, for each integer in the input in order, get its prime factors, sort them, ...
14
votes
10answers
652 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 ...
33
votes
16answers
2k views

Who is the tallest?

N children, with no two sharing their exact size, are lined up in some order. Each can only compare heights with their immediate neighbours. When the teacher shouts "raise hands if you are the tallest"...
28
votes
12answers
4k views

Lifetime of a Worm

Terms A worm is any list of nonnegative integers, and its rightmost (i.e., last) element is called the head. If the head is not 0, the worm has an active segment consisting of the longest contiguous ...