Skip to main content

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
110 votes
36 answers
12k views

What are the five most powerful characters in your language?

Choose any five characters your language supports. There are 5! = 5×4×3×2×1 = 120 ways these can be arranged into a 5-character string that contains each character once; 120 permutations. Choose your ...
Calvin's Hobbies's user avatar
94 votes
163 answers
13k views

Determine whether strings are anagrams

Challenge Given two strings, work out if they both have exactly the same characters in them. Example Input word, wrdo This returns true because they are the ...
Tom Gullen's user avatar
  • 1,249
76 votes
11 answers
11k views

The 9 Billion Names of God

The 9 Billion Names of God is a short story by Arthur C. Clarke. It's about a group of Tibetan monks whose order is devoted to writing down all the possible names of God, written in their own alphabet....
CSturgess's user avatar
  • 863
48 votes
26 answers
4k views

Greater than less than greater than something fishy

Given a length N string of less-than and greater-than signs (<, >), insert the integers 0 through N at the start and end ...
Calvin's Hobbies's user avatar
46 votes
20 answers
5k views

Shotgun Numbers

The Shotgun numbers are a sequence with a rather simple definition but some interesting structure. Start with the natural numbers: ...
Martin Ender's user avatar
45 votes
16 answers
3k views

Scramble words while preserving their outlines

This is much more advanced than How to randomize letters in a word and Cambridge Transposition because of the rule about which letters may be swapped with which. A simple regex will not suffice here. ...
Adám's user avatar
  • 30.4k
44 votes
28 answers
2k views

Antsy permutations

Introduction Suppose you have a ruler with numbers from 0 to r-1. You place an ant between any two of the numbers, and it starts to crawl erratically on the ruler. The ruler is so narrow that the ant ...
Zgarb's user avatar
  • 42.7k
41 votes
26 answers
3k views

Sheffle tho vawols ureund!

Given an input string, output that string with all vowels a, e, i, ...
Fatalize's user avatar
  • 38.9k
41 votes
11 answers
4k views

Find a binary needle in a decimal haystack

The challenge You're given: a non-empty, unsorted list h of positive integers (the haystack) a positive integer n (the needle) Your task is to return the list of all unique decimal concatenations ...
Arnauld's user avatar
  • 193k
39 votes
67 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
  • 34.9k
37 votes
36 answers
3k views

Manage Trash So

It's become somewhat of a tradition in PPCG that some users temporarily change their names by an anagram (a new name formed by reordering the letters of the old). Sometimes it gets difficult to find ...
Luis Mendo's user avatar
  • 105k
37 votes
21 answers
3k views

The Original Number

Guidelines Scenario John has an important number, and he doesn't want others to see it. He decided to encrypt the number, using the following steps: His number is always a non-decreasing sequence (ie. ...
aimorris's user avatar
  • 1,401
36 votes
6 answers
2k views

Can you reach this number by doubling and rearranging?

Inspired by this question on Math.SE. Starting with 1 you can repeatedly perform one of the following two operations: Double the number. or Rearrange its digits ...
Martin Ender's user avatar
35 votes
35 answers
7k views

Random Golf of the Day #1: Shuffle an Array

About the Series I will be running a little series of code-golf challenges revolving around the theme of randomness. This will basically be a 9-Hole Golf Course, but spread out over several questions....
Martin Ender's user avatar
35 votes
17 answers
3k views

