Why does this choke?

Discussion in 'Python' started by S Kemplay, Nov 7, 2003.

  1. S Kemplay

    S Kemplay Guest

    Hi all,

    I wrote a script to choose random dates for a statistics assignment.
    I only need to choose 30 dates from one year with no leap years and it works
    fine. However I tested with different numbers of dates. It hangs from 450 up.
    I only need 30 dates but it would be good to know why it hangs. (My coding
    probably has something to do with it :))


    import random

    def getmonth():
    month = random.randint(1,12)
    return month

    def getday(month, leaps):
    thirtyones = [1,3,5,7,8,10,12]
    thirties = [4,6,9,11]
    if month in thirtyones:
    day = random.randint(1,31)
    elif month in thirties:
    day = random.randint(1,30)
    else:
    if leaps == 1: leap = random.randint(1,4)
    else: leap = 1
    if leap in [2,3,4]:
    day = random.randint(1,29)
    else:
    day = random.randint(1,28)
    return day

    def getdates(n, leaps):
    dates = []
    i = 0
    while i < n:
    month = getmonth()
    day = getday(month, leaps)
    if (day, month) in dates:
    continue
    i += 1
    dates.append((day, month))
    return dates


    Thanks

    Sean Kemplay
    S Kemplay, Nov 7, 2003
    #1
    1. Advertising

  2. S Kemplay

    John J. Lee Guest

    S Kemplay <> writes:

    > I wrote a script to choose random dates for a statistics assignment.
    > I only need to choose 30 dates from one year with no leap years and it works
    > fine. However I tested with different numbers of dates. It hangs from 450 up.
    > I only need 30 dates but it would be good to know why it hangs. (My coding
    > probably has something to do with it :))

    [...]
    > if (day, month) in dates:
    > continue

    [...]

    This looks like the culprit. I bet it really hangs from 366 up <wink>.


    John
    John J. Lee, Nov 7, 2003
    #2
    1. Advertising

  3. S Kemplay

    S Kemplay Guest

    Thanks John, that explains it :)

    Sean Kemplay

    On Friday 07 November 2003 23:30, John J. Lee wrote:
    > S Kemplay <> writes:
    > > I wrote a script to choose random dates for a statistics assignment.
    > > I only need to choose 30 dates from one year with no leap years and it
    > > works fine. However I tested with different numbers of dates. It hangs
    > > from 450 up. I only need 30 dates but it would be good to know why it
    > > hangs. (My coding probably has something to do with it :))

    >
    > [...]
    >
    > > if (day, month) in dates:
    > > continue

    >
    > [...]
    >
    > This looks like the culprit. I bet it really hangs from 366 up <wink>.
    >
    >
    > John
    S Kemplay, Nov 7, 2003
    #3
    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. William Payne

    Why doesn't std::cin choke on this?

    William Payne, Jan 5, 2004, in forum: C++
    Replies:
    10
    Views:
    661
    Chris \( Val \)
    Jan 6, 2004
  2. S Kemplay

    Why does this choke?

    S Kemplay, Nov 7, 2003, in forum: Python
    Replies:
    4
    Views:
    372
    Dennis Lee Bieber
    Nov 9, 2003
  3. Tim Peters
    Replies:
    0
    Views:
    372
    Tim Peters
    Nov 8, 2003
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,754
    Smokey Grindel
    Dec 2, 2006
  5. Z. M. Wu

    why does Perl choke here?

    Z. M. Wu, Nov 24, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    97
    Gunnar Hjalmarsson
    Nov 24, 2003
Loading...

Share This Page