<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def keep_episodes(show, keep):
 """ Delete all but last count episodes in show. """

    deleted = 0
    print('%s Cleaning %s to %s episodes.' % (datestr(), show.title, keep))
    sort = lambda x:x.originallyAvailableAt or x.addedAt
    items = sorted(show.episodes(), key=sort, reverse=True)
    for episode in items[keep:]:
        delete_episode(episode)
        deleted += 1
    return deleted