Live data from Hacker News

Generating all permutations, combinations, and power set of a string (2012)

exceptional-code.blogspot.com

11–20 of 48 posts

Re: Generating all permutations, combinations, and power set of a string (2012)

#12
I prefer the use of iterators, instead of generating a whole collection. An example of a permutation iterator in C++ is:

    class Permutations
    {
    public:
        Permutations(int val_n) : n(val_n), _more(true)
        {
            a = new int[n];
            for (int i = 0; i = 0; i--)
                if (a[i]  i; j--)
                        if (a[j] > a[i])
                        {
                            swap(a[j], a[i]);
                            break;
                        }
                    i++;
                    for (int j = n-1; j > i; j--, i++)
                        swap(a[j], a[i]);
                    break;
                }
        }
        int operator[](int i) { return a[i]; }
            
        int n;
    private:
        void swap(int &x, int &y)
        {
            int h = x;
            x = y;
            y = h;
        }
        bool _more;
        int *a;
    };

Re: Generating all permutations, combinations, and power set of a string (2012)

#15
post #10
post #7

Posts like this are why I love HN. I recently wrote a program to find anagrams of a given string (a Countdown solver if you live in the UK). It includes a really naive method for generating all possible permutations of the string, but reading this post I can immediately see a far better way to do it. That's my weekend taken care of! thanks to the poster and author.

There was a recent post about anagrams and the author just put all the letters in alphabetical order and then compared the words, which was more efficient than generating permutations.

Sounds interesting, do you have a link?

Re: Generating all permutations, combinations, and power set of a string (2012)

#16
post #10

Earlier quoted context omitted.

There was a recent post about anagrams and the author just put all the letters in alphabetical order and then compared the words, which was more efficient than generating permutations.

Sounds interesting, do you have a link?

http://blog.plover.com/lang/anagram-scoring.html

Re: Generating all permutations, combinations, and power set of a string (2012)

#17

If anyone wants to know how deep down the rabbit hole this stuff goes, they should read this blog post [1] on writing a Levenshtein Automaton to speed up fuzzy matching in Lucene by 100 times. It gets deep! [1]: http://blog.mikemccandless.com/2011/03/lucenes-fuzzyquery-is...

I am going to have to go through [1] referenced in the post.

[1]: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.16.6...

Re: Generating all permutations, combinations, and power set of a string (2012)

#18
post #10
post #7

Posts like this are why I love HN. I recently wrote a program to find anagrams of a given string (a Countdown solver if you live in the UK). It includes a really naive method for generating all possible permutations of the string, but reading this post I can immediately see a far better way to do it. That's my weekend taken care of! thanks to the poster and author.

There was a recent post about anagrams and the author just put all the letters in alphabetical order and then compared the words, which was more efficient than generating permutations.

That is what I did for this challenge: https://www.reddit.com/r/dailyprogrammer/comments/52enht/201...

https://github.com/Rhebel/DailyProgrammer/blob/master/DailyP...

I know my code is super verbose compared to other people's but I'd rather be understood than clever.

Re: Generating all permutations, combinations, and power set of a string (2012)

#20
post #7

Posts like this are why I love HN. I recently wrote a program to find anagrams of a given string (a Countdown solver if you live in the UK). It includes a really naive method for generating all possible permutations of the string, but reading this post I can immediately see a far better way to do it. That's my weekend taken care of! thanks to the poster and author.

"Good morning, that's a nice tnettenba." [1]

[1] https://www.youtube.com/watch?v=lsFAokXCxTI

Post reply on HN