Refer to the following link:
http://homepage.mac.com/s_lott/books/python/html/p05/p05c05_mahjongg.html
It might help in "determining which tile to throw out" is pretty straight-forward. Given the discards and the nature of the hand, defining a "ready" hand is a matter of determining which potential sets have the most available (non-discarded, not in-play) tiles.
Each tile belongs to one of many possible patterns (pair, up to 3 chows, pung, kong). It's easy to compute the tiles required to fill the pattern. It's easy to reduce the counts of possible tiles based on what's played and what's exposed. The resulting possible patterns can be ranked. Discards are picked from the most-difficult-to-fill combinations.
[See this http://www.amazon.com/Mah-Jong-Handbook-Play-Score/dp/0804838747]