Python calculate arrangements of sequence

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












I have just started thinking about probabilities. A Problem that come up was how to calculate all the potential arrangements for a given sequence. By arrangement I mean unique permutation.



I initially used this method:



from itertools import permutations

sequence = '11223344'
len(set(permutations(sequence)))
# 2520


But for long sequences this can take a long time! (or run out of memory)



I came up with this function arrangements



from math import factorial
from functools import reduce
from operator import mul

def arrangements(sequence):
return factorial(len(sequence))/reduce(mul,
[factorial(sequence.count(i)) for i in set(sequence)])

# arrangements(sequence)
# 2520.0


My thinking is this:



For a given length sequence with all unique items there are factorial(len(sequence)) permutations.



For every repeated item in the sequence there will be factorial(#repeats) that will result in the same permutation.



My function calculates all permutations / all repeated permutations.



I'm sure I have reinvented an already existing standard python function somewhere. I'd like to know if my thinking is sound and the implementation makes sense.



Wouldn't itertools.arrangements be cool?







share|improve this question



























    up vote
    4
    down vote

    favorite












    I have just started thinking about probabilities. A Problem that come up was how to calculate all the potential arrangements for a given sequence. By arrangement I mean unique permutation.



    I initially used this method:



    from itertools import permutations

    sequence = '11223344'
    len(set(permutations(sequence)))
    # 2520


    But for long sequences this can take a long time! (or run out of memory)



    I came up with this function arrangements



    from math import factorial
    from functools import reduce
    from operator import mul

    def arrangements(sequence):
    return factorial(len(sequence))/reduce(mul,
    [factorial(sequence.count(i)) for i in set(sequence)])

    # arrangements(sequence)
    # 2520.0


    My thinking is this:



    For a given length sequence with all unique items there are factorial(len(sequence)) permutations.



    For every repeated item in the sequence there will be factorial(#repeats) that will result in the same permutation.



    My function calculates all permutations / all repeated permutations.



    I'm sure I have reinvented an already existing standard python function somewhere. I'd like to know if my thinking is sound and the implementation makes sense.



    Wouldn't itertools.arrangements be cool?







    share|improve this question























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      I have just started thinking about probabilities. A Problem that come up was how to calculate all the potential arrangements for a given sequence. By arrangement I mean unique permutation.



      I initially used this method:



      from itertools import permutations

      sequence = '11223344'
      len(set(permutations(sequence)))
      # 2520


      But for long sequences this can take a long time! (or run out of memory)



      I came up with this function arrangements



      from math import factorial
      from functools import reduce
      from operator import mul

      def arrangements(sequence):
      return factorial(len(sequence))/reduce(mul,
      [factorial(sequence.count(i)) for i in set(sequence)])

      # arrangements(sequence)
      # 2520.0


      My thinking is this:



      For a given length sequence with all unique items there are factorial(len(sequence)) permutations.



      For every repeated item in the sequence there will be factorial(#repeats) that will result in the same permutation.



      My function calculates all permutations / all repeated permutations.



      I'm sure I have reinvented an already existing standard python function somewhere. I'd like to know if my thinking is sound and the implementation makes sense.



      Wouldn't itertools.arrangements be cool?







      share|improve this question













      I have just started thinking about probabilities. A Problem that come up was how to calculate all the potential arrangements for a given sequence. By arrangement I mean unique permutation.



      I initially used this method:



      from itertools import permutations

      sequence = '11223344'
      len(set(permutations(sequence)))
      # 2520


      But for long sequences this can take a long time! (or run out of memory)



      I came up with this function arrangements



      from math import factorial
      from functools import reduce
      from operator import mul

      def arrangements(sequence):
      return factorial(len(sequence))/reduce(mul,
      [factorial(sequence.count(i)) for i in set(sequence)])

      # arrangements(sequence)
      # 2520.0


      My thinking is this:



      For a given length sequence with all unique items there are factorial(len(sequence)) permutations.



      For every repeated item in the sequence there will be factorial(#repeats) that will result in the same permutation.



      My function calculates all permutations / all repeated permutations.



      I'm sure I have reinvented an already existing standard python function somewhere. I'd like to know if my thinking is sound and the implementation makes sense.



      Wouldn't itertools.arrangements be cool?









      share|improve this question












      share|improve this question




      share|improve this question








      edited Mar 21 at 2:39
























      asked Mar 20 at 13:45









      James Schinner

      422113




      422113




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          Notes



          • I'd expect arrangements to return the unique permutations of sequence, not just how many there are.

          • If it returns a number, it should be an integer.

          • You could use collections.Counter instead of counting the integers again and again.

          • You're right, it would be nice to have itertools.unique_permutations. In the meantime, I often come back to this SO answer.

          Possible refactoring



          from math import factorial
          from functools import reduce
          from collections import Counter
          from operator import mul


          def count_unique_permutations(sequence):
          count_permutations = factorial(len(sequence))
          repetitions = (factorial(v) for v in Counter(sequence).values())
          return count_permutations // reduce(mul, repetitions)





          share|improve this answer























          • Hadn't considered using Counter, it looks better :)
            – James Schinner
            Mar 20 at 21:25


















          up vote
          3
          down vote













          The keyword to search for is "multinomial coefficient". That returns this answer: https://stackoverflow.com/questions/46374185/does-python-have-a-function-which-computes-multinomial-coefficients






          share|improve this answer





















          • Good to know. The linked code isn't very readable IMHO but it has the merit of not needing 4 imports for 4 lines of code.
            – Eric Duminil
            Mar 20 at 15:16










          • Thanks for the Terminology. @EricDuminil at least all imports are from the standard library.
            – James Schinner
            Mar 20 at 21:28










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190030%2fpython-calculate-arrangements-of-sequence%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          Notes



          • I'd expect arrangements to return the unique permutations of sequence, not just how many there are.

          • If it returns a number, it should be an integer.

          • You could use collections.Counter instead of counting the integers again and again.

          • You're right, it would be nice to have itertools.unique_permutations. In the meantime, I often come back to this SO answer.

          Possible refactoring



          from math import factorial
          from functools import reduce
          from collections import Counter
          from operator import mul


          def count_unique_permutations(sequence):
          count_permutations = factorial(len(sequence))
          repetitions = (factorial(v) for v in Counter(sequence).values())
          return count_permutations // reduce(mul, repetitions)





          share|improve this answer























          • Hadn't considered using Counter, it looks better :)
            – James Schinner
            Mar 20 at 21:25















          up vote
          4
          down vote



          accepted










          Notes



          • I'd expect arrangements to return the unique permutations of sequence, not just how many there are.

          • If it returns a number, it should be an integer.

          • You could use collections.Counter instead of counting the integers again and again.

          • You're right, it would be nice to have itertools.unique_permutations. In the meantime, I often come back to this SO answer.

          Possible refactoring



          from math import factorial
          from functools import reduce
          from collections import Counter
          from operator import mul


          def count_unique_permutations(sequence):
          count_permutations = factorial(len(sequence))
          repetitions = (factorial(v) for v in Counter(sequence).values())
          return count_permutations // reduce(mul, repetitions)





          share|improve this answer























          • Hadn't considered using Counter, it looks better :)
            – James Schinner
            Mar 20 at 21:25













          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          Notes



          • I'd expect arrangements to return the unique permutations of sequence, not just how many there are.

          • If it returns a number, it should be an integer.

          • You could use collections.Counter instead of counting the integers again and again.

          • You're right, it would be nice to have itertools.unique_permutations. In the meantime, I often come back to this SO answer.

          Possible refactoring



          from math import factorial
          from functools import reduce
          from collections import Counter
          from operator import mul


          def count_unique_permutations(sequence):
          count_permutations = factorial(len(sequence))
          repetitions = (factorial(v) for v in Counter(sequence).values())
          return count_permutations // reduce(mul, repetitions)





          share|improve this answer















          Notes



          • I'd expect arrangements to return the unique permutations of sequence, not just how many there are.

          • If it returns a number, it should be an integer.

          • You could use collections.Counter instead of counting the integers again and again.

          • You're right, it would be nice to have itertools.unique_permutations. In the meantime, I often come back to this SO answer.

          Possible refactoring



          from math import factorial
          from functools import reduce
          from collections import Counter
          from operator import mul


          def count_unique_permutations(sequence):
          count_permutations = factorial(len(sequence))
          repetitions = (factorial(v) for v in Counter(sequence).values())
          return count_permutations // reduce(mul, repetitions)






          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Mar 20 at 14:05


























          answered Mar 20 at 13:56









          Eric Duminil

          1,8501613




          1,8501613











          • Hadn't considered using Counter, it looks better :)
            – James Schinner
            Mar 20 at 21:25

















          • Hadn't considered using Counter, it looks better :)
            – James Schinner
            Mar 20 at 21:25
















          Hadn't considered using Counter, it looks better :)
          – James Schinner
          Mar 20 at 21:25





          Hadn't considered using Counter, it looks better :)
          – James Schinner
          Mar 20 at 21:25













          up vote
          3
          down vote













          The keyword to search for is "multinomial coefficient". That returns this answer: https://stackoverflow.com/questions/46374185/does-python-have-a-function-which-computes-multinomial-coefficients






          share|improve this answer





















          • Good to know. The linked code isn't very readable IMHO but it has the merit of not needing 4 imports for 4 lines of code.
            – Eric Duminil
            Mar 20 at 15:16










          • Thanks for the Terminology. @EricDuminil at least all imports are from the standard library.
            – James Schinner
            Mar 20 at 21:28














          up vote
          3
          down vote













          The keyword to search for is "multinomial coefficient". That returns this answer: https://stackoverflow.com/questions/46374185/does-python-have-a-function-which-computes-multinomial-coefficients






          share|improve this answer





















          • Good to know. The linked code isn't very readable IMHO but it has the merit of not needing 4 imports for 4 lines of code.
            – Eric Duminil
            Mar 20 at 15:16










          • Thanks for the Terminology. @EricDuminil at least all imports are from the standard library.
            – James Schinner
            Mar 20 at 21:28












          up vote
          3
          down vote










          up vote
          3
          down vote









          The keyword to search for is "multinomial coefficient". That returns this answer: https://stackoverflow.com/questions/46374185/does-python-have-a-function-which-computes-multinomial-coefficients






          share|improve this answer













          The keyword to search for is "multinomial coefficient". That returns this answer: https://stackoverflow.com/questions/46374185/does-python-have-a-function-which-computes-multinomial-coefficients







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Mar 20 at 15:08









          Acccumulation

          9595




          9595











          • Good to know. The linked code isn't very readable IMHO but it has the merit of not needing 4 imports for 4 lines of code.
            – Eric Duminil
            Mar 20 at 15:16










          • Thanks for the Terminology. @EricDuminil at least all imports are from the standard library.
            – James Schinner
            Mar 20 at 21:28
















          • Good to know. The linked code isn't very readable IMHO but it has the merit of not needing 4 imports for 4 lines of code.
            – Eric Duminil
            Mar 20 at 15:16










          • Thanks for the Terminology. @EricDuminil at least all imports are from the standard library.
            – James Schinner
            Mar 20 at 21:28















          Good to know. The linked code isn't very readable IMHO but it has the merit of not needing 4 imports for 4 lines of code.
          – Eric Duminil
          Mar 20 at 15:16




          Good to know. The linked code isn't very readable IMHO but it has the merit of not needing 4 imports for 4 lines of code.
          – Eric Duminil
          Mar 20 at 15:16












          Thanks for the Terminology. @EricDuminil at least all imports are from the standard library.
          – James Schinner
          Mar 20 at 21:28




          Thanks for the Terminology. @EricDuminil at least all imports are from the standard library.
          – James Schinner
          Mar 20 at 21:28












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190030%2fpython-calculate-arrangements-of-sequence%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          C++11 CLH Lock Implementation