Hidden Inversions (Cops' Thread)

This is a cops-and-robbers puzzle the robbers' thread can be found here. Your task will be two write two programs (or functions) such that they are anagrams of each other and one performs the left ...
Wheat Wizard's user avatar
  • 98.8k
33 votes
21 answers
6k views

Levi-Civita symbol

The three-dimensional Levi-Civita symbol is a function f taking triples of numbers (i,j,k) each in ...
xnor's user avatar
  • 145k
31 votes
20 answers
3k views

Faro shuffle an array

A Faro shuffle is a technique frequently used by magicians to "shuffle" a deck. To perform a Faro shuffle you first cut the deck into 2 equal halves then you interleave the two halves. For ...
DJMcMayhem's user avatar
  • 59.3k
30 votes
21 answers
3k views

Generate a random derangement

Challenge description A "derangement" of a sequence is a permutation where no element appears in its original position. For example ECABD is a derangement of ...
shooqie's user avatar
  • 5,652
29 votes
31 answers
3k views

Deranged !Combinatorics: Compute the Subfactorial

The subfactorial or rencontres numbers (A000166) are a sequence of numbers similar to the factorial numbers which show up in the combinatorics of permutations. In particular the nth subfactorial !n ...
Martin Ender's user avatar
29 votes
30 answers
5k 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 \$2^n\$ ...
miles's user avatar
  • 16.9k
28 votes
30 answers
3k 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
28 votes
29 answers
5k views

Output an Anagram! No Not That One!

Given a list of unique strings that are anagrams of each other, output an anagram of those words that is different from each word in the list. The strings will be alphanumeric, and there is guaranteed ...
MercyBeaucou's user avatar
  • 3,061
28 votes
11 answers
1k views

Anagram Quines (Cops' Thread)

This is a cops-and-robbers challenge the Robbers' Thread can be found here Your challenge is to write a program that outputs an anagram of its source code but not the original source code itself. For ...
Wheat Wizard's user avatar
  • 98.8k
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
27 votes
30 answers
3k views

Alphabetically permute a string

Task Your goal, should you choose to accept it, is to write a program, that, given an input string (or array of characters), outputs every possible permutation of the letters in that string. I'm ...
Brian Gradin's user avatar
27 votes
5 answers
664 views

Bubble the brackets!

There are a few questions on this site about balancing brackets, and checking whether brackets are balanced. I propose it's now time to use those balanced brackets for something! In mathematics and ...
Arthur's user avatar
  • 371
25 votes
42 answers
3k views

Golf an Anagram Quine

In this question I asked you to guess an anagram quine based on its output. However it looks like we don't have a question asking to golf an anagram quine yet. So your task will be to make the ...
Wheat Wizard's user avatar
  • 98.8k
25 votes
37 answers
4k views

Reliably Broken Sort

Given a list of positive integers that contains at least 3 distinct entries, output a permutation of that list that isn't sorted in ascending or descending order. Examples ...
flawr's user avatar
  • 43.8k
25 votes
17 answers
3k views

Codegolf the Hafnian

The challenge is to write codegolf for the Hafnian of a matrix. The Hafnian of an \$2n \times 2n\$ symmetric matrix \$A\$ is defined as: $$\operatorname{haf}(A) = \frac 1 {n!2^n} \sum_{\sigma \in S_{...
user avatar
25 votes
8 answers
2k views

The Permutation Pigeon-hole Principle

In the game of sudoku, many players like to "pencil in" possible numbers that can go in each square: The above row can be represented as an array: ...
Nathan Merrill's user avatar
24 votes
26 answers
3k views

Find The Rank Of A Word

Definition The rank of a word is defined as the position of the word when all the possible permutations (or arrangements) of its letters are arranged alphabetically, like in a dictionary, no matter if ...
Manish Kundu's user avatar
  • 5,290
24 votes
16 answers
1k views

Every Possible Cycle Length

A function (or program) which takes inputs and provides outputs can be said to have a cycle if calling the function on its own output repeatedly eventually reaches the original number. For instance, ...
isaacg's user avatar
  • 42k
23 votes
27 answers
3k views

List all ordered partitions of n

The challenge is to list all ordered partitions (composition (combinatorics)) of a given positive integer n. These are the lists of numbers from ...
driima's user avatar
  • 461
23 votes
20 answers
2k views

Imitate an ordering

Given two lists of numbers, a source and a pattern, reorder the source to match the relative ordering of the pattern. Any two entries of the reordered source should compare the same way as the entries ...
xnor's user avatar
  • 145k
23 votes
32 answers
3k views

Code-Golf: Permutations

Write a function that takes as input a set of integers (can be a list, array or any other container with distinct numbers), and outputs the list of all its permutations. Python (95 chars): ...
zxul767's user avatar
  • 361
23 votes
22 answers
2k views

Permutapalindromic numbers

Given an integer N as input, output the Nth permutapalindromic number. A permutapalindromic number is a strictly positive ...
Fatalize's user avatar
  • 38.9k
23 votes
27 answers
2k views

Rearranging the sequence

Introduction Let's observe the following sequence (non-negative integers): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ... For example, let's take the ...
Adnan's user avatar
  • 44.4k
23 votes
23 answers
4k views

Encode - Shuffle - Decode

Challenge Your task is to encode an integer as a string of ASCII characters, then successfully decode it after said string has been randomly shuffled. You will write two programs/functions, which ...
Delfad0r's user avatar
  • 6,266
23 votes
25 answers
2k views

Quine Anagrams! (Cops' Thread)

This is the cops' thread. For the robbers' thread, click here. Cops' Task First, write a quine in a language of your choosing. Next, scramble the quine. Make sure to do this well as the robbers will ...
Oliver Ni's user avatar
  • 10.6k
23 votes
12 answers
4k views

Number of cycles of a permutation

Consider a permutation of the integers 1, ..., n, such as this one for n = 6: ...
Luis Mendo's user avatar
  • 105k
23 votes
25 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.8k
23 votes
16 answers
2k views

Random Golf of the Day #8: Shuffle an infinite list

About the Series First off, you may treat this like any other code golf challenge, and answer it without worrying about the series at all. However, there is a leaderboard across all challenges. You ...
Martin Ender'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,059
22 votes
13 answers
1k views

Numbers with similar powers

Given an integer p > 1, find the smallest integer q > p such that the list of exponents in the prime factorization of q is the same of that of p, no matter the order or the value of the prime ...
Arnauld's user avatar
  • 193k
22 votes
12 answers
3k views

Color me a Pole

Lets say your job is to paint poles, and a client asks you to paint a pole with 4 red sections and 3 yellow sections. You can do that pretty easily as follows: ...
Wheat Wizard's user avatar
  • 98.8k
22 votes
2 answers
1k views

The Combinatorics of Transistor

The video game Transistor features a very interesting ability system. You collect 16 "Functions" which you can use in 16 different slots. What's interesting is that there are 3 types of slots and ...
Martin Ender's user avatar
22 votes
8 answers
1k views

Build this pyramid

Your pyramid The pyramid I want you to build is made entirely of cubes. It has 24 layers, and the Nth layer from the top contains N2 cubes arranged in an N by N square. The pyramid looks like this: ...
Misha Lavrov's user avatar
  • 5,245
22 votes
3 answers
872 views

​L​o​o​p​ ​I​t​

Note: The title of this question should be "Loop It", but because title needs to be at least 15 characters, there are some invisible spaces. This note is such that the challenge can be ...
flawr's user avatar
  • 43.8k
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
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

1
2 3 4 5