# [Tutor] pytunes (Was __slots__, struct, etc.)

John Fouhy john at fouhy.net
Mon Oct 24 23:29:54 CEST 2005

```For all of them --- not guaranteed that you'll be able to achieve the optimum..

My solution is basically a greedy algorithm: Make a list of (artist,
tracks) pairs, and sort descending by len(tracks).  Then iteratively:
1. Take the next track from the first artist on the list.
2. Move that artist down to position total/len(tracks).

Repeat until all tracks have been selected :-)

Generally it works well, except at the end of the playlist sometimes.
See attachment for my implementation. (uses mmpython, which you can
find on the net)

--
John.

On 21/10/05, Liam Clarke <ml.cyresse at gmail.com> wrote:
> Hmmm, that's interesting.
>
> Obviously, for a playlist with x songs, and n songs by a particular
> artist, the optimum separation between songs would be x/n.
>
> Which would be easy for one artist, but for all of them?
>
> Hmm...
>
> Let's see. First off, let's discount all the artists with only one
> song, they can be the space filler.
>
> Next, you'd have to work out optimal spacing for each artist's songs,
> and try to insert them into a list. If that slot was already taken,
> you'd want to look down and up the list for the next free space,
> choosing whichever maximised the distance. And you'd have to treat
> x[5] - 10 as x[96] for a list x where len(x) == 100.
>
> Err, there's got to be an algorithm for this sorta stuff, but I can't
> understand the big words when I google it... what's your solution?
>
> On 10/21/05, John Fouhy <john at fouhy.net> wrote:
> > Hmm, neat. I don't have any feedback for you on your code, but since
> > you're working with this sort of thing, I have a puzzle for you:
> >
> > Suppose you have a collection of MP3 files.  You want to build a
> > random playlist, subject to the condition that tracks by the same
> > artist are as far apart as possible.  You can assume you have the
> > track / artist data in any structure you like.  How would you do this?
> >
> > (I have a solution, but I don't think it is optimal.  In fact, I'm not
> > sure how to actually define "optimal" here... And so I am interested
> > in how other people would solve this problem :-) )
> >
> > --
> > John.
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: randomizeMusic.py
Url: http://mail.python.org/pipermail/tutor/attachments/20051025/8011a02f/randomizeMusic.asc
```