Questions tagged [permutations]

A permutation is a particular ordering of some list of objects. Problems tagged with permutation usually involve finding or generating permutations, including anagrams of text.

Filter by
Sorted by
Tagged with
10 votes
16 answers
1k views

Numbers with distinct decimal digits

Write a program or function that outputs all positive integers with distinct decimal digits (OEIS: A010784) Examples: ...
bsoelch's user avatar
  • 5,815
15 votes
18 answers
2k views

First odd then even indices

Your task is to find out how often you need to "shuffle" a given list with the following operation until you get back the original list. ...
bsoelch's user avatar
  • 5,815
16 votes
17 answers
1k views

Count N-Rich Permutations of an Integer Sequence

Given a sequence of integers with length \$L\$ and an integer \$1 \le N \le L\$, an "\$N\$-rich" permutation is one whose the longest strictly increasing contiguous subsequence has length ...
EphraimRuttenberg's user avatar
21 votes
7 answers
1k views

How many sorting networks?

Below on the left is a picture of a sorting network that can sort 4 inputs. On the right you can see it sorting the input 3,2,4,1. A sorting network of size ...
AnttiP's user avatar
  • 7,828
19 votes
26 answers
2k views

Find Unique Anagrams

Challenge Description: An anagram is a word or phrase formed by rearranging the letters of another word or phrase. For example, "listen" and "silent" are anagrams. In this ...
cold10's user avatar
  • 293
0 votes
1 answer
269 views

Generate all possible equations from a list of numbers [closed]

