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.

42
votes
17answers
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 ...
16
votes
2answers
987 views

Score a game of Kingdom Builder

I want to try a new form of code golf here. Similar to bonuses, not all parts of the challenge have to be completed, but each answer has to implement a subset of certain size (and there is a core that ...
21
votes
10answers
912 views

0-1 Maximal Phase Counter

Consider an array of bits, say 1 1 1 0 0 0 0 1 0 0 1 0 1 1 1 1 1 0 1 0 We call a contiguous subarray of length ≥ 5 a phase if at least 85% of the bits are the ...
6
votes
3answers
518 views

Compute the Join and Meet of Partitions

Background In this challenge, a partition of a set S means a collection of nonempty subsets of S, which are mutually disjoint ...
19
votes
8answers
1k views

Jump the Array!

Let's play a one-player game called jump the array. To play, you only need an array of integers, say a. You start at some position ...
33
votes
18answers
4k views

Unflatten an Array

This challenge was inspired by a question on Mathematica.SE. Say you've got a nested list/array of some arbitrary structure (the lists at each level don't necessarily have the same length). For ...
21
votes
6answers
1k views

Time Travelling Stock Trader

Story Long time ago Bobby created a Bitcoin wallet with 1 Satoshi (1e-8 BTC, smallest currency unit) and forgot about it. Like many others he later though "Damn, if only I invested more back then...". ...
40
votes
33answers
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 ...
16
votes
25answers
3k views

Shorter than a Split Second!

Challenge You task for this question is to split an input array of integers on the second occurrence of every integer in that array. Not clear enough ? Here is an example to help Input array: <...
19
votes
46answers
2k views

Create an array with repeated numbers

Challenge Your task in this question is to write a program or a named function which takes a positive integer n (greater than 0) as input via STDIN, ARGV or ...
25
votes
33answers
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] ...
5
votes
3answers
1k views

Shortest Code to Translate a PHP array

NOTE: This question was migrated from https://stackoverflow.com/q/27134588/1442685 PHP QUESTION Almost purely out of curiousity, how can I take an array like this: ...
6
votes
4answers
662 views

Shortest program to split special array

Background Imagine, you have a big array A, which is mostly zeroes, but contains a short subarray B which has only strictly ...
21
votes
2answers
1k views

Book Stack Sort

When stacking books you usually want to put the largest ones at the bottom and the smallest ones at the top. However, my latent OCD makes me feel very uneasy if I've got two books where one is shorter ...
0
votes
2answers
102 views

Approximated (unsorted) square from a set of values [closed]

So a colleaque came with this problem lately and I want to challange you to solve the problem in the code-golf style, shortest solution in any language wins. The Problem: Given is a random set of ...
-3
votes
7answers
219 views

Create an array of unique ints in a set range [closed]

Goal: Write a piece of code that produces a array containing all integer values in a set range in random order (upper and lower bounds included). Use any language you like, but use as little built-in ...
11
votes
1answer
387 views

Golfscript Diagonal Array

Is there a way in Golfscript to bring all of the diagonals of an array into a single array? For example, for the array [[1 2 3][4 5 6][7 8 9]] return ...
24
votes
42answers
4k views

Generating a single string with an Oxford comma from a list

What are some clever (brief and idiomatic) approaches to taking a list of strings and returning a single properly punctuated string built from the list, with each element quoted. This came up for me ...
5
votes
6answers
1k views

Programming Praxis - Survive like Flavius Josephus [duplicate]

Here's a challenge inspired by Programming Praxis: http://programmingpraxis.com/2009/02/19/flavius-josephus/ Write a function josephus(n,m) that returns a list of n people, numbered from 0 to n-...
8
votes
16answers
911 views

Rank a list of scores with “skips”

Given a list of scores (non-negative integers) pre-sorted from greatest to least: [ 10, 10, 6, 6, 4, 0] Assign each score an integer rank, beginning with 1 ...
2
votes
4answers
4k views

Rotate 2D Array [duplicate]

