7
\$\begingroup\$

It's time to make a more advanced program (rather than something simple, like printing some shape). You need a package manager for something, I guess.

First of all, if something wasn't defined, you are allowed to do anything, including destroying the computer (but please, don't, unless this will save your characters). After all, you will be using the package manager 100% correctly, so this won't happen. Who cares about security problems? It just has to be fast, like C programs.

The package manager should use STDIN and STDOUT as storage - that is, the database should be read from STDIN, and result database should be output to STDOUT, in true Unix style. The format of database is anything you want, as long you will be able to read it back.

The package manager shall support four commands. Typing unrecognized command is undefined, and anything can happen.

  • init - outputs the empty database to be read by other commands. This shouldn't read the database state.
  • install x y z - installs the programs x y z. If the program already exists, you can do anything
  • remove x y z - removes the programs x y z. If the program doesn't exist, you can do anything.
  • list - shows every program (with new line after every program). The list should be in alphabetical order. This mustn't output the database state.

The packages are only allowed to have lowercase ASCII letters (a-z). Anything else is undefined.

There is an example of example program:

$ ./lolmanager init > db
$ ./lolmanager install cat dog < db > db2
$ ./lolmanager remove dog < db2 > db
$ ./lolmanager list < db
cat
$ ./lolmanager install something else < db > db2
$ ./lolmanager list < db2
cat
else
something
$

The shortest code wins.

\$\endgroup\$
4
  • 4
    \$\begingroup\$ "shows every program (with new line after every program). The list should be in alphabetical order. This shouldn't output the database state." So in effect, this means that separated by newlines, in alphabetical order, is a forbidden db state format? \$\endgroup\$
    – nitro2k01
    Commented Dec 31, 2013 at 17:20
  • \$\begingroup\$ alphabetical order in the description and any order in the example seem to be different. \$\endgroup\$
    – Howard
    Commented Dec 31, 2013 at 17:21
  • \$\begingroup\$ @Howard: You catched me on a small mistake. I couldn't have decided whether list should be ordered or not (by accident). This should be fixed now, it's just a mistake while writing the task. \$\endgroup\$
    – null
    Commented Dec 31, 2013 at 17:22
  • \$\begingroup\$ Similar: Write a program to perform set operations \$\endgroup\$ Commented Jan 1, 2014 at 11:22

5 Answers 5

4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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", ...)
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ 84 84 84 Too much 84 \$\endgroup\$ Commented Mar 14, 2016 at 3:39
  • \$\begingroup\$ @CatsAreFluffy They work on all in the early TI-8X series, notably TI-83s ;) \$\endgroup\$
    – Timtech
    Commented Mar 14, 2016 at 18:23
1
\$\begingroup\$

J (127 108)

Runs as a script (obviously). I could save some characters if I could assume it always receives an end-of-file even for init.

exit echo;,&LF&.>(LF cut stdin^:(-.A e.~<'init')''),`(*/@(~:/~)#[)`((/:{])@[)`[@.('irl'i.{.>2}A)[3}.A=:ARGV
\$\endgroup\$
1
\$\begingroup\$

Ruby, 102 bytes

c,*a=$*
$/=?\0
puts c=~/it/?'':(i=STDIN.gets.split
case c
when/a/
a|i
when/v/
i-a
when/st/
i.sort
end)

Edit: Inspired by Howard, I replaced init, install, remove and list literals for some more general patterns: /it/, /a/, /v/ and /st/.

\$\endgroup\$
6
  • \$\begingroup\$ Only remove has v, so you can shorten the pattern. \$\endgroup\$
    – null
    Commented Dec 31, 2013 at 20:57
  • \$\begingroup\$ You're right, thanks! Also install is the only one containing a. \$\endgroup\$
    – daniero
    Commented Jan 1, 2014 at 14:27
  • \$\begingroup\$ Your program doesn't sort the values when you use list (I just tested it). \$\endgroup\$
    – null
    Commented Jan 1, 2014 at 19:13
  • \$\begingroup\$ @GlitchMr I didn't read the rules properly I guess. Fixed (and shortened). \$\endgroup\$
    – daniero
    Commented Jan 1, 2014 at 21:52
  • \$\begingroup\$ And while shortening, you somehow managed to remove /v/ optimization ;-). \$\endgroup\$
    – null
    Commented Jan 1, 2014 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.