Okrzyki, przyjaciel! (chaircrusher) wrote,
Okrzyki, przyjaciel!
chaircrusher

On the geek beat

If you install Red Hat Enterprise Linux, as my dept recently did, there's no applications to play mp3 files. Some time ago I made a build of MPlayer which will play some movie files in Firefox that didn't seem supported by RHEL out of the box.

mplayer has zero user interface -- well, you can run it in a shell window and it will interpret keys you type as transpfort controls, but that's a fairly antediluvian UI. But if you're a keyboard jockey, it ain't so bad.

The good thing, though, if you actually use a terminal window and shell commands, is that it's easy to tell mplayer to play a bunch of files:

mplayer *.mp3

Or to play all the files in the current directory and all its subdirectories:

mplayer ` find . -name \*mp3 `

To generate a random playlist, I needed a stream randomizer


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
int main(int argc, char **argv)
{
std::vector<std::string> lines;
std::copy(std::istream_iterator<std::string>(std::cin), std::istream_iterator<std::string>(), std::back_inserter (lines));
std::random_shuffle(lines.begin(),lines.end());
std::copy (lines.begin(), lines.end(), std::ostream_iterator<std::string> (std::cout, "\n"));
}



So now i can use mplayer to generate a random playlist of all the mp3 files in the current directory:

mplayer ` find . -name \*mp3 | randomize `


x09 has a programmer interview question to put interviewees on the spot. The brute force solution I give above uses the Standard Template Library to produce a very concise correct answer to this problem. Unfortunately, it depends on buffering up the whole file before shuffling the lines and writing them back out. Not so good when you feed it a 32-gigabyte list of numbers or something.

Tom's preferred solution only buffers two line of the input stream at a time. Every time you read a line, you either print it out, or, based on a random probability, you print out the saved line and save the current line.

This is the sort of thing my mind rebels against, but last week with clues from x09 I had a solution that unfortunately, I deleted. I tried to replicate it today to no avail, so I guess I don't get the job with him ;-) Maybe he'll favor us with his solution.
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 1 comment