-5
\$\begingroup\$

Goal:

The goal of this challenge is to take a simple 6 row by 6 column XML file (without the standard XML header) and parse it out so that the program can return the value of a cell given it's coordinates.

The entry with the shortest overall length by language and overall will be declared the winner.

Additional information:

The XML syntax will always be an HTML table with each inner level being indented by four single spaces. You may assume that every file put into the program will be exactly like the one given below.

If your program uses imports you do not need to include them in the golfed version of your code but you must submit a copy of the program in the explanation that shows all imports used. This explanation will not effect your entries byte count.

Rules:

  • The XML file cannot be hardcoded
  • XML libraries/functions cannot be used

Extra Credit:

  • Can pretty print the table if given the --all flag (20 bytes off of total)
  • Can print all entries of a column given the column number in --col=number (20 bytes off of total)
  • Can print all entries of a row given the row number in --row=number (20 bytes off of total)
  • Can parse any size file as long as #rows = #columns (40 bytes off of total)

XML File:

<table>
    <tr>
        <th>Foo</th>
        <th>Bar</th>
        <th>Baz</th>
        <th>Cat</th>
        <th>Dog</th>
        <th>Grinch</th>
    </tr>
    <tr>
        <th>Something</th>
        <th>Something</th>
        <th>Darkside</th>
        <th>Star</th>
        <th>Wars</th>
        <th>Chicken</th>
    </tr>
    <tr>
        <th>Star</th>
        <th>Trek</th>
        <th>Xbox</th>
        <th>Words</th>
        <th>Too</th>
        <th>Many</th>
    </tr>
    <tr>
        <th>Columns</th>
        <th>Not</th>
        <th>Enough</th>
        <th>Words</th>
        <th>To</th>
        <th>Fill</th>
    </tr>
    <tr>
        <th>Them</th>
        <th>So</th>
        <th>I</th>
        <th>Will</th>
        <th>Do</th>
        <th>My</th>
    </tr>
    <tr>
        <th>Best</th>
        <th>To</th>
        <th>Fill</th>
        <th>Them</th>
        <th>All</th>
        <th>Up</th>
    </tr>
</table>
\$\endgroup\$
  • \$\begingroup\$ Putting aside the ban on golfing languages (this is a golf site after all), I'm not sure any of those bonuses are worth it. Maybe the last one in some languages (esp golf langs, which are banned), but parsing flags and doing something with them will take more than 2 bytes any way I see it. Honestly, the rule setup looks like you're just outsourcing a useful program. \$\endgroup\$ – Geobits Jun 8 '15 at 16:05
  • \$\begingroup\$ I agree with you on the bonuses so I upped their value x10. As for the flags I am going off an observation that most programs are written to where they can accept these so I feel like if they person wants they should be given some incentive to use them. As for outsourcing I don't want to allow golfing languages because I want to see a program in a language that isn't designed to be ridiculously compact. \$\endgroup\$ – Benjamin Williams Jun 8 '15 at 16:23
  • 4
    \$\begingroup\$ You don't have to ban languages for that. I golf in Java personally, and others here use C#, python, etc... If you want a variety of languages, the easiest thing is to just pose an interesting challenge, with less emphasis on "full header/program", "must be able to accept arguments", "no golf langs", etc. Seemingly arbitrary rules (whether you feel them to be arbitrary or not) puts people off answering at all, regardless of language. \$\endgroup\$ – Geobits Jun 8 '15 at 16:28
  • \$\begingroup\$ I see your point, I will be altering the rules to get rid of those restrictions. As for full headers I think those should stay as it means that every byte of your working program is exposed and can be tested. Though I could move that so that it is only required in an explanation and not within the code itself. \$\endgroup\$ – Benjamin Williams Jun 8 '15 at 16:33
  • \$\begingroup\$ If the input file format is so constrained, a single regular expression can give you a given row and a given column inside that row. And for any number of rows and columns \$\endgroup\$ – edc65 Jun 8 '15 at 19:59
2
\$\begingroup\$

JavaScript (ES6), 158 bytes

A function, parameters:

  • xml
  • row number (1 to n)
  • col number (1 to n or 0 to get the whole row as an array)

It works with an xml string with any number of rows and columns (formatted as in the example, no extra tags inside rows or cells).

I don't claim any bonus as the input mode is not what requested (--row=, etc)

Using this same expression in any language that implements regexps (perl maybe) you can have a stand-alone program.

Test running the snipper below (ES6 so Firefox only)

F=(t,r,c)=>(
  m=t.match(RegExp(`(<tr>(\\s*<th>(.*?)</th>\\s*){${c}}[\\s\\S]*?</tr>\\s*){${r}}`,"i")),
  c?m[3] // single cell
  :m[1].split(/<th>(.*?)<\/th>/g).filter((v,i)=>i&1) // clean row value
)  

// test

function test()
{
  var r=+R.value, c=+C.value, t=T.value
  X.value=F(t,r,c)
}
#X { width: 440px; }
#T { width: 440px; height: 10em; }
#R,#C { width: 3em; }
Row: <input id=R value=1> (1 ... n)
Col: <input id=C value=1> (1...n, 0 for whole row)
<button onclick="test()">Get</button>
<br><input readonly id=X>
<br>Xml:<br>
<textarea id=T><table>
    <tr>
        <th>Foo</th>
        <th>Bar</th>
        <th>Baz</th>
        <th>Cat</th>
        <th>Dog</th>
        <th>Grinch</th>
    </tr>
    <tr>
        <th>Something</th>
        <th>Something</th>
        <th>Darkside</th>
        <th>Star</th>
        <th>Wars</th>
        <th>Chicken</th>
    </tr>
    <tr>
        <th>Star</th>
        <th>Trek</th>
        <th>Xbox</th>
        <th>Words</th>
        <th>Too</th>
        <th>Many</th>
    </tr>
    <tr>
        <th>Columns</th>
        <th>Not</th>
        <th>Enough</th>
        <th>Words</th>
        <th>To</th>
        <th>Fill</th>
    </tr>
    <tr>
        <th>Them</th>
        <th>So</th>
        <th>I</th>
        <th>Will</th>
        <th>Do</th>
        <th>My</th>
    </tr>
    <tr>
        <th>Best</th>
        <th>To</th>
        <th>Fill</th>
        <th>Them</th>
        <th>All</th>
        <th>Up</th>
    </tr>
</table></textarea>

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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