# Styling RSS w XLS notes
XML files can include both xml-stylesheets (XSLT) and normal stylesheets (CSS).
XSLT files are transformations, and allow you to process an XML doc into an HTML doc (among other things).
In this case, it doesn't matter if we import the CSS here or via /rss/rss.xsl -- it gets applied either way.
The XSLT will output HTML for us, but the HTML content from the RSS feed (i.e., the bodies of posts) must be unescaped.
There's a special attribute (`disable-output-escaping`) which will do that.
However, we need to run some JS, too, because not every browser supports decoding html like that.
* firefox does not seem to support `disable-output-escaping="yes"`, so it requires the JS in rss.js
* chrome does support `disable-output-escaping="yes"`, so don't remove those attrs
The JS works by testing `#cometestme`, and then (if needed) looping over elements matching `[name=decodable]` and basically `el.innerHTML = el.textContent`.
Note, `disable-output-escaping="yes"` is a legacy feature from XSLT v1.0; the new way to do it is with character maps.
When I tried those, they didn't seem to work in firefox (which is when I tried the original JS).
IDK if chrome support character maps, but if it does, then that is a good update to implement. TODO I guess.
Optimal encoding of permutations<p>This is a repost of <a href="https://www.reddit.com/r/AskComputerScience/comments/6gxa3e/how_to_describe_a_permutation_of_a_list_in_as_few/">a post I made to reddit</a> on June 13th 2017. I found it again recently and wanted to document it here.</p>
<h3 id="the-problem-whats-the-optimal-encoding">The Problem (what's the optimal encoding?)</h3>
<blockquote>
<p>You have a known list of n candidates on a ballot. In the act of voting, the voter must number each candidate from 1 to n, using each number once. The voter is thus describing a transformation on the list of candidates into a particular permutation. What is the minimum number of bits needed to exactly describe any order, and the method?</p>
<p>My naive approach is to count (from the left) the number of 'jumps' each candidate must take to arrive in their ranked position <u>skipping any previously ranked candidates</u>.</p>
<p>So if we had 3 candidates: <code>cs = ['c1', 'c2', 'c3']</code> and a list of ranks <code>rs = [2, 3, 1]</code> we'd do this (where <code>zip(cs, rs)</code> matches candidates to ranks):</p>
<ul>
<li>First, <code>c1</code> should be in the 2nd position, which is 1 move from the far left open spot. Since <code>c1</code> <u>could</u> move 2 positions though we need to use 2 bits. Record <code>01</code></li>
<li>Second, 'c2' needs to be in 3rd position, which is also 1 move from the far left open spot (since we skip over the filled position from step 1). Because 'c2' could <u>only</u> move a maximum of 1 jump, we only need 1 bit: record <code>1</code>.</li>
<li>Third, 'c3' trivially fits into the last space, so we need 0 bits.</li>
</ul>
<p>Our final result is <code>011</code>.</p>
<p>To decode the permutation, take N bits in the sequence <code>ceil(log(2, n)), ceil(log(2, n-1)), ceil(log(2, n-2)), ..., ceil(log(2, 2))</code>, which is [2, 1, 0] in our case. Use those bits (in this case <code>['01', '1']</code>) to move each candidate into open spots.</p>
<p>By numerical analysis this method very closely fits O(n Log n) space complexity. (<code>total_bits = 0.89 * n * log(2, n)</code> seems to describe the pattern very closely).</p>
<p>The exact number of bits used will be <code>t = Sigma(i=2, n)( ceil(log(2, i)) )</code>.</p>
<p>It occurs to me that simply listing the positions is <code>O(n log n)</code> too, but the exact number of bits required is <code>t = ceil(log(2, n))*n</code> which is more than my method.</p>
</blockquote>
<p>Reddit user <a href="https://www.reddit.com/user/tilrman">tilrman</a> <a href="https://www.reddit.com/r/AskComputerScience/comments/6gxa3e/how_to_describe_a_permutation_of_a_list_in_as_few/ditwrht/">pointed out</a>:</p>
<blockquote>
<p>With n candidates, there are P(n) = n! permutations. Each voter selects one of these permutations, so a vote can be represented by an integer [0, n!-1].</p>
</blockquote>
<p>2 more comments follow in that branch before I posted my soln</p>
<h3 id="my-solution">My Solution</h3>
<p>clarification: in <code>"base factorial"</code> I'm using <q>base</q> in the same way as <q>base 10</q> (our number system) or <q>base 2</q> (binary numbers).</p>
<blockquote>
<p>Basic idea is to use <a href="https://en.wikipedia.org/wiki/Factorial_number_system">factoradics</a> - sort of like <q>base factorial</q> where the k<sup>th</sup> integer is in [0, k] and to convert back to base 10 you'd multiply it by <code>k!</code>. e.g.: <code>3210</code> is <code>0*1! + 1*2! + 2*3! + 3*4!</code>. Also <code>4321 + 1 = 10000</code> which is <code>1*5! = 120</code> in base 10.</p>
<p>These numbers uniquely enumerate permutations for a list of n unique elements and allow one to 'read' the permutation from the number. e.g. <code>[a, b, c, d, e]</code> in permutation <code>2310</code> leads to <code>[d, c, a, e, b]</code>. Put <code>a</code> in 2+1th free slot, put <code>b</code> in 3+1th free spot, put <code>c</code> in 1+1th free slot, put <code>d</code> in 0+1th free slot, put <code>e</code> in the last spot.</p>
<p>This means we can map all integers in [0, n!-1] to a unique permutation (via modulo-ing and subtracting successive factorials), and back again quickly and efficiently (though arbitrarily sized integers (e.g. in python or haskell) make this much easier than small integers - <code>150!</code> is like 110 bytes long as a raw integer)</p>
<p>Finally a sanity check: for a list of k elements we have a maximum count of <code>(k-1) (k-2) ... (k-k+1)</code> where each bracketed expression is a digit. If we add 1 we end up with 1 followed by k zeros, which corresponds to <code>k!</code>, and so our maximum integer is 1 less at <code>k!-1</code> which is what we expect :)</p>
</blockquote>
historicalprogrammingmaths
https://forum.xk.io/n/10010