list question... unique values in all possible unique spots

Discussion in 'Python' started by ToshiBoy, Aug 9, 2008.

  1. ToshiBoy

    ToshiBoy Guest

    I'm wondering how to do this the most elegant way: I found this quiz
    in some magazine. I've already solved it on paper, but want to write a
    python program to solve it. It comes down to being able to represent
    range(1,27) through a number of formulas. How do I write a loop that
    will loop through this list, so that: 1. every number only occurs
    once, and 2. I get every possibility of order within the list? I guess
    it'd be somewhat similar to Sudoku, where you have the numbers from
    1-9 in any possible order. Here it's 1-26.
     
    ToshiBoy, Aug 9, 2008
    #1
    1. Advertising

  2. On Aug 9, 9:05 am, ToshiBoy <> wrote:

    > I'm wondering how to do this the most elegant way: I found this quiz
    > in some magazine. I've already solved it on paper, but want to write a
    > python program to solve it. It comes down to being able to represent
    > range(1,27) through a number of formulas. How do I write a loop that
    > will loop through this list, so that: 1. every number only occurs
    > once, and 2. I get every possibility of order within the list? I guess
    > it'd be somewhat similar to Sudoku, where you have the numbers from
    > 1-9 in any possible order. Here it's 1-26.


    Search for a permutation generator function.

    George
     
    George Sakkis, Aug 9, 2008
    #2
    1. Advertising

  3. ToshiBoy

    Mensanator Guest

    On Aug 9, 8:05�am, ToshiBoy <> wrote:
    > I'm wondering how to do this the most elegant way: I found this quiz
    > in some magazine. I've already solved it on paper, but want to write a
    > python program to solve it. It comes down to being able to represent
    > range(1,27) through a number of formulas. How do I write a loop that
    > will loop through this list, so that: 1. every number only occurs
    > once, and 2. I get every possibility of order within the list? I guess
    > it'd be somewhat similar to Sudoku, where you have the numbers from
    > 1-9 in any possible order. Here it's 1-26.


    Python 2.6 has a permutation function:

    IDLE 2.6b1
    >>> import itertools
    >>> for i in itertools.permutations(range(4)): print i


    (0, 1, 2, 3)
    (0, 1, 3, 2)
    (0, 2, 1, 3)
    (0, 2, 3, 1)
    (0, 3, 1, 2)
    (0, 3, 2, 1)
    (1, 0, 2, 3)
    (1, 0, 3, 2)
    (1, 2, 0, 3)
    (1, 2, 3, 0)
    (1, 3, 0, 2)
    (1, 3, 2, 0)
    (2, 0, 1, 3)
    (2, 0, 3, 1)
    (2, 1, 0, 3)
    (2, 1, 3, 0)
    (2, 3, 0, 1)
    (2, 3, 1, 0)
    (3, 0, 1, 2)
    (3, 0, 2, 1)
    (3, 1, 0, 2)
    (3, 1, 2, 0)
    (3, 2, 0, 1)
    (3, 2, 1, 0)

    Bur bear in mind that permutations of size n are n!.
    So the permutaions of range(1,27) is 26! which is

    >>> print gmpy.fac(26)

    403291461126605635584000000

    That's 403 octillion.

    Are you sure you want to do this?
     
    Mensanator, Aug 9, 2008
    #3
  4. ToshiBoy

    Mensanator Guest

    On Aug 11, 3:53 pm, Tobiah <> wrote:
    > On Mon, 11 Aug 2008 13:46:10 -0700, Tobiah wrote:
    > > On Sat, 09 Aug 2008 08:07:26 -0700, Mensanator wrote:

    >
    > >> 403291461126605635584000000

    >
    > > I think it's only 4 septillion.

    >
    > I meant to say 403.


    Whatever.

    Ever tried to iterate 403 septillion times?
     
    Mensanator, Aug 12, 2008
    #4
  5. ToshiBoy

    Guest

    Mensanator:
    > Ever tried to iterate 403 septillion times?


    The OP is talking about formulas, like:
    X + Y * Z = W
    Where X, Y, Z, W distinct and in [1, 26], so you have C(26, 4)
    combinations that's way less than 26!

    >>> binomial(26, 4)

    14950

    So this can be solved with a xcombinations() generator.

    Bye,
    bearophile
     
    , Aug 12, 2008
    #5
  6. ToshiBoy

    Mensanator Guest

    On Aug 11, 7:34 pm, wrote:
    > Mensanator:
    >
    > > Ever tried to iterate 403 septillion times?

    >
    > The OP is talking about formulas, like:
    > X + Y * Z = W
    > Where X, Y, Z, W distinct and in [1, 26], so you have C(26, 4)
    > combinations that's way less than 26!
    >
    > >>> binomial(26, 4)

    >
    > 14950
    >
    > So this can be solved with a xcombinations() generator.


    How did you surmise it's C(26,4)?

    >
    > Bye,
    > bearophile
     
    Mensanator, Aug 12, 2008
    #6
  7. ToshiBoy

    ToshiBoy Guest

    Thank you for all your responses. I've tried the permutations road
    (thank you to all those of you who have suggested it) and it takes %*&
    %^ long :) As expected. I've solved it a different way, which runs
    through the 26 spots by just adding one at a time if available. Still
    takes a long time, but not as long as the permutation. That said,the
    permutation works great for other projects.
     
    ToshiBoy, Aug 12, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Klixx0r
    Replies:
    1
    Views:
    2,106
    Gerbrand van Dieijen
    Dec 8, 2004
  2. bonkura_
    Replies:
    1
    Views:
    553
    Roedy Green
    Oct 23, 2005
  3. bonkura_
    Replies:
    0
    Views:
    397
    bonkura_
    Oct 10, 2005
  4. Markus
    Replies:
    1
    Views:
    473
    Markus
    Nov 22, 2005
  5. Thomas Guettler
    Replies:
    0
    Views:
    212
    Thomas Guettler
    Jun 22, 2011
Loading...

Share This Page