Make a package manager
GolfScript, 59 characters
[{;;[]`}:it{|`}:ll{-`}:ve{;$n*}:st];~"#{ARGV*' '}"' '%(-2>~
Implemented the first idea which came into my mind. The database format is simply a GolfScript array representation. Install/remove also handle the case when a program is already installed.
Explanation
# Define four functions for later use ([...]; simply removes them from the stack):
[
{;;[]`}:it # init -> discard arguments, discard db, create empty db
{|`}:ll # install -> add arguments to db (if not already there)
{-`}:ve # remove -> remove all arguments from db
{;$n*}:st # list -> discard arguments, sort, join with newline
];
~ # Parse db given on STDIN
"#{ARGV*' '}"' '% # Parse command line arguments into array
(-2> # Take first argument (command) and the last two chars from it
~ # Execute that command (i.e. "it" for "init", ...)
GolfScript (23 chars)
n%"#{ARGV*'
'}"n/(;^$n*
My database format is the same as the list format.
I interpret the spec to allow assuming that the init
command guarantees no input on stdin and no further command-line arguments, and that the list
command guarantees no further command-line arguments. Then every command does exactly the same thing: xor the supplied database with the 2nd to nth command-line argument.
Golf-Basic 84 for TI-84 Calculators, Character count: 84
:2_R:1_I:0_L:""_Str0i`Nd`Str0l`0i`Ng`Ad`Str0g`0l`Ai`S@N=2:Str0+S_Str0@N=1:""_Str0g`0
Hide the source code. Your teacher might think you're cheating :)
Output
?Init|db
?Install|db
?"CAT, DOG"
?List|db
CAT, DOG
?Remove|db
?"CAT, DOG"
?List|db
?Install|db
?"LOL"
?List|db
LOL