A Python Challenge. Better than rentacoder?
Monday, August 25th, 2008Python programmers love a challenge, so I thought I would throw this one to the lazyweb.
I have an app that generates a large sequence of files, the nature of which is unimportant. What is important is generating a location to store them. To keep the directory structure manageable there should be no more than 100 files plus sub-directories in any given directory. For instance if there are 99 files in a directory, then there is only room for one more file or a directory — bringing the total to 100. There is an additional requirement that the directory structures should be as flat as possible, i.e. foo/bar for the nth item is preferable over foo/bar/baz. To summarize the requirements:
- No more than a total of 100 files plus folders in any given directory.
- Directory structure should be as flat as possible.
- Algorithm should be theoretically unbounded, i.e work for any length of sequence.
- Character length of the path is unimportant.
- Entries should be in Python (but feel free to post solutions in other languages for comparison).
Entries should be a function called make_path, which takes a single parameter, n, which is the number of the file in the sequence (starting at 0). The return value should be a path to the file, such as foo/bar/baz (where baz is a file in directory foo/bar). Feel free to add any extension to the filename, to make it clearer that the path references a file. You can’t rely on the function being called in sequence, so don’t keep any kind of persistent state across calls to make_path (nor any kind of file access).
Wordpress comments tend to mess up formatting with Python code, so please post your entries on Pygments.org or a similar site and a comment to let me know the url.
The winner gets the respect of his/her peers and will be owed a favor by me, because I figured it would be more fun to write this blog entry than solve it myself! ;-) All entries grant the rights for anyone to freely use and modify the code.