This is my first codegolf post so let me know if I have missed anything. Thanks :) Description You are given a list of numbers with 2 < n <= 6 length i.e. [1, ...
Kyle Sharp's user avatar
0 votes
0 answers
129 views

Decoding a non injective bit matrix encoding

The problem Someone has created an encoding format for square bit matrices, however they have found it isn't perfect! One encoding may not decode to exactly one matrix, or it may not even be possible ...
Energize1828's user avatar
13 votes
14 answers
612 views

Generate a permutation from the high-water marks

Given a permutation, we can define its high-water marks as the indices in which its cumulative maximum increases, or, equivalently, indices with values bigger than all previous values. For example, ...
Command Master's user avatar
11 votes
17 answers
435 views

Normal Subgroups of \$S_4\$

Objective Given a permutation of 4 distinct items, classify the permutation by the normal subgroup(s) it belongs. Input/Output Format You gotta choose the followings as the hyperparameters for your ...
Dannyu NDos's user avatar
  • 5,497
15 votes
9 answers
1k views

CGAC2022 Day 6: Shuffles with specific "magic number"

Part of Code Golf Advent Calendar 2022 event. See the linked meta post for details. On the flight to Hawaii for vacation, I'm playing with a deck of cards numbered from 1 to \$n\$. Out of curiosity, ...
Bubbler's user avatar
  • 73.8k
19 votes
15 answers
1k views

Character insertion on letterboards

You know those letterboards outside old-style cinemas which show upcoming films - perhaps you have a miniature one in your home? If you've operated one, you'll know that you can normally add letters ...
FlipTack's user avatar
  • 14.6k
0 votes
3 answers
322 views

All permutations of range from \$1\$ to \$n\$ [duplicate]

Given a positive input \$n\$, output all permutations of either \$\{0,1,\ldots,n-1\}\$ or \$\{1,2,\ldots,n\}\$. Examples Outputting permutations of \$\{1,2,\ldots,n\}\$. Input Output ...
97.100.97.109's user avatar
11 votes
7 answers
924 views

Swap every two elements in the list every possible way

Inspired by this question. Challenge Let L be a list of n distinct elements. Let P be the ...
Pavgran's user avatar
  • 467
11 votes
12 answers
849 views

Permutations summing to permutations

Given an integer \$N\$ consider a permutation \$p=p_1,p_2,p_3,\ldots\$ of \$1,\ldots,N-1\$. Let \$P = p_1 , p_1+p_2 \bmod N, p_1+p_2+p_3 \bmod N, \ldots\$ be its prefix sums modulo \$N\$. Sometimes \$...
Albert.Lang's user avatar
  • 2,329
11 votes
14 answers
699 views

Increasing permutation trees

For this challenge a "binary tree" is a rooted tree where each node has 0 children (leaf) or 2. The children of a node are unordered, meaning that while you might draw the tree with left ...
Wheat Wizard's user avatar
  • 96.5k
15 votes
13 answers
916 views

Count alternating permutations

An alternating permutation is a permutation of the first \$ n \$ integers \$ \{ 1 ... n \} \$, such that adjacent pairs of values in the permutation alternate between increasing and decreasing (or ...
pxeger's user avatar
  • 23.3k
20 votes
13 answers
3k views

Shuffle an array, a little bit

Given some input array a = [a1, a2, ..., an] and a positive integer k, shuffle the input array ...
flawr's user avatar
  • 43.7k
7 votes
16 answers
1k views

Complex permutation

Given an array of integers as input(including negative integers), find all (can include duplicates) permutations that do not have the same number next to each other, then print them all out. If no ...
Joao-3's user avatar
  • 683
8 votes
20 answers
1k views

Give the inverse permutation

Task Given a finite permutation output its inverse. You may take input and output in any reasonable format equivalent to a list of natural numbers. You may choose to use 0 indexing or 1 indexing. ...
Wheat Wizard's user avatar
  • 96.5k
15 votes
8 answers
612 views

Is it a sub-permutation?

A finite-permutation is a function which takes an \$n\$-tuple and produces an \$n\$-tuple such that every element of the input is present in the output, and the ordering does not rely on the values of ...
Wheat Wizard's user avatar
  • 96.5k
22 votes
23 answers
1k views

How many laps around the permutation?

Your input is an array of numbers: a permutation of \$\{1, 2 \dots n\}\$ for some integer \$n \geq 2\$. How many times must you repeat this list before you can "pick out" the numbers \$[1, 2 ...
Lynn's user avatar
  • 68.1k
18 votes
16 answers
1k views

Slater-Velez permutation

Let's build a sequence of positive integers. The rule will be that the next number will be the smallest number which: It hasn't already appeared in the sequence Its absolute difference from the ...
Wheat Wizard's user avatar
  • 96.5k
28 votes
30 answers
2k views

Unsort an array

Inspired by this StackOverflow post Given a list where each element appears a maximum of 2 times, we can define it's "sortedness" as the sum of the distances between equal elements. For ...
caird coinheringaahin g's user avatar
8 votes
17 answers
828 views

Minimum difference between cartesian product of 3 elements that add up to a certain number

Let's say I've got a list (or array) of: l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] I want to get the third Cartesian power of the above list, where we take every ...
U13-Forward's user avatar
  • 2,001
10 votes
4 answers
201 views

Hamiltonian levencycle of 1-dup permutations

The word "levencycle" is inspired by cyclic levenquine challenge. Definitions A 1-dup permutation of order \$n\$ is some permutation of \$1, \cdots, n\$ plus one duplicate number in the ...
Bubbler's user avatar
  • 73.8k
37 votes
65 answers
4k 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-...
tsh's user avatar
  • 34k
21 votes
16 answers
3k views

Largest Number with No Repeating Digit Pairs

Inspired by the problem with the same name on Puzzling SE by our very own Dmitry Kamenetsky. You are to find the largest number that only uses every digit pair once, in a given base. For example, in ...
AviFS's user avatar
  • 2,111
15 votes
14 answers
1k views

Evaluation order of an APL n-train

From Codidact with permission. Description APL trains are a series of functions, that get applied to an argument in this way: (f g) x = f g x here ...
Adám's user avatar
  • 29.9k
8 votes
3 answers
411 views

Patterns in Permutations

This fastest-code challenge is based partly on this MSE question and exists to extend some OEIS sequences, and create others. If I extend or create sequences based on this challenge, I'll link to this ...
Peter Kagey's user avatar
  • 8,659
2 votes
0 answers
85 views

Find the number of parenthesis combinations [duplicate]

For example, given 3 sets of parenthesis, you have: ()()() ((())) ()(()) (())() (()()) = 5 possible combinations. Challenge Program must: • Take 1 number as an ...
Camden Weaver's user avatar
3 votes
0 answers
174 views

Sums of permutations of vectors [closed]

I am looking for a more efficient way of computing the following. Let A and B be two vectors of non-negative integers of length <...
Per Alexandersson's user avatar
22 votes
20 answers
2k views

Verify a Superpermutation

A superpermutation on n symbols is a string which contains every permutation of n symbols in its body. For instance, 123121321 is a superpermutation on three ...
golf69's user avatar
  • 2,049
18 votes
20 answers
4k views

Generate Gmail Dot-Aliases

Background You may be aware that periods in between letters in gmail addresses are ignored. Email sent to [email protected], [email protected], and [email protected] all end up in the same ...
Conor James Thomas Warford Hen's user avatar
9 votes
6 answers
601 views

Pairs of palindromic anagramic dates separated by a palindromic number of days

Definitions: A palindrome is a string which reads the same backward or forward (not counting spaces or special characters), such as "madam" or "Sorel Eros". A date is said to be a palindrome when ...
Regis Portalez's user avatar
3 votes
7 answers
257 views

String permutations

Print the biggest-size subset of strings in the input that are all permutations of each other. If there are ties, any one will do. If no strings are permutations of each other (for example, only one ...
Nohl's user avatar
  • 57
20 votes
19 answers
3k views

Super permutations

Super permutations Input: A string The program should loop through all lengths of the input (decrementing one each time), generate all combinations with replacement of the string, then make ...
InxaneNinja's user avatar
20 votes
11 answers
4k views

Ethan Finds the Maximum Element

This is a problem that the Hacker Cup team made for the 2018 Facebook Hacker Cup, but we ended up not using it (though Ethan struggles through a variety of other challenges). Normally code size isn't ...
Wesley May's user avatar
18 votes
18 answers
2k views

Plain Hunt Bell-Ringing

Your task is to create a plain hunt (a bell ringing pattern) with n bells. An example with 6 bells: ...
gadzooks02's user avatar
14 votes
11 answers
850 views

Output Distinct Factor Cuboids

Output Distinct Factor Cuboids Today's task is very simple: given a positive integer, output a representative of each cuboid formable by its factors. Explanations A cuboid's volume is the product ...
lyxal's user avatar
  • 32.2k
18 votes
14 answers
1k views

Permutations in Disguise

Given a \$n\$-dimensional vector \$v\$ with real entries, find a closest permutation \$p\$ of \$(1,2,...,n)\$ with respect to the \$l_1\$-distance. Details If it is more convenient, you can use ...
flawr's user avatar
  • 43.7k
22 votes
9 answers
3k views

I see your BIDMAS and raise you a BADMIS

I see your BIDMAS and raise you a BADMIS Challenge Given a set of numbers with operators between them: "5 + 4 * 9 / 3 - 8", return all the possible results of the expression for every permutation of ...
Freddie R's user avatar
  • 321
21 votes
12 answers
3k views

Calculate Landau's function

Landau's function \$g(n)\$ (OEIS A000793) gives the maximum order of an element of the symmetric group \$S_n\$. Here, the order of a permutation \$\pi\$ is the smallest positive integer \$k\$ such ...
Daniel Schepler's user avatar
9 votes
4 answers
663 views

Leading and Suffering Numbers

Introduction: In Dutch, the words leading and suffering, being 'leiden' and 'lijden' respectively, are pronounced the same way. One is written with a "short ei", and one with a "long ij&...
Kevin Cruijssen's user avatar
14 votes
15 answers
1k views

Dihedral group D4 composition with custom labels

The dihedral group \$D_4\$ is the symmetry group of the square, that is the moves that transform a square to itself via rotations and reflections. It consists of 8 elements: rotations by 0, 90, 180, ...
xnor's user avatar
  • 144k
17 votes
23 answers
2k 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 ...
flawr's user avatar
  • 43.7k
3 votes
3 answers
1k views

Words hidden in my phone number [closed]

Telephones in the United States (and perhaps some other places) have letters associated with some of the numbers. ...
Michael Stern's user avatar
15 votes
11 answers
1k views

Source permutation

A permutation of a set \$S = \{s_1, s_2, \dotsc, s_n\}\$ is a bijective function \$\pi: S \to S\$. For example, if \$S = \{1,2,3,4\}\$ then the function \$\pi: x \mapsto 1 + (x + 1 \mod 4)\$ is a ...
ბიმო's user avatar
  • 16.7k
14 votes
6 answers
370 views

Unique Brick Tilings Within A Rectangle

I was browsing Stackoverflow and saw this question about tiling an MxN rectangle, and I thought it would be great for golfing. Here is the task. Given the dimension M and N, write a program that ...
rtpax's user avatar
  • 721
28 votes
13 answers
3k views

Superpermutations

Introduction You're a criminal tasked with stealing some secret plans from the new tech startup Dejavu. You sneak in over the back wall, but find a door that requires a pin to open it. You recognize ...
Isaac C's user avatar
  • 389
15 votes
10 answers
1k views

Hide the buildings

Shorter version of Skyscrapers Challenge Task Given an array of building heights and a positive integer k, find all the permutations(without duplicates) of the ...
Vedant Kandoi's user avatar

1
2 3 4 5