Questions tagged [permutation]

A permutation is an arrangement of objects into a particular order.

A permutation is an arrangement of objects into a particular order.

For example, permutations of a set such as {1,2,3} are (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), and (3,2,1). The number of permutation of n objects is n! = n*(n-1)*(n-2)*...*3*2*1

References

5231 questions
26
votes
4 answers

np.random.permutation with seed?

I want to use a seed with np.random.permutation, like np.random.permutation(10, seed=42) I get the following error: "permutation() takes no keyword arguments" How can I do that else? Thanks.
Rockbar
  • 1,081
  • 1
  • 20
  • 31
26
votes
1 answer

On representations of permutations

I would like to have an inductive type to describe permutations and their action on some containers. It is clear that depending on the description of this type the definition complexity (in terms of its length) of algorithms (computing composition…
Katty J.
  • 686
  • 4
  • 11
25
votes
2 answers

Find all perfect squares that are a permutation of some 300 digits

This was a question that was asked to my friend in a Google interview a while back. He was unable to come up with a solution but ended up bagging the job anyway. Here's the question You have been given 300 digits comprising of 100 ones, 100 twos,…
sanz
  • 1,069
  • 1
  • 10
  • 26
25
votes
3 answers

What is a permuted index?

I am reading Accelerated C++. I don't understand Exercise 5-1: Design and implement a program to produce a permuted index from the following input. A permuted index is one in which each phrase is indexed by every word in the phrase. The…
Darson
  • 305
  • 4
  • 10
25
votes
6 answers

Heap's algorithm for permutations

I'm preparing for interviews and I'm trying to memorize Heap's algorithm: procedure generate(n : integer, A : array of any): if n = 1 then output(A) else for i := 0; i < n; i += 1 do generate(n - 1, A) …
24
votes
11 answers

Generating permutations using bash

is it possible to write a bash script that can read in each line from a file and generate permutations (without repetition) for each? Using awk / perl is fine. File ---- ab abc Output ------ ab ba abc acb bac bca cab cba
siliconpi
  • 8,105
  • 18
  • 69
  • 107
24
votes
4 answers

Why does next_permutation skip some permutations?

Why doesn't this simple function output all permutations of the inputted 5 letter string? I think there should be 120 and it only outputs 90. #include #include #include #include using namespace std; //…
Austin
  • 6,921
  • 12
  • 73
  • 138
23
votes
6 answers

Separating a String

Given a string, I want to generate all possible combinations. In other words, all possible ways of putting a comma somewhere in the string. For example: input: ["abcd"] output: ["abcd"] ["abc","d"] ["ab","cd"] …
Noob Coder
  • 949
  • 2
  • 10
  • 17
22
votes
5 answers

Given a permutation's lexicographic number, is it possible to get any item in it in O(1)

I want to know whether the task explained below is even theoretically possible, and if so how I could do it. You are given a space of N elements (i.e. all numbers between 0 and N-1.) Let's look at the space of all permutations on that space, and…
Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
22
votes
2 answers

C++ error: Undefined symbols for architecture x86_64

I'm trying to learn C++ and was trying to solve a problem where given a number of steps and the number of possible ways you can climb up the steps, give all the permutations of the possible ways you can climb up the steps. So for example, if there…
Timur Ridjanovic
  • 1,002
  • 1
  • 12
  • 18
22
votes
7 answers

Remove redundant parentheses from an arithmetic expression

This is an interview question, for which I did not find any satisfactory answers on stackoverflow or outside. Problem statement: Given an arithmetic expression, remove redundant parentheses. E.g. ((a*b)+c) should become a*b+c I can think of an…
Darth.Vader
  • 5,079
  • 7
  • 50
  • 90
22
votes
2 answers

How many ways can you insert a series of values into a BST to form a specific tree?

This earlier question asked how many ways there were to insert the values 1 - 7 into a binary search tree that would result in the following tree: 4 / \ 2 6 / \ / \ 1 3 5 7 (The answer is 80, by the way). Suppose…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
21
votes
7 answers

How can I generate all permutations of an array in Perl?

What's the best (elegant, simple, efficient) way to generate all n! permutations of an array in perl? For example, if I have an array @arr = (0, 1, 2), I want to output all permutations: 0 1 2 0 2 1 1 0 2 1 2 0 2 0 1 2 1 0 It should probably be a…
Frank
  • 64,140
  • 93
  • 237
  • 324
21
votes
6 answers

Find the index of a given permutation in the sorted list of the permutations of a given string

We're given a string and a permutation of the string. For example, an input string sandeep and a permutation psdenae. Find the position of the given permutation in the sorted list of the permutations of the original string.
sunmoon
  • 1,448
  • 1
  • 15
  • 27
21
votes
4 answers

Creating multiple numbers with certain number of bits set

Problem I need to create 32 Bit numbers (signed or unsigned doesn't matter, the highest bit will never be set anyway) and each number must have a given number of Bits set. Naive Solution The easiest solution is of course to start with the number of…
Mecki
  • 125,244
  • 33
  • 244
  • 253