1

I'm working on a code to solve this problem:

You and your friends are in New York and are planning to go see a Broadway musical. Unfortunately, New York being New York, the tickets are just a tiny bit expensive. But one of the shows has a ticket lottery each night where impecunious people such as you have a chance to win the right to buy slightly less expensive tickets to good seats. The lottery operates as follows. First, everyone interested enters the lottery. Then, n lucky winners are drawn, and each of these is offered to buy up to t tickets.

Given the number of people p in your group (all of which entered the lottery) and the total number of people m that entered the lottery, what is the probability that you will be able to get tickets for your entire group? Assume that the n lucky winners are chosen uniformly at random from the m people that entered the lottery, and that each person can win at most once.

Here's my code:

import math

def lottery():

    m = int(raw_input('The number of people who entered the lottery: '))
    n = int(raw_input('The number of winner drawn from the total: '))
    t = int(raw_input('The number of tickets each winner can purchase: '))
    p = int(raw_input('The number of people in your group: '))

    def combinations(n, k):
        if 0 <= k <= n:
            ntok = 1
            ktok = 1
            for t in xrange(1, min(k, n - k) + 1):
                ntok *= n
                ktok *= t
                n -= 1
            return ntok // ktok
        else:
            return 0

    needed_wins = int(math.ceil(p/t))

    others = m - p

    loss = 0
    for i in range(needed_wins):
        loss += combinations(others, n-i) * combinations(p, i)

    total = combinations(m, n)

    prob = 1 - loss / total

    print(prob)

I tried to run it but the result came out wrong. For example, if the combination is (100,10,2,1), the result should be 0.1; instead it returned 1. I really appreciate it if anyone can help me out here.

3
  • 1
    You should look into itertools.combinations
    – jdotjdot
    Dec 24, 2012 at 2:30
  • @LongPham could you be more specific about what "I believe that itertools doesn't work for integer division" means? The comment about itertools.combinations() is not referring to anything relating to division.
    – Matt
    Dec 24, 2012 at 3:16
  • @Matt my bad. I meant I tried the itertools.combination() but it returned that int objects are not iterable.
    – Long Pham
    Dec 24, 2012 at 4:16

2 Answers 2

5

In Python 2, when you divide two integers, you always get an integer result. Try adding this line to the top of the file, which will get you the new Python 3 behavior, where dividing ints produces floats:

from __future__ import division
1
  • @LongPham if it works make sure to accept Ned's answer so that the question is marked as solved! Dec 24, 2012 at 3:54
1
import math

def lottery(m, n, t, p):

  needed_wins = math.ceil(p/t)

  others = m - p

  loss = 0
  for i in range(needed_wins):
    comb_others = combinations(others, n-i)
    comb_group = combinations(p, i)
    loss += comb_others * comb_group

  total = combinations(m, n)

  prob = 1 - loss / total

  return prob


def combinations(n, k):
  if 0 <= k <= n:
    ntok = 1 
    ktok = 1
    for t in range(1, min(k, n - k) + 1):
      ntok *= n
      ktok *= t
      n -= 1
    return ntok // ktok
  else:
    return 0
1
  • The changes are: I removed the user input to make the function more generic. Added error handling in combinations() for k out of range. Returned the result instead of printing it. Used more clear variable names. In this way the function is more reusable and avoids potential errors due to invalid input data.
    – Jose Perez
    Oct 23, 2023 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.