Given an input of a 2 dimensional array of integers, rotate the array 90 degrees clockwise. It should work for rectangular arrays of all sizes and proportions. The rotated array should be printed at ...
3
votes
2answers
639 views

Deplane! Deplane! (Or: RGBRGBRGB => RRRGGGBBB?)

Imagine bytes in memory for an RGB image--R and G and B represented by bytes in sequence. If an image is NxM then the memory layout will be M repetitions for vertical scanlines of 3*N bytes in ...
15
votes
5answers
917 views

Stack the deck!

Alice and Bob like to play a card game, with a deck of cards numbered with consecutive nonnegative integers. Alice has a very particular way of shuffling the deck, though. First, she takes the top ...
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 ...
10
votes
23answers
10k views

Rotate An Integer Array with an O(n) algorithm [closed]

Write a function that rotates an integer array by a given number k. k elements from the end should move to the beginning of array, and all other elements should move to right to make the space. The ...
4
votes
5answers
464 views

How many times is rectangular array A embedded in rectangular array B?

Given two rectangular arrays, A and B, let's say that A is embedded in B if A can be obtained from B by deleting some (or none) of the rows and columns of B. Write a program with the following I/O: ...
4
votes
1answer
635 views

Change the innermost element type of a jagged array — any .NET language: C#, F#, VB.NET, MSIL, C++/CLI, etc

Write a class containing a method with the signature System.Array ChangeElementType(System.Array jaggedArray) (you may rename identifiers ...
1
vote
10answers
4k views

Convert JSON string to Key / Value Arrays

Convert JSON (key/value pairs) to two native arrays, one array of keys and another of values, in your language. ...
18
votes
21answers
4k views

Javascript: Create a 10x10 array of 1s

Obviously this challenge would be trivial with separate functions and libraries, so they aren't allowed. Your code must conform to an ECMAscript specification (any spec will do), so no browser-...
-1
votes
1answer
337 views

Merge working time periods

You have an array of working time period pairs (from, till) received from user input and written according 24-hour clock format convention. You have to merge these periods if the intersect each ...
22
votes
39answers
3k 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 ...
8
votes
8answers
934 views

Array Sandwiches

(Adapted from Problem C of the first qualifier of the ACM Programming Contest of 2012/2013) You have several arrays, named A1, A2, ... , An, each sorted in ascending order. Every item in the array ...
5
votes
15answers
493 views

Create a hash from an array of arrays of key|value pairs [closed]

Starting from an array of sub-arrays, each sub-array is a 2 elements key|value pair. Example input = [["A", 5], ["B", 2], ["A", 12], ["B", -2], ["C", 7]] Convert ...
10
votes
13answers
771 views

Operations with Lists

Inspired by this question. Given a list containing numbers, print: The sum and product of the numbers in the list The average and median The differences between each term in the list (e.x. ...
1
vote
1answer
205 views

Reduce the amount of loops when finding if an element is stored only once in the array? [closed]

I was amazed to find that there is a way to reduce the times you loop on an array to find if a element is contained once. One would think that the only way would be to loop through the whole array, ...
5
votes
4answers
539 views

Polynomial extrapolation

Task Given a list of integers a1, a2, …, ak (k ≥ 2) and a positive integer m, write a function or a complete program that calculates the next m numbers of the list. Assume that ai = P(i) ...
14
votes
17answers
4k views

Shuffle a deck without local variables [closed]

The object of this puzzle is to take a deck of 52 cards and shuffle it so that each card is in a random position. Given: An array, deck, of 52 distinct integers ...
7
votes
9answers
399 views

All Possible Matching Lists

Implement a function that takes a list that consists of 0, 1 or 2, the list is called "pattern". Your job is to return all possible lists that match the pattern. 0 matches 0 1 matches 1 2 matches 0 ...
7
votes
0answers
762 views

Stable positive/negative separation [closed]

From SO. You are given an array with positive and negative integers. Write a function to change the elements order in the array such that negative integers are at the beginning, the positive integers ...