league problem in python

R

Ross

I'm new to programming and have chosen Python as my first language.
I've gone through Allen Downey's Think Python book and I think I'm
ready to dive into a project. The first problem I've chosen to tackle
is a problem I have seen at my tennis club. Each spring/fall, the pro
puts out a sheet of paper for people to sign up for tennis leagues.
Depending on how many people sign up for a league, he'll assign a
certain number of courts each week to that league.

After this, he makes up a schedule of who plays who on each week and
who has a bye. Unfortunately, he does this by hand and a lot of times,
some people will play certain people more than once and certain other
people never. Other problems that arise: some people have more bye
weeks than others, some people have all their bye weeks clumped
together so that they don't play for weeks on end.

I would like to create a simple program where the pro could enter in
how many people were in the league, the number of courts available,
and the number of weeks the schedule would run and then generate a
schedule where everybody played everybody else once and got the same
number of bye weeks, preferably spaced out evenly.

How should I go about starting this problem...I'm feel like this is a
really simple problem, but I'm having writer's/coder's block. Can you
guys help?
 
B

bearophileHUGS

Ross:
How should I go about starting this problem...I'm feel like this is a
really simple problem, but I'm having writer's/coder's block. Can you
guys help?

There are refined ways to design a program, but this sounds like a
simple and small one, so you probably don't need much formal things to
solve it.

So start writing down:
1) A precise description of what results you want
2) A precise description of what data you have to find those answers
2b) You may want to think about what kind of user interface to give to
your program, but being this program very small and being you not much
experienced yet, you may think about this later too (see point 9).

Then:
3) Invent the simplest way to feed that data to the program, for
example reading a TXT file.
4) Create artificially some toy data files, very short, and able to
reveal several of the corner cases of your problem.

Then start programming in a test-driven way. Write a tiny program that
just reads and decodes the input data of one of your inputs and shows
them.

5) Once that is done, you may write some outputs, that is the result
of each of those little inputs.
6) Try to slowly grow the code to go toward the solutions you look
for.
7) If the algorithm and processing required to find the solutions
isn't simple, then you may want to step away from the computer, take a
graphite pencil, eraser and paper and invent how to solve the problem
generally.
7b) sometimes you may also need to invent how to represent data and
solutions (and even intermediate stages) into your small program.

8) Keep creating more complex test cases and look if their solution is
correct. If it's wrong then debug your program. Soon your purpose will
be to invent stranger and stranger corner cases able to break your
program (that make your program give a wrong answer).

9) Eventually you will have to think to improve the user interface. In
bigger programs this has to be done after point (2), but for your
small program you may do it now too.

This programming strategy is not good enough if you are writing big
programs or very difficult ones, but for small easy programs it's more
than enough.

Bye,
bearophile
 
R

r

How should I go about starting this problem...I'm feel like this is a
really simple problem, but I'm having writer's/coder's block. Can you
guys help?

Ross,
I highly disagree with bear on this. What you have here is a 90
percent math problem and a 10 percent code problem. First get the
numbers to add up correctly and then code that sucker up!
 
A

Andrew Henshaw

.... snip ...
I would like to create a simple program where the pro could enter in
how many people were in the league, the number of courts available,
and the number of weeks the schedule would run and then generate a
schedule where everybody played everybody else once and got the same
number of bye weeks, preferably spaced out evenly.

How should I go about starting this problem...I'm feel like this is a
really simple problem, but I'm having writer's/coder's block. Can you
guys help?

At least as a start, you want the round-robin tournament algorithm (see
http://en.wikipedia.org/wiki/Round-robin_tournament). Here's some code that
I use:

###### begin code #######
def roundrobin(teams, rounds=1):
# if odd number of teams, add a "bye" team
if len(teams) % 2:
teams.append(None)
mid = len(teams) // 2
for i in range(rounds):
yield zip(teams[:mid], teams[mid:])
teams = teams[0:1] + teams[mid:mid+1] + teams[1:mid-1] +
teams[mid+1:] + teams[mid-1:mid]

### test code ###
if __name__ == '__main__':
ROUNDS = 10
# two test cases - even number of teams and odd number of teams
for teams in (range(6), range(5)):
print "\nteams =", teams
for round in roundrobin(teams, ROUNDS):
print round

###### end code #######

And the output:

teams = [0, 1, 2, 3, 4, 5]
[(0, 3), (1, 4), (2, 5)]
[(0, 4), (3, 5), (1, 2)]
[(0, 5), (4, 2), (3, 1)]
[(0, 2), (5, 1), (4, 3)]
[(0, 1), (2, 3), (5, 4)]
[(0, 3), (1, 4), (2, 5)]
[(0, 4), (3, 5), (1, 2)]
[(0, 5), (4, 2), (3, 1)]
[(0, 2), (5, 1), (4, 3)]
[(0, 1), (2, 3), (5, 4)]

teams = [0, 1, 2, 3, 4]
[(0, 3), (1, 4), (2, None)]
[(0, 4), (3, None), (1, 2)]
[(0, None), (4, 2), (3, 1)]
[(0, 2), (None, 1), (4, 3)]
[(0, 1), (2, 3), (None, 4)]
[(0, 3), (1, 4), (2, None)]
[(0, 4), (3, None), (1, 2)]
[(0, None), (4, 2), (3, 1)]
[(0, 2), (None, 1), (4, 3)]
[(0, 1), (2, 3), (None, 4)]
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top