1
\$\begingroup\$

Challenge

You find yourself in a bash terminal, and want to do some simple tests on sqlite3.

So you issue the following command:

echo "CREATE TABLE T (id INT NON NULL PRIMARY KEY)" | sqlite3 test.db

Now you want to insert the first N integers (starting from 0, excluding N) into the table T.

For convenience, you execute the following

N=5 # value arbitrary and large, not known for purposes of the challenge

Now for the example case of N == 5, you would have to execute the following SQL command:

echo "INSERT INTO T VALUES (0), (1), (2), (3), (4);" | sqlite3 test.db

But of course, N is large, so you want to come op with a bash oneliner to dynamically generate and execute the SQL statement to insert these values.


Rules

  • The result of entering the command string into the presented bash teminal session (and pressing enter) should be the modification of test.db as described in the challenge.

  • You must not leave behind any unintended artifacts on disk (ignoring command histories etc.)

  • Any cli tool can be called and considered installed, e.g. python -c, ghci -e, awk, ...

  • The final command string must be a single line of characters enterable in the bash terminal session described above (if we want to get technical, the command string cannot contain a byte with value 10 (LF))

  • Your command string must be valid UTF-8

  • You don't absolutely have to end in | sqlite3 test.db, if the tools you are using have some built in way to write to test.db, that can be used aswell. (Although it might not help you that much, see the scoring).

  • If you are using tools where it is not obvious how to install them for others (let's just say, if the are not available as a Debian Package), please add a link on where to get them.


Scoring

Score shall be based on the following metric, which tries to favor elegant and readable solutions:

  • +100 Points by default so we don't have to look at that many negative scores.

  • -5 points for every character not in the printable ASCII range (values 32-127, )

  • -3 points for every 10 characters above 80 (e.g. 89: -0, 90: -3, 99: -3, 100: -6)

  • -2 points for every use of \

  • -1 point for every use of any of these characters: (){}[]'"` (trying to punish nesting here, sorry LISP fans)

The tie breaker are the upvotes, which I hope will respect the spirit of elegant / readable solutions.

Highest Score wins !


Non competing example solution in Haskell to demonstrate Scoring:

ghci -e 'import Data.List' -e 'putStr $ "insert into T values" ++ (concat $ intersperse "," $ map (\i -> "(" ++ show i ++ ")") [0..'$N']) ++ ";"' | sqlite3 test.db
Default:                  = 100
Non Ascii:        0  * -5 =  -0
Length: 164:      8  * -3 = -24
Backslashes:      1  * -2 =  -2
Special Chars:    24 * -1 = -24
--------------------------------
Total Points:                50  
\$\endgroup\$
7
  • \$\begingroup\$ Welcome to Code Golf! "Most elegant" challenges aren't really considered a good scoring criterion anymore, and I don't remember off the top of my head, but they might be off-topic. Do you think there's a more objective criterion you could use here, like code-golf? \$\endgroup\$ Jul 26 at 18:02
  • 2
    \$\begingroup\$ @Rydwolf Programs Thanks for the welcome! Shouldn't it be fine since there is an objective number based scoring system? \$\endgroup\$
    – ChrisB
    Jul 26 at 18:03
  • 5
    \$\begingroup\$ That said, the seemingly arbitrary choice of point deductions may not be popular. For future reference, we recommend using the Sandbox to get feedback on challenge ideas before posting them to main, especially for challenges that don't use standard [code-golf] scoring rules \$\endgroup\$ Jul 26 at 18:09
  • 1
    \$\begingroup\$ @cairdcoinheringaahing: I tried to make the scoring section more clear, Is that what you had in mind? \$\endgroup\$
    – ChrisB
    Jul 26 at 18:14
  • 2
    \$\begingroup\$ Yep, that, to me, conveys the objectivity of the scoring system in a much better way :) \$\endgroup\$ Jul 26 at 18:15

1 Answer 1

4
\$\begingroup\$

Score 100 (optimal)

echo seq $1Uxargs MRE sqlite3 testNdb Binsert into t values HEMQIB | tr A-Z !-1Iz-~| bash

Attempt This Online!

This is the version below, but encoded so that it contains no literal ()" characters (and with some whitespace removed, so the final thing fits in under 90 characters)

Score 96

seq $1 | xargs -I % sqlite3 test.db "insert into t values (% - 1)"

Attempt This Online!

Gratuitous whitespace, in the spirit of elegance, since it's still well under 90 bytes.

\$\endgroup\$

Your Answer

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

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