Add annotations for problems 6 and 7
This commit is contained in:
parent
aff05950e8
commit
d5446682a9
4 changed files with 180 additions and 57 deletions
|
@ -131,30 +131,42 @@
|
||||||
<a class="sourceLine" id="cb10-23" title="23">rr←{recur←{⍵[1]+⍺×1+⍵[2]} ⋄ 1↓⌽⊃{(⊂(⊃⍵)recur⍺),⍵}/⌽⍺,¨⍵}</a></code></pre></div>
|
<a class="sourceLine" id="cb10-23" title="23">rr←{recur←{⍵[1]+⍺×1+⍵[2]} ⋄ 1↓⌽⊃{(⊂(⊃⍵)recur⍺),⍵}/⌽⍺,¨⍵}</a></code></pre></div>
|
||||||
<div class="sourceCode" id="cb11"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb11-1" title="1">⍝ Simply apply the formula for cashflow calculations.</a>
|
<div class="sourceCode" id="cb11"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb11-1" title="1">⍝ Simply apply the formula for cashflow calculations.</a>
|
||||||
<a class="sourceLine" id="cb11-2" title="2">pv←{+/⍺÷×\1+⍵}</a></code></pre></div>
|
<a class="sourceLine" id="cb11-2" title="2">pv←{+/⍺÷×\1+⍵}</a></code></pre></div>
|
||||||
|
<p>TODO</p>
|
||||||
<h2 id="problem-6-merge">Problem 6 – Merge</h2>
|
<h2 id="problem-6-merge">Problem 6 – Merge</h2>
|
||||||
<div class="sourceCode" id="cb12"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb12-1" title="1">∇ val←ns getval var</a>
|
<div class="sourceCode" id="cb12"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb12-1" title="1">∇ text←templateFile Merge jsonFile;template;ns</a>
|
||||||
<a class="sourceLine" id="cb12-2" title="2"> :If ''≡var ⍝ literal '@'</a>
|
<a class="sourceLine" id="cb12-2" title="2"> template←⊃⎕NGET templateFile 1</a>
|
||||||
<a class="sourceLine" id="cb12-3" title="3"> val←'@'</a>
|
<a class="sourceLine" id="cb12-3" title="3"> ns←⎕JSON⊃⎕NGET jsonFile</a>
|
||||||
<a class="sourceLine" id="cb12-4" title="4"> :ElseIf (⊂var)∊ns.⎕NL ¯2</a>
|
<a class="sourceLine" id="cb12-4" title="4"> ⍝ We use a simple regex search and replace on the</a>
|
||||||
<a class="sourceLine" id="cb12-5" title="5"> val←⍕ns⍎var</a>
|
<a class="sourceLine" id="cb12-5" title="5"> ⍝ template.</a>
|
||||||
<a class="sourceLine" id="cb12-6" title="6"> :Else</a>
|
<a class="sourceLine" id="cb12-6" title="6"> text←↑('@[a-zA-Z]*@'⎕R{ns getval ¯1↓1↓⍵.Match})template</a>
|
||||||
<a class="sourceLine" id="cb12-7" title="7"> val←'???'</a>
|
<a class="sourceLine" id="cb12-7" title="7">∇</a></code></pre></div>
|
||||||
<a class="sourceLine" id="cb12-8" title="8"> :EndIf</a>
|
<p>We first read the template and the JSON values from their files. The <a href="https://help.dyalog.com/18.0/index.htm#Language/System%20Functions/nget.htm"><code>⎕NGET</code></a> function read simple text files, and <a href="https://help.dyalog.com/18.0/index.htm#Language/System%20Functions/json.htm"><code>⎕JSON</code></a> extracts the key-value pairs as a namespace.</p>
|
||||||
<a class="sourceLine" id="cb12-9" title="9">∇</a></code></pre></div>
|
<p>Assuming all variable names contain only letters, we match the regex <code>@[a-zA-Z]*@</code> to match variable names enclosed between <code>@</code> symbols. The function <code>getval</code> then returns the appropriate value, and we can replace the variable name in the template.</p>
|
||||||
<div class="sourceCode" id="cb13"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb13-1" title="1">∇ text←templateFile Merge jsonFile;template;ns</a>
|
<div class="sourceCode" id="cb13"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb13-1" title="1">∇ val←ns getval var</a>
|
||||||
<a class="sourceLine" id="cb13-2" title="2"> template←⊃⎕NGET templateFile 1</a>
|
<a class="sourceLine" id="cb13-2" title="2"> :If ''≡var ⍝ literal '@'</a>
|
||||||
<a class="sourceLine" id="cb13-3" title="3"> ns←⎕JSON⊃⎕NGET jsonFile</a>
|
<a class="sourceLine" id="cb13-3" title="3"> val←'@'</a>
|
||||||
<a class="sourceLine" id="cb13-4" title="4"> ⍝ We use a simple regex search and replace on the</a>
|
<a class="sourceLine" id="cb13-4" title="4"> :ElseIf (⊂var)∊ns.⎕NL ¯2</a>
|
||||||
<a class="sourceLine" id="cb13-5" title="5"> ⍝ template.</a>
|
<a class="sourceLine" id="cb13-5" title="5"> val←⍕ns⍎var</a>
|
||||||
<a class="sourceLine" id="cb13-6" title="6"> text←↑('@[a-zA-Z]*@'⎕R{ns getval ¯1↓1↓⍵.Match})template</a>
|
<a class="sourceLine" id="cb13-6" title="6"> :Else</a>
|
||||||
<a class="sourceLine" id="cb13-7" title="7">∇</a></code></pre></div>
|
<a class="sourceLine" id="cb13-7" title="7"> val←'???'</a>
|
||||||
|
<a class="sourceLine" id="cb13-8" title="8"> :EndIf</a>
|
||||||
|
<a class="sourceLine" id="cb13-9" title="9">∇</a></code></pre></div>
|
||||||
|
<p>This function takes the namespace matching the variable names to their respective values, and the name of the variable.</p>
|
||||||
|
<ul>
|
||||||
|
<li>If the variable name is empty, we matched the string <code>@@</code>, which corresponds to a literal <code>@</code>.</li>
|
||||||
|
<li>If the variable name is present in the namespace, we query the namespace to get the required value.</li>
|
||||||
|
<li>Otherwise, we have an unknown variable, so we replace it with <code>???</code>.</li>
|
||||||
|
</ul>
|
||||||
<h2 id="problem-7-upc">Problem 7 – UPC</h2>
|
<h2 id="problem-7-upc">Problem 7 – UPC</h2>
|
||||||
<div class="sourceCode" id="cb14"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb14-1" title="1">CheckDigit←{10|-⍵+.×11⍴3 1}</a></code></pre></div>
|
<div class="sourceCode" id="cb14"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb14-1" title="1">CheckDigit←{10|-⍵+.×11⍴3 1}</a></code></pre></div>
|
||||||
|
<p>The check digit satisfies the equation <span class="math display">\[ 3 x_{1}+x_{2}+3 x_{3}+x_{4}+3 x_{5}+x_{6}+3 x_{7}+x_{8}+3 x_{9}+x_{10}+3 x_{11}+x_{12} \equiv 0 \bmod 10, \]</span> therefore, <span class="math display">\[ x_{12} \equiv -(3 x_{1}+x_{2}+3 x_{3}+x_{4}+3 x_{5}+x_{6}+3 x_{7}+x_{8}+3 x_{9}+x_{10}+3 x_{11}) \bmod 10. \]</span></p>
|
||||||
|
<p>Translated to APL, we just take the dot product between the first 11 digits of the barcode with <code>11⍴3 1</code>, negate it, and take the remainder by 10.</p>
|
||||||
<div class="sourceCode" id="cb15"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb15-1" title="1">⍝ Left and right representations of digits. Decoding</a>
|
<div class="sourceCode" id="cb15"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb15-1" title="1">⍝ Left and right representations of digits. Decoding</a>
|
||||||
<a class="sourceLine" id="cb15-2" title="2">⍝ the binary representation from decimal is more</a>
|
<a class="sourceLine" id="cb15-2" title="2">⍝ the binary representation from decimal is more</a>
|
||||||
<a class="sourceLine" id="cb15-3" title="3">⍝ compact than writing everything explicitly.</a>
|
<a class="sourceLine" id="cb15-3" title="3">⍝ compact than writing everything explicitly.</a>
|
||||||
<a class="sourceLine" id="cb15-4" title="4">lrepr←⍉(7⍴2)⊤13 25 19 61 35 49 47 59 55 11</a>
|
<a class="sourceLine" id="cb15-4" title="4">lrepr←⍉(7⍴2)⊤13 25 19 61 35 49 47 59 55 11</a>
|
||||||
<a class="sourceLine" id="cb15-5" title="5">rrepr←~¨lrepr</a></code></pre></div>
|
<a class="sourceLine" id="cb15-5" title="5">rrepr←~¨lrepr</a></code></pre></div>
|
||||||
|
<p>For the second task, the first thing we need to do is save the representation of digits. To save space, I did not encode the binary representation explicitly, instead using a decimal representation that I then decode in base 2. The right representation is just the bitwise negation.</p>
|
||||||
<div class="sourceCode" id="cb16"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb16-1" title="1">∇ bits←WriteUPC digits;left;right</a>
|
<div class="sourceCode" id="cb16"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb16-1" title="1">∇ bits←WriteUPC digits;left;right</a>
|
||||||
<a class="sourceLine" id="cb16-2" title="2"> :If (11=≢digits)∧∧/digits∊0,⍳9</a>
|
<a class="sourceLine" id="cb16-2" title="2"> :If (11=≢digits)∧∧/digits∊0,⍳9</a>
|
||||||
<a class="sourceLine" id="cb16-3" title="3"> left←,lrepr[1+6↑digits;]</a>
|
<a class="sourceLine" id="cb16-3" title="3"> left←,lrepr[1+6↑digits;]</a>
|
||||||
|
@ -164,6 +176,8 @@
|
||||||
<a class="sourceLine" id="cb16-7" title="7"> bits←¯1</a>
|
<a class="sourceLine" id="cb16-7" title="7"> bits←¯1</a>
|
||||||
<a class="sourceLine" id="cb16-8" title="8"> :EndIf</a>
|
<a class="sourceLine" id="cb16-8" title="8"> :EndIf</a>
|
||||||
<a class="sourceLine" id="cb16-9" title="9">∇</a></code></pre></div>
|
<a class="sourceLine" id="cb16-9" title="9">∇</a></code></pre></div>
|
||||||
|
<p>First of all, if the vector <code>digits</code> does not have exactly 11 elements, all between 0 and 9, it is an error and we return <code>¯1</code>.</p>
|
||||||
|
<p>Then, we take the first 6 digits and encode them with <code>lrepr</code>, and the last 5 digits plus the check digit encoded with <code>rrepr</code>. In each case, adding 1 is necessary because <code>⎕IO←1</code>. We return the final bit array with the required beginning, middle, and end guard patterns.</p>
|
||||||
<div class="sourceCode" id="cb17"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb17-1" title="1">∇ digits←ReadUPC bits</a>
|
<div class="sourceCode" id="cb17"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb17-1" title="1">∇ digits←ReadUPC bits</a>
|
||||||
<a class="sourceLine" id="cb17-2" title="2"> :If 95≠⍴bits ⍝ incorrect number of bits</a>
|
<a class="sourceLine" id="cb17-2" title="2"> :If 95≠⍴bits ⍝ incorrect number of bits</a>
|
||||||
<a class="sourceLine" id="cb17-3" title="3"> digits←¯1</a>
|
<a class="sourceLine" id="cb17-3" title="3"> digits←¯1</a>
|
||||||
|
@ -180,6 +194,12 @@
|
||||||
<a class="sourceLine" id="cb17-14" title="14"> :EndIf</a>
|
<a class="sourceLine" id="cb17-14" title="14"> :EndIf</a>
|
||||||
<a class="sourceLine" id="cb17-15" title="15"> :EndIf</a>
|
<a class="sourceLine" id="cb17-15" title="15"> :EndIf</a>
|
||||||
<a class="sourceLine" id="cb17-16" title="16">∇</a></code></pre></div>
|
<a class="sourceLine" id="cb17-16" title="16">∇</a></code></pre></div>
|
||||||
|
<ul>
|
||||||
|
<li>If we don’t have the correct number of bits, we return <code>¯1</code>.</li>
|
||||||
|
<li>We test the first digit for its parity, to determine if its actually a left representation. If it’s not, we reverse the bit array.</li>
|
||||||
|
<li>Then, we take the bit array representing the right digits (<code>¯42↑¯3↓bits</code>), separate the different digits using <a href="https://help.dyalog.com/18.0/index.htm#Language/Primitive%20Functions/Partition.htm">Partition</a> (<code>⊆</code>), and look up each of them in the <code>rrepr</code> vector using <a href="https://help.dyalog.com/18.0/index.htm#Language/Primitive%20Functions/Index%20Of.htm">Index Of</a> (<code>⍳</code>). We do the same for the left digits.</li>
|
||||||
|
<li>Final checks for the range of the digits (i.e., if the representations could not be found in the <code>lrepr</code> and <code>rrepr</code> vectors), and for the check digit.</li>
|
||||||
|
</ul>
|
||||||
<h2 id="problem-8-balancing-the-scales">Problem 8 – Balancing the Scales</h2>
|
<h2 id="problem-8-balancing-the-scales">Problem 8 – Balancing the Scales</h2>
|
||||||
<div class="sourceCode" id="cb18"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb18-1" title="1">∇ parts←Balance nums;subsets;partitions</a>
|
<div class="sourceCode" id="cb18"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb18-1" title="1">∇ parts←Balance nums;subsets;partitions</a>
|
||||||
<a class="sourceLine" id="cb18-2" title="2"> ⍝ This is a brute force solution, running in</a>
|
<a class="sourceLine" id="cb18-2" title="2"> ⍝ This is a brute force solution, running in</a>
|
||||||
|
@ -208,6 +228,7 @@
|
||||||
<a class="sourceLine" id="cb18-25" title="25"> parts←nums{((⊂,(⊂~))⊃↓⍉⍵)/¨2⍴⊂⍺}partitions</a>
|
<a class="sourceLine" id="cb18-25" title="25"> parts←nums{((⊂,(⊂~))⊃↓⍉⍵)/¨2⍴⊂⍺}partitions</a>
|
||||||
<a class="sourceLine" id="cb18-26" title="26"> :EndIf</a>
|
<a class="sourceLine" id="cb18-26" title="26"> :EndIf</a>
|
||||||
<a class="sourceLine" id="cb18-27" title="27">∇</a></code></pre></div>
|
<a class="sourceLine" id="cb18-27" title="27">∇</a></code></pre></div>
|
||||||
|
<p>TODO</p>
|
||||||
<h2 id="problem-9-upwardly-mobile">Problem 9 – Upwardly Mobile</h2>
|
<h2 id="problem-9-upwardly-mobile">Problem 9 – Upwardly Mobile</h2>
|
||||||
<div class="sourceCode" id="cb19"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb19-1" title="1">∇ weights←Weights filename;mobile;branches;mat</a>
|
<div class="sourceCode" id="cb19"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb19-1" title="1">∇ weights←Weights filename;mobile;branches;mat</a>
|
||||||
<a class="sourceLine" id="cb19-2" title="2"> ⍝ Put your code and comments below here</a>
|
<a class="sourceLine" id="cb19-2" title="2"> ⍝ Put your code and comments below here</a>
|
||||||
|
@ -224,6 +245,7 @@
|
||||||
<a class="sourceLine" id="cb19-13" title="13"> ⍝ integer weights.</a>
|
<a class="sourceLine" id="cb19-13" title="13"> ⍝ integer weights.</a>
|
||||||
<a class="sourceLine" id="cb19-14" title="14"> weights←((1∘,)×(∧/÷))mat[;1]⌹1↓[2]mat</a>
|
<a class="sourceLine" id="cb19-14" title="14"> weights←((1∘,)×(∧/÷))mat[;1]⌹1↓[2]mat</a>
|
||||||
<a class="sourceLine" id="cb19-15" title="15">∇</a></code></pre></div>
|
<a class="sourceLine" id="cb19-15" title="15">∇</a></code></pre></div>
|
||||||
|
<p>TODO</p>
|
||||||
<div class="sourceCode" id="cb20"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb20-1" title="1"> :EndNamespace</a>
|
<div class="sourceCode" id="cb20"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb20-1" title="1"> :EndNamespace</a>
|
||||||
<a class="sourceLine" id="cb20-2" title="2">:EndNamespace</a></code></pre></div>
|
<a class="sourceLine" id="cb20-2" title="2">:EndNamespace</a></code></pre></div>
|
||||||
</section>
|
</section>
|
||||||
|
|
|
@ -165,30 +165,42 @@
|
||||||
<a class="sourceLine" id="cb10-23" title="23">rr←{recur←{⍵[1]+⍺×1+⍵[2]} ⋄ 1↓⌽⊃{(⊂(⊃⍵)recur⍺),⍵}/⌽⍺,¨⍵}</a></code></pre></div>
|
<a class="sourceLine" id="cb10-23" title="23">rr←{recur←{⍵[1]+⍺×1+⍵[2]} ⋄ 1↓⌽⊃{(⊂(⊃⍵)recur⍺),⍵}/⌽⍺,¨⍵}</a></code></pre></div>
|
||||||
<div class="sourceCode" id="cb11"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb11-1" title="1">⍝ Simply apply the formula for cashflow calculations.</a>
|
<div class="sourceCode" id="cb11"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb11-1" title="1">⍝ Simply apply the formula for cashflow calculations.</a>
|
||||||
<a class="sourceLine" id="cb11-2" title="2">pv←{+/⍺÷×\1+⍵}</a></code></pre></div>
|
<a class="sourceLine" id="cb11-2" title="2">pv←{+/⍺÷×\1+⍵}</a></code></pre></div>
|
||||||
|
<p>TODO</p>
|
||||||
<h2 id="problem-6-merge">Problem 6 – Merge</h2>
|
<h2 id="problem-6-merge">Problem 6 – Merge</h2>
|
||||||
<div class="sourceCode" id="cb12"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb12-1" title="1">∇ val←ns getval var</a>
|
<div class="sourceCode" id="cb12"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb12-1" title="1">∇ text←templateFile Merge jsonFile;template;ns</a>
|
||||||
<a class="sourceLine" id="cb12-2" title="2"> :If ''≡var ⍝ literal '@'</a>
|
<a class="sourceLine" id="cb12-2" title="2"> template←⊃⎕NGET templateFile 1</a>
|
||||||
<a class="sourceLine" id="cb12-3" title="3"> val←'@'</a>
|
<a class="sourceLine" id="cb12-3" title="3"> ns←⎕JSON⊃⎕NGET jsonFile</a>
|
||||||
<a class="sourceLine" id="cb12-4" title="4"> :ElseIf (⊂var)∊ns.⎕NL ¯2</a>
|
<a class="sourceLine" id="cb12-4" title="4"> ⍝ We use a simple regex search and replace on the</a>
|
||||||
<a class="sourceLine" id="cb12-5" title="5"> val←⍕ns⍎var</a>
|
<a class="sourceLine" id="cb12-5" title="5"> ⍝ template.</a>
|
||||||
<a class="sourceLine" id="cb12-6" title="6"> :Else</a>
|
<a class="sourceLine" id="cb12-6" title="6"> text←↑('@[a-zA-Z]*@'⎕R{ns getval ¯1↓1↓⍵.Match})template</a>
|
||||||
<a class="sourceLine" id="cb12-7" title="7"> val←'???'</a>
|
<a class="sourceLine" id="cb12-7" title="7">∇</a></code></pre></div>
|
||||||
<a class="sourceLine" id="cb12-8" title="8"> :EndIf</a>
|
<p>We first read the template and the JSON values from their files. The <a href="https://help.dyalog.com/18.0/index.htm#Language/System%20Functions/nget.htm"><code>⎕NGET</code></a> function read simple text files, and <a href="https://help.dyalog.com/18.0/index.htm#Language/System%20Functions/json.htm"><code>⎕JSON</code></a> extracts the key-value pairs as a namespace.</p>
|
||||||
<a class="sourceLine" id="cb12-9" title="9">∇</a></code></pre></div>
|
<p>Assuming all variable names contain only letters, we match the regex <code>@[a-zA-Z]*@</code> to match variable names enclosed between <code>@</code> symbols. The function <code>getval</code> then returns the appropriate value, and we can replace the variable name in the template.</p>
|
||||||
<div class="sourceCode" id="cb13"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb13-1" title="1">∇ text←templateFile Merge jsonFile;template;ns</a>
|
<div class="sourceCode" id="cb13"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb13-1" title="1">∇ val←ns getval var</a>
|
||||||
<a class="sourceLine" id="cb13-2" title="2"> template←⊃⎕NGET templateFile 1</a>
|
<a class="sourceLine" id="cb13-2" title="2"> :If ''≡var ⍝ literal '@'</a>
|
||||||
<a class="sourceLine" id="cb13-3" title="3"> ns←⎕JSON⊃⎕NGET jsonFile</a>
|
<a class="sourceLine" id="cb13-3" title="3"> val←'@'</a>
|
||||||
<a class="sourceLine" id="cb13-4" title="4"> ⍝ We use a simple regex search and replace on the</a>
|
<a class="sourceLine" id="cb13-4" title="4"> :ElseIf (⊂var)∊ns.⎕NL ¯2</a>
|
||||||
<a class="sourceLine" id="cb13-5" title="5"> ⍝ template.</a>
|
<a class="sourceLine" id="cb13-5" title="5"> val←⍕ns⍎var</a>
|
||||||
<a class="sourceLine" id="cb13-6" title="6"> text←↑('@[a-zA-Z]*@'⎕R{ns getval ¯1↓1↓⍵.Match})template</a>
|
<a class="sourceLine" id="cb13-6" title="6"> :Else</a>
|
||||||
<a class="sourceLine" id="cb13-7" title="7">∇</a></code></pre></div>
|
<a class="sourceLine" id="cb13-7" title="7"> val←'???'</a>
|
||||||
|
<a class="sourceLine" id="cb13-8" title="8"> :EndIf</a>
|
||||||
|
<a class="sourceLine" id="cb13-9" title="9">∇</a></code></pre></div>
|
||||||
|
<p>This function takes the namespace matching the variable names to their respective values, and the name of the variable.</p>
|
||||||
|
<ul>
|
||||||
|
<li>If the variable name is empty, we matched the string <code>@@</code>, which corresponds to a literal <code>@</code>.</li>
|
||||||
|
<li>If the variable name is present in the namespace, we query the namespace to get the required value.</li>
|
||||||
|
<li>Otherwise, we have an unknown variable, so we replace it with <code>???</code>.</li>
|
||||||
|
</ul>
|
||||||
<h2 id="problem-7-upc">Problem 7 – UPC</h2>
|
<h2 id="problem-7-upc">Problem 7 – UPC</h2>
|
||||||
<div class="sourceCode" id="cb14"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb14-1" title="1">CheckDigit←{10|-⍵+.×11⍴3 1}</a></code></pre></div>
|
<div class="sourceCode" id="cb14"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb14-1" title="1">CheckDigit←{10|-⍵+.×11⍴3 1}</a></code></pre></div>
|
||||||
|
<p>The check digit satisfies the equation <span class="math display">\[ 3 x_{1}+x_{2}+3 x_{3}+x_{4}+3 x_{5}+x_{6}+3 x_{7}+x_{8}+3 x_{9}+x_{10}+3 x_{11}+x_{12} \equiv 0 \bmod 10, \]</span> therefore, <span class="math display">\[ x_{12} \equiv -(3 x_{1}+x_{2}+3 x_{3}+x_{4}+3 x_{5}+x_{6}+3 x_{7}+x_{8}+3 x_{9}+x_{10}+3 x_{11}) \bmod 10. \]</span></p>
|
||||||
|
<p>Translated to APL, we just take the dot product between the first 11 digits of the barcode with <code>11⍴3 1</code>, negate it, and take the remainder by 10.</p>
|
||||||
<div class="sourceCode" id="cb15"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb15-1" title="1">⍝ Left and right representations of digits. Decoding</a>
|
<div class="sourceCode" id="cb15"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb15-1" title="1">⍝ Left and right representations of digits. Decoding</a>
|
||||||
<a class="sourceLine" id="cb15-2" title="2">⍝ the binary representation from decimal is more</a>
|
<a class="sourceLine" id="cb15-2" title="2">⍝ the binary representation from decimal is more</a>
|
||||||
<a class="sourceLine" id="cb15-3" title="3">⍝ compact than writing everything explicitly.</a>
|
<a class="sourceLine" id="cb15-3" title="3">⍝ compact than writing everything explicitly.</a>
|
||||||
<a class="sourceLine" id="cb15-4" title="4">lrepr←⍉(7⍴2)⊤13 25 19 61 35 49 47 59 55 11</a>
|
<a class="sourceLine" id="cb15-4" title="4">lrepr←⍉(7⍴2)⊤13 25 19 61 35 49 47 59 55 11</a>
|
||||||
<a class="sourceLine" id="cb15-5" title="5">rrepr←~¨lrepr</a></code></pre></div>
|
<a class="sourceLine" id="cb15-5" title="5">rrepr←~¨lrepr</a></code></pre></div>
|
||||||
|
<p>For the second task, the first thing we need to do is save the representation of digits. To save space, I did not encode the binary representation explicitly, instead using a decimal representation that I then decode in base 2. The right representation is just the bitwise negation.</p>
|
||||||
<div class="sourceCode" id="cb16"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb16-1" title="1">∇ bits←WriteUPC digits;left;right</a>
|
<div class="sourceCode" id="cb16"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb16-1" title="1">∇ bits←WriteUPC digits;left;right</a>
|
||||||
<a class="sourceLine" id="cb16-2" title="2"> :If (11=≢digits)∧∧/digits∊0,⍳9</a>
|
<a class="sourceLine" id="cb16-2" title="2"> :If (11=≢digits)∧∧/digits∊0,⍳9</a>
|
||||||
<a class="sourceLine" id="cb16-3" title="3"> left←,lrepr[1+6↑digits;]</a>
|
<a class="sourceLine" id="cb16-3" title="3"> left←,lrepr[1+6↑digits;]</a>
|
||||||
|
@ -198,6 +210,8 @@
|
||||||
<a class="sourceLine" id="cb16-7" title="7"> bits←¯1</a>
|
<a class="sourceLine" id="cb16-7" title="7"> bits←¯1</a>
|
||||||
<a class="sourceLine" id="cb16-8" title="8"> :EndIf</a>
|
<a class="sourceLine" id="cb16-8" title="8"> :EndIf</a>
|
||||||
<a class="sourceLine" id="cb16-9" title="9">∇</a></code></pre></div>
|
<a class="sourceLine" id="cb16-9" title="9">∇</a></code></pre></div>
|
||||||
|
<p>First of all, if the vector <code>digits</code> does not have exactly 11 elements, all between 0 and 9, it is an error and we return <code>¯1</code>.</p>
|
||||||
|
<p>Then, we take the first 6 digits and encode them with <code>lrepr</code>, and the last 5 digits plus the check digit encoded with <code>rrepr</code>. In each case, adding 1 is necessary because <code>⎕IO←1</code>. We return the final bit array with the required beginning, middle, and end guard patterns.</p>
|
||||||
<div class="sourceCode" id="cb17"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb17-1" title="1">∇ digits←ReadUPC bits</a>
|
<div class="sourceCode" id="cb17"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb17-1" title="1">∇ digits←ReadUPC bits</a>
|
||||||
<a class="sourceLine" id="cb17-2" title="2"> :If 95≠⍴bits ⍝ incorrect number of bits</a>
|
<a class="sourceLine" id="cb17-2" title="2"> :If 95≠⍴bits ⍝ incorrect number of bits</a>
|
||||||
<a class="sourceLine" id="cb17-3" title="3"> digits←¯1</a>
|
<a class="sourceLine" id="cb17-3" title="3"> digits←¯1</a>
|
||||||
|
@ -214,6 +228,12 @@
|
||||||
<a class="sourceLine" id="cb17-14" title="14"> :EndIf</a>
|
<a class="sourceLine" id="cb17-14" title="14"> :EndIf</a>
|
||||||
<a class="sourceLine" id="cb17-15" title="15"> :EndIf</a>
|
<a class="sourceLine" id="cb17-15" title="15"> :EndIf</a>
|
||||||
<a class="sourceLine" id="cb17-16" title="16">∇</a></code></pre></div>
|
<a class="sourceLine" id="cb17-16" title="16">∇</a></code></pre></div>
|
||||||
|
<ul>
|
||||||
|
<li>If we don’t have the correct number of bits, we return <code>¯1</code>.</li>
|
||||||
|
<li>We test the first digit for its parity, to determine if its actually a left representation. If it’s not, we reverse the bit array.</li>
|
||||||
|
<li>Then, we take the bit array representing the right digits (<code>¯42↑¯3↓bits</code>), separate the different digits using <a href="https://help.dyalog.com/18.0/index.htm#Language/Primitive%20Functions/Partition.htm">Partition</a> (<code>⊆</code>), and look up each of them in the <code>rrepr</code> vector using <a href="https://help.dyalog.com/18.0/index.htm#Language/Primitive%20Functions/Index%20Of.htm">Index Of</a> (<code>⍳</code>). We do the same for the left digits.</li>
|
||||||
|
<li>Final checks for the range of the digits (i.e., if the representations could not be found in the <code>lrepr</code> and <code>rrepr</code> vectors), and for the check digit.</li>
|
||||||
|
</ul>
|
||||||
<h2 id="problem-8-balancing-the-scales">Problem 8 – Balancing the Scales</h2>
|
<h2 id="problem-8-balancing-the-scales">Problem 8 – Balancing the Scales</h2>
|
||||||
<div class="sourceCode" id="cb18"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb18-1" title="1">∇ parts←Balance nums;subsets;partitions</a>
|
<div class="sourceCode" id="cb18"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb18-1" title="1">∇ parts←Balance nums;subsets;partitions</a>
|
||||||
<a class="sourceLine" id="cb18-2" title="2"> ⍝ This is a brute force solution, running in</a>
|
<a class="sourceLine" id="cb18-2" title="2"> ⍝ This is a brute force solution, running in</a>
|
||||||
|
@ -242,6 +262,7 @@
|
||||||
<a class="sourceLine" id="cb18-25" title="25"> parts←nums{((⊂,(⊂~))⊃↓⍉⍵)/¨2⍴⊂⍺}partitions</a>
|
<a class="sourceLine" id="cb18-25" title="25"> parts←nums{((⊂,(⊂~))⊃↓⍉⍵)/¨2⍴⊂⍺}partitions</a>
|
||||||
<a class="sourceLine" id="cb18-26" title="26"> :EndIf</a>
|
<a class="sourceLine" id="cb18-26" title="26"> :EndIf</a>
|
||||||
<a class="sourceLine" id="cb18-27" title="27">∇</a></code></pre></div>
|
<a class="sourceLine" id="cb18-27" title="27">∇</a></code></pre></div>
|
||||||
|
<p>TODO</p>
|
||||||
<h2 id="problem-9-upwardly-mobile">Problem 9 – Upwardly Mobile</h2>
|
<h2 id="problem-9-upwardly-mobile">Problem 9 – Upwardly Mobile</h2>
|
||||||
<div class="sourceCode" id="cb19"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb19-1" title="1">∇ weights←Weights filename;mobile;branches;mat</a>
|
<div class="sourceCode" id="cb19"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb19-1" title="1">∇ weights←Weights filename;mobile;branches;mat</a>
|
||||||
<a class="sourceLine" id="cb19-2" title="2"> ⍝ Put your code and comments below here</a>
|
<a class="sourceLine" id="cb19-2" title="2"> ⍝ Put your code and comments below here</a>
|
||||||
|
@ -258,6 +279,7 @@
|
||||||
<a class="sourceLine" id="cb19-13" title="13"> ⍝ integer weights.</a>
|
<a class="sourceLine" id="cb19-13" title="13"> ⍝ integer weights.</a>
|
||||||
<a class="sourceLine" id="cb19-14" title="14"> weights←((1∘,)×(∧/÷))mat[;1]⌹1↓[2]mat</a>
|
<a class="sourceLine" id="cb19-14" title="14"> weights←((1∘,)×(∧/÷))mat[;1]⌹1↓[2]mat</a>
|
||||||
<a class="sourceLine" id="cb19-15" title="15">∇</a></code></pre></div>
|
<a class="sourceLine" id="cb19-15" title="15">∇</a></code></pre></div>
|
||||||
|
<p>TODO</p>
|
||||||
<div class="sourceCode" id="cb20"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb20-1" title="1"> :EndNamespace</a>
|
<div class="sourceCode" id="cb20"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb20-1" title="1"> :EndNamespace</a>
|
||||||
<a class="sourceLine" id="cb20-2" title="2">:EndNamespace</a></code></pre></div>
|
<a class="sourceLine" id="cb20-2" title="2">:EndNamespace</a></code></pre></div>
|
||||||
</section>
|
</section>
|
||||||
|
|
|
@ -127,30 +127,42 @@
|
||||||
<a class="sourceLine" id="cb10-23" title="23">rr←{recur←{⍵[1]+⍺×1+⍵[2]} ⋄ 1↓⌽⊃{(⊂(⊃⍵)recur⍺),⍵}/⌽⍺,¨⍵}</a></code></pre></div>
|
<a class="sourceLine" id="cb10-23" title="23">rr←{recur←{⍵[1]+⍺×1+⍵[2]} ⋄ 1↓⌽⊃{(⊂(⊃⍵)recur⍺),⍵}/⌽⍺,¨⍵}</a></code></pre></div>
|
||||||
<div class="sourceCode" id="cb11"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb11-1" title="1">⍝ Simply apply the formula for cashflow calculations.</a>
|
<div class="sourceCode" id="cb11"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb11-1" title="1">⍝ Simply apply the formula for cashflow calculations.</a>
|
||||||
<a class="sourceLine" id="cb11-2" title="2">pv←{+/⍺÷×\1+⍵}</a></code></pre></div>
|
<a class="sourceLine" id="cb11-2" title="2">pv←{+/⍺÷×\1+⍵}</a></code></pre></div>
|
||||||
|
<p>TODO</p>
|
||||||
<h2 id="problem-6-merge">Problem 6 – Merge</h2>
|
<h2 id="problem-6-merge">Problem 6 – Merge</h2>
|
||||||
<div class="sourceCode" id="cb12"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb12-1" title="1">∇ val←ns getval var</a>
|
<div class="sourceCode" id="cb12"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb12-1" title="1">∇ text←templateFile Merge jsonFile;template;ns</a>
|
||||||
<a class="sourceLine" id="cb12-2" title="2"> :If ''≡var ⍝ literal '@'</a>
|
<a class="sourceLine" id="cb12-2" title="2"> template←⊃⎕NGET templateFile 1</a>
|
||||||
<a class="sourceLine" id="cb12-3" title="3"> val←'@'</a>
|
<a class="sourceLine" id="cb12-3" title="3"> ns←⎕JSON⊃⎕NGET jsonFile</a>
|
||||||
<a class="sourceLine" id="cb12-4" title="4"> :ElseIf (⊂var)∊ns.⎕NL ¯2</a>
|
<a class="sourceLine" id="cb12-4" title="4"> ⍝ We use a simple regex search and replace on the</a>
|
||||||
<a class="sourceLine" id="cb12-5" title="5"> val←⍕ns⍎var</a>
|
<a class="sourceLine" id="cb12-5" title="5"> ⍝ template.</a>
|
||||||
<a class="sourceLine" id="cb12-6" title="6"> :Else</a>
|
<a class="sourceLine" id="cb12-6" title="6"> text←↑('@[a-zA-Z]*@'⎕R{ns getval ¯1↓1↓⍵.Match})template</a>
|
||||||
<a class="sourceLine" id="cb12-7" title="7"> val←'???'</a>
|
<a class="sourceLine" id="cb12-7" title="7">∇</a></code></pre></div>
|
||||||
<a class="sourceLine" id="cb12-8" title="8"> :EndIf</a>
|
<p>We first read the template and the JSON values from their files. The <a href="https://help.dyalog.com/18.0/index.htm#Language/System%20Functions/nget.htm"><code>⎕NGET</code></a> function read simple text files, and <a href="https://help.dyalog.com/18.0/index.htm#Language/System%20Functions/json.htm"><code>⎕JSON</code></a> extracts the key-value pairs as a namespace.</p>
|
||||||
<a class="sourceLine" id="cb12-9" title="9">∇</a></code></pre></div>
|
<p>Assuming all variable names contain only letters, we match the regex <code>@[a-zA-Z]*@</code> to match variable names enclosed between <code>@</code> symbols. The function <code>getval</code> then returns the appropriate value, and we can replace the variable name in the template.</p>
|
||||||
<div class="sourceCode" id="cb13"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb13-1" title="1">∇ text←templateFile Merge jsonFile;template;ns</a>
|
<div class="sourceCode" id="cb13"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb13-1" title="1">∇ val←ns getval var</a>
|
||||||
<a class="sourceLine" id="cb13-2" title="2"> template←⊃⎕NGET templateFile 1</a>
|
<a class="sourceLine" id="cb13-2" title="2"> :If ''≡var ⍝ literal '@'</a>
|
||||||
<a class="sourceLine" id="cb13-3" title="3"> ns←⎕JSON⊃⎕NGET jsonFile</a>
|
<a class="sourceLine" id="cb13-3" title="3"> val←'@'</a>
|
||||||
<a class="sourceLine" id="cb13-4" title="4"> ⍝ We use a simple regex search and replace on the</a>
|
<a class="sourceLine" id="cb13-4" title="4"> :ElseIf (⊂var)∊ns.⎕NL ¯2</a>
|
||||||
<a class="sourceLine" id="cb13-5" title="5"> ⍝ template.</a>
|
<a class="sourceLine" id="cb13-5" title="5"> val←⍕ns⍎var</a>
|
||||||
<a class="sourceLine" id="cb13-6" title="6"> text←↑('@[a-zA-Z]*@'⎕R{ns getval ¯1↓1↓⍵.Match})template</a>
|
<a class="sourceLine" id="cb13-6" title="6"> :Else</a>
|
||||||
<a class="sourceLine" id="cb13-7" title="7">∇</a></code></pre></div>
|
<a class="sourceLine" id="cb13-7" title="7"> val←'???'</a>
|
||||||
|
<a class="sourceLine" id="cb13-8" title="8"> :EndIf</a>
|
||||||
|
<a class="sourceLine" id="cb13-9" title="9">∇</a></code></pre></div>
|
||||||
|
<p>This function takes the namespace matching the variable names to their respective values, and the name of the variable.</p>
|
||||||
|
<ul>
|
||||||
|
<li>If the variable name is empty, we matched the string <code>@@</code>, which corresponds to a literal <code>@</code>.</li>
|
||||||
|
<li>If the variable name is present in the namespace, we query the namespace to get the required value.</li>
|
||||||
|
<li>Otherwise, we have an unknown variable, so we replace it with <code>???</code>.</li>
|
||||||
|
</ul>
|
||||||
<h2 id="problem-7-upc">Problem 7 – UPC</h2>
|
<h2 id="problem-7-upc">Problem 7 – UPC</h2>
|
||||||
<div class="sourceCode" id="cb14"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb14-1" title="1">CheckDigit←{10|-⍵+.×11⍴3 1}</a></code></pre></div>
|
<div class="sourceCode" id="cb14"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb14-1" title="1">CheckDigit←{10|-⍵+.×11⍴3 1}</a></code></pre></div>
|
||||||
|
<p>The check digit satisfies the equation <span class="math display">\[ 3 x_{1}+x_{2}+3 x_{3}+x_{4}+3 x_{5}+x_{6}+3 x_{7}+x_{8}+3 x_{9}+x_{10}+3 x_{11}+x_{12} \equiv 0 \bmod 10, \]</span> therefore, <span class="math display">\[ x_{12} \equiv -(3 x_{1}+x_{2}+3 x_{3}+x_{4}+3 x_{5}+x_{6}+3 x_{7}+x_{8}+3 x_{9}+x_{10}+3 x_{11}) \bmod 10. \]</span></p>
|
||||||
|
<p>Translated to APL, we just take the dot product between the first 11 digits of the barcode with <code>11⍴3 1</code>, negate it, and take the remainder by 10.</p>
|
||||||
<div class="sourceCode" id="cb15"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb15-1" title="1">⍝ Left and right representations of digits. Decoding</a>
|
<div class="sourceCode" id="cb15"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb15-1" title="1">⍝ Left and right representations of digits. Decoding</a>
|
||||||
<a class="sourceLine" id="cb15-2" title="2">⍝ the binary representation from decimal is more</a>
|
<a class="sourceLine" id="cb15-2" title="2">⍝ the binary representation from decimal is more</a>
|
||||||
<a class="sourceLine" id="cb15-3" title="3">⍝ compact than writing everything explicitly.</a>
|
<a class="sourceLine" id="cb15-3" title="3">⍝ compact than writing everything explicitly.</a>
|
||||||
<a class="sourceLine" id="cb15-4" title="4">lrepr←⍉(7⍴2)⊤13 25 19 61 35 49 47 59 55 11</a>
|
<a class="sourceLine" id="cb15-4" title="4">lrepr←⍉(7⍴2)⊤13 25 19 61 35 49 47 59 55 11</a>
|
||||||
<a class="sourceLine" id="cb15-5" title="5">rrepr←~¨lrepr</a></code></pre></div>
|
<a class="sourceLine" id="cb15-5" title="5">rrepr←~¨lrepr</a></code></pre></div>
|
||||||
|
<p>For the second task, the first thing we need to do is save the representation of digits. To save space, I did not encode the binary representation explicitly, instead using a decimal representation that I then decode in base 2. The right representation is just the bitwise negation.</p>
|
||||||
<div class="sourceCode" id="cb16"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb16-1" title="1">∇ bits←WriteUPC digits;left;right</a>
|
<div class="sourceCode" id="cb16"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb16-1" title="1">∇ bits←WriteUPC digits;left;right</a>
|
||||||
<a class="sourceLine" id="cb16-2" title="2"> :If (11=≢digits)∧∧/digits∊0,⍳9</a>
|
<a class="sourceLine" id="cb16-2" title="2"> :If (11=≢digits)∧∧/digits∊0,⍳9</a>
|
||||||
<a class="sourceLine" id="cb16-3" title="3"> left←,lrepr[1+6↑digits;]</a>
|
<a class="sourceLine" id="cb16-3" title="3"> left←,lrepr[1+6↑digits;]</a>
|
||||||
|
@ -160,6 +172,8 @@
|
||||||
<a class="sourceLine" id="cb16-7" title="7"> bits←¯1</a>
|
<a class="sourceLine" id="cb16-7" title="7"> bits←¯1</a>
|
||||||
<a class="sourceLine" id="cb16-8" title="8"> :EndIf</a>
|
<a class="sourceLine" id="cb16-8" title="8"> :EndIf</a>
|
||||||
<a class="sourceLine" id="cb16-9" title="9">∇</a></code></pre></div>
|
<a class="sourceLine" id="cb16-9" title="9">∇</a></code></pre></div>
|
||||||
|
<p>First of all, if the vector <code>digits</code> does not have exactly 11 elements, all between 0 and 9, it is an error and we return <code>¯1</code>.</p>
|
||||||
|
<p>Then, we take the first 6 digits and encode them with <code>lrepr</code>, and the last 5 digits plus the check digit encoded with <code>rrepr</code>. In each case, adding 1 is necessary because <code>⎕IO←1</code>. We return the final bit array with the required beginning, middle, and end guard patterns.</p>
|
||||||
<div class="sourceCode" id="cb17"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb17-1" title="1">∇ digits←ReadUPC bits</a>
|
<div class="sourceCode" id="cb17"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb17-1" title="1">∇ digits←ReadUPC bits</a>
|
||||||
<a class="sourceLine" id="cb17-2" title="2"> :If 95≠⍴bits ⍝ incorrect number of bits</a>
|
<a class="sourceLine" id="cb17-2" title="2"> :If 95≠⍴bits ⍝ incorrect number of bits</a>
|
||||||
<a class="sourceLine" id="cb17-3" title="3"> digits←¯1</a>
|
<a class="sourceLine" id="cb17-3" title="3"> digits←¯1</a>
|
||||||
|
@ -176,6 +190,12 @@
|
||||||
<a class="sourceLine" id="cb17-14" title="14"> :EndIf</a>
|
<a class="sourceLine" id="cb17-14" title="14"> :EndIf</a>
|
||||||
<a class="sourceLine" id="cb17-15" title="15"> :EndIf</a>
|
<a class="sourceLine" id="cb17-15" title="15"> :EndIf</a>
|
||||||
<a class="sourceLine" id="cb17-16" title="16">∇</a></code></pre></div>
|
<a class="sourceLine" id="cb17-16" title="16">∇</a></code></pre></div>
|
||||||
|
<ul>
|
||||||
|
<li>If we don’t have the correct number of bits, we return <code>¯1</code>.</li>
|
||||||
|
<li>We test the first digit for its parity, to determine if its actually a left representation. If it’s not, we reverse the bit array.</li>
|
||||||
|
<li>Then, we take the bit array representing the right digits (<code>¯42↑¯3↓bits</code>), separate the different digits using <a href="https://help.dyalog.com/18.0/index.htm#Language/Primitive%20Functions/Partition.htm">Partition</a> (<code>⊆</code>), and look up each of them in the <code>rrepr</code> vector using <a href="https://help.dyalog.com/18.0/index.htm#Language/Primitive%20Functions/Index%20Of.htm">Index Of</a> (<code>⍳</code>). We do the same for the left digits.</li>
|
||||||
|
<li>Final checks for the range of the digits (i.e., if the representations could not be found in the <code>lrepr</code> and <code>rrepr</code> vectors), and for the check digit.</li>
|
||||||
|
</ul>
|
||||||
<h2 id="problem-8-balancing-the-scales">Problem 8 – Balancing the Scales</h2>
|
<h2 id="problem-8-balancing-the-scales">Problem 8 – Balancing the Scales</h2>
|
||||||
<div class="sourceCode" id="cb18"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb18-1" title="1">∇ parts←Balance nums;subsets;partitions</a>
|
<div class="sourceCode" id="cb18"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb18-1" title="1">∇ parts←Balance nums;subsets;partitions</a>
|
||||||
<a class="sourceLine" id="cb18-2" title="2"> ⍝ This is a brute force solution, running in</a>
|
<a class="sourceLine" id="cb18-2" title="2"> ⍝ This is a brute force solution, running in</a>
|
||||||
|
@ -204,6 +224,7 @@
|
||||||
<a class="sourceLine" id="cb18-25" title="25"> parts←nums{((⊂,(⊂~))⊃↓⍉⍵)/¨2⍴⊂⍺}partitions</a>
|
<a class="sourceLine" id="cb18-25" title="25"> parts←nums{((⊂,(⊂~))⊃↓⍉⍵)/¨2⍴⊂⍺}partitions</a>
|
||||||
<a class="sourceLine" id="cb18-26" title="26"> :EndIf</a>
|
<a class="sourceLine" id="cb18-26" title="26"> :EndIf</a>
|
||||||
<a class="sourceLine" id="cb18-27" title="27">∇</a></code></pre></div>
|
<a class="sourceLine" id="cb18-27" title="27">∇</a></code></pre></div>
|
||||||
|
<p>TODO</p>
|
||||||
<h2 id="problem-9-upwardly-mobile">Problem 9 – Upwardly Mobile</h2>
|
<h2 id="problem-9-upwardly-mobile">Problem 9 – Upwardly Mobile</h2>
|
||||||
<div class="sourceCode" id="cb19"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb19-1" title="1">∇ weights←Weights filename;mobile;branches;mat</a>
|
<div class="sourceCode" id="cb19"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb19-1" title="1">∇ weights←Weights filename;mobile;branches;mat</a>
|
||||||
<a class="sourceLine" id="cb19-2" title="2"> ⍝ Put your code and comments below here</a>
|
<a class="sourceLine" id="cb19-2" title="2"> ⍝ Put your code and comments below here</a>
|
||||||
|
@ -220,6 +241,7 @@
|
||||||
<a class="sourceLine" id="cb19-13" title="13"> ⍝ integer weights.</a>
|
<a class="sourceLine" id="cb19-13" title="13"> ⍝ integer weights.</a>
|
||||||
<a class="sourceLine" id="cb19-14" title="14"> weights←((1∘,)×(∧/÷))mat[;1]⌹1↓[2]mat</a>
|
<a class="sourceLine" id="cb19-14" title="14"> weights←((1∘,)×(∧/÷))mat[;1]⌹1↓[2]mat</a>
|
||||||
<a class="sourceLine" id="cb19-15" title="15">∇</a></code></pre></div>
|
<a class="sourceLine" id="cb19-15" title="15">∇</a></code></pre></div>
|
||||||
|
<p>TODO</p>
|
||||||
<div class="sourceCode" id="cb20"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb20-1" title="1"> :EndNamespace</a>
|
<div class="sourceCode" id="cb20"><pre class="sourceCode default"><code class="sourceCode default"><a class="sourceLine" id="cb20-1" title="1"> :EndNamespace</a>
|
||||||
<a class="sourceLine" id="cb20-2" title="2">:EndNamespace</a></code></pre></div>
|
<a class="sourceLine" id="cb20-2" title="2">:EndNamespace</a></code></pre></div>
|
||||||
</section>
|
</section>
|
||||||
|
|
|
@ -209,8 +209,29 @@ operator (~⍣~), with an initial value of 1.
|
||||||
pv←{+/⍺÷×\1+⍵}
|
pv←{+/⍺÷×\1+⍵}
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
TODO
|
||||||
|
|
||||||
* Problem 6 -- Merge
|
* Problem 6 -- Merge
|
||||||
|
|
||||||
|
#+begin_src default
|
||||||
|
∇ text←templateFile Merge jsonFile;template;ns
|
||||||
|
template←⊃⎕NGET templateFile 1
|
||||||
|
ns←⎕JSON⊃⎕NGET jsonFile
|
||||||
|
⍝ We use a simple regex search and replace on the
|
||||||
|
⍝ template.
|
||||||
|
text←↑('@[a-zA-Z]*@'⎕R{ns getval ¯1↓1↓⍵.Match})template
|
||||||
|
∇
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
We first read the template and the JSON values from their files. The
|
||||||
|
[[https://help.dyalog.com/18.0/index.htm#Language/System%20Functions/nget.htm][~⎕NGET~]] function read simple text files, and [[https://help.dyalog.com/18.0/index.htm#Language/System%20Functions/json.htm][~⎕JSON~]] extracts the
|
||||||
|
key-value pairs as a namespace.
|
||||||
|
|
||||||
|
Assuming all variable names contain only letters, we match the regex
|
||||||
|
~@[a-zA-Z]*@~ to match variable names enclosed between ~@~
|
||||||
|
symbols. The function ~getval~ then returns the appropriate value, and
|
||||||
|
we can replace the variable name in the template.
|
||||||
|
|
||||||
#+begin_src default
|
#+begin_src default
|
||||||
∇ val←ns getval var
|
∇ val←ns getval var
|
||||||
:If ''≡var ⍝ literal '@'
|
:If ''≡var ⍝ literal '@'
|
||||||
|
@ -223,15 +244,13 @@ operator (~⍣~), with an initial value of 1.
|
||||||
∇
|
∇
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
#+begin_src default
|
This function takes the namespace matching the variable names to their
|
||||||
∇ text←templateFile Merge jsonFile;template;ns
|
respective values, and the name of the variable.
|
||||||
template←⊃⎕NGET templateFile 1
|
- If the variable name is empty, we matched the string ~@@~, which
|
||||||
ns←⎕JSON⊃⎕NGET jsonFile
|
corresponds to a literal ~@~.
|
||||||
⍝ We use a simple regex search and replace on the
|
- If the variable name is present in the namespace, we query the
|
||||||
⍝ template.
|
namespace to get the required value.
|
||||||
text←↑('@[a-zA-Z]*@'⎕R{ns getval ¯1↓1↓⍵.Match})template
|
- Otherwise, we have an unknown variable, so we replace it with ~???~.
|
||||||
∇
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
* Problem 7 -- UPC
|
* Problem 7 -- UPC
|
||||||
|
|
||||||
|
@ -239,6 +258,15 @@ operator (~⍣~), with an initial value of 1.
|
||||||
CheckDigit←{10|-⍵+.×11⍴3 1}
|
CheckDigit←{10|-⍵+.×11⍴3 1}
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
The check digit satisfies the equation
|
||||||
|
\[ 3 x_{1}+x_{2}+3 x_{3}+x_{4}+3 x_{5}+x_{6}+3 x_{7}+x_{8}+3 x_{9}+x_{10}+3 x_{11}+x_{12} \equiv 0 \bmod 10, \]
|
||||||
|
therefore,
|
||||||
|
\[ x_{12} \equiv -(3 x_{1}+x_{2}+3 x_{3}+x_{4}+3 x_{5}+x_{6}+3 x_{7}+x_{8}+3 x_{9}+x_{10}+3 x_{11}) \bmod 10. \]
|
||||||
|
|
||||||
|
Translated to APL, we just take the dot product between the first 11
|
||||||
|
digits of the barcode with ~11⍴3 1~, negate it, and take the remainder
|
||||||
|
by 10.
|
||||||
|
|
||||||
#+begin_src default
|
#+begin_src default
|
||||||
⍝ Left and right representations of digits. Decoding
|
⍝ Left and right representations of digits. Decoding
|
||||||
⍝ the binary representation from decimal is more
|
⍝ the binary representation from decimal is more
|
||||||
|
@ -247,6 +275,12 @@ operator (~⍣~), with an initial value of 1.
|
||||||
rrepr←~¨lrepr
|
rrepr←~¨lrepr
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
For the second task, the first thing we need to do is save the
|
||||||
|
representation of digits. To save space, I did not encode the binary
|
||||||
|
representation explicitly, instead using a decimal representation that
|
||||||
|
I then decode in base 2. The right representation is just the
|
||||||
|
bitwise negation.
|
||||||
|
|
||||||
#+begin_src default
|
#+begin_src default
|
||||||
∇ bits←WriteUPC digits;left;right
|
∇ bits←WriteUPC digits;left;right
|
||||||
:If (11=≢digits)∧∧/digits∊0,⍳9
|
:If (11=≢digits)∧∧/digits∊0,⍳9
|
||||||
|
@ -259,6 +293,14 @@ operator (~⍣~), with an initial value of 1.
|
||||||
∇
|
∇
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
First of all, if the vector ~digits~ does not have exactly 11
|
||||||
|
elements, all between 0 and 9, it is an error and we return ~¯1~.
|
||||||
|
|
||||||
|
Then, we take the first 6 digits and encode them with ~lrepr~, and the
|
||||||
|
last 5 digits plus the check digit encoded with ~rrepr~. In each case,
|
||||||
|
adding 1 is necessary because ~⎕IO←1~. We return the final bit array
|
||||||
|
with the required beginning, middle, and end guard patterns.
|
||||||
|
|
||||||
#+begin_src default
|
#+begin_src default
|
||||||
∇ digits←ReadUPC bits
|
∇ digits←ReadUPC bits
|
||||||
:If 95≠⍴bits ⍝ incorrect number of bits
|
:If 95≠⍴bits ⍝ incorrect number of bits
|
||||||
|
@ -278,6 +320,17 @@ operator (~⍣~), with an initial value of 1.
|
||||||
∇
|
∇
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
- If we don't have the correct number of bits, we return ~¯1~.
|
||||||
|
- We test the first digit for its parity, to determine if its actually
|
||||||
|
a left representation. If it's not, we reverse the bit array.
|
||||||
|
- Then, we take the bit array representing the right digits
|
||||||
|
(~¯42↑¯3↓bits~), separate the different digits using [[https://help.dyalog.com/18.0/index.htm#Language/Primitive%20Functions/Partition.htm][Partition]]
|
||||||
|
(~⊆~), and look up each of them in the ~rrepr~ vector using [[https://help.dyalog.com/18.0/index.htm#Language/Primitive%20Functions/Index%20Of.htm][Index Of]]
|
||||||
|
(~⍳~). We do the same for the left digits.
|
||||||
|
- Final checks for the range of the digits (i.e., if the
|
||||||
|
representations could not be found in the ~lrepr~ and ~rrepr~
|
||||||
|
vectors), and for the check digit.
|
||||||
|
|
||||||
* Problem 8 -- Balancing the Scales
|
* Problem 8 -- Balancing the Scales
|
||||||
|
|
||||||
#+begin_src default
|
#+begin_src default
|
||||||
|
@ -310,6 +363,8 @@ operator (~⍣~), with an initial value of 1.
|
||||||
∇
|
∇
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
TODO
|
||||||
|
|
||||||
* Problem 9 -- Upwardly Mobile
|
* Problem 9 -- Upwardly Mobile
|
||||||
|
|
||||||
#+begin_src default
|
#+begin_src default
|
||||||
|
@ -330,6 +385,8 @@ operator (~⍣~), with an initial value of 1.
|
||||||
∇
|
∇
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
TODO
|
||||||
|
|
||||||
#+begin_src default
|
#+begin_src default
|
||||||
:EndNamespace
|
:EndNamespace
|
||||||
:EndNamespace
|
:EndNamespace
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue