From d5446682a9b94271fca33445133ba96144e3d45c Mon Sep 17 00:00:00 2001 From: Dimitri Lozeve Date: Mon, 3 Aug 2020 11:13:13 +0200 Subject: [PATCH] Add annotations for problems 6 and 7 --- _site/atom.xml | 54 +++++++++---- .../dyalog-apl-competition-2020-phase-2.html | 54 +++++++++---- _site/rss.xml | 54 +++++++++---- posts/dyalog-apl-competition-2020-phase-2.org | 75 ++++++++++++++++--- 4 files changed, 180 insertions(+), 57 deletions(-) diff --git a/_site/atom.xml b/_site/atom.xml index 6ac4a4e..b1fe91c 100644 --- a/_site/atom.xml +++ b/_site/atom.xml @@ -131,30 +131,42 @@ rr←{recur←{⍵[1]+⍺×1+⍵[2]} ⋄ 1↓⌽⊃{(⊂(⊃⍵)recur⍺),⍵}/⌽⍺,¨⍵}
⍝ Simply apply the formula for cashflow calculations.
 pv←{+/⍺÷×\1+⍵}
+

TODO

Problem 6 – Merge

-
∇ val←ns getval var
-  :If ''≡var ⍝ literal '@'
-      val←'@'
-  :ElseIf (⊂var)∊ns.⎕NL ¯2
-      val←⍕ns⍎var
-  :Else
-      val←'???'
-  :EndIf
-
-
∇ 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
-
+
∇ 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
+
+

We first read the template and the JSON values from their files. The ⎕NGET function read simple text files, and ⎕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.

+
∇ val←ns getval var
+  :If ''≡var ⍝ literal '@'
+      val←'@'
+  :ElseIf (⊂var)∊ns.⎕NL ¯2
+      val←⍕ns⍎var
+  :Else
+      val←'???'
+  :EndIf
+
+

This function takes the namespace matching the variable names to their respective values, and the name of the variable.

+

Problem 7 – UPC

CheckDigit←{10|-⍵+.×11⍴3 1}
+

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.

⍝ Left and right representations of digits. Decoding
 ⍝ the binary representation from decimal is more
 ⍝ compact than writing everything explicitly.
 lrepr←⍉(7⍴2)⊤13 25 19 61 35 49 47 59 55 11
 rrepr←~¨lrepr
+

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.

∇ bits←WriteUPC digits;left;right
   :If (11=≢digits)∧∧/digits∊0,⍳9
       left←,lrepr[1+6↑digits;]
@@ -164,6 +176,8 @@
       bits←¯1
   :EndIf
 
+

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.

∇ digits←ReadUPC bits
   :If 95≠⍴bits ⍝ incorrect number of bits
       digits←¯1
@@ -180,6 +194,12 @@
       :EndIf
   :EndIf
 
+

Problem 8 – Balancing the Scales

∇ parts←Balance nums;subsets;partitions
   ⍝ This is a brute force solution, running in
@@ -208,6 +228,7 @@
       parts←nums{((⊂,(⊂~))⊃↓⍉⍵)/¨2⍴⊂⍺}partitions
   :EndIf
 
+

TODO

Problem 9 – Upwardly Mobile

∇ weights←Weights filename;mobile;branches;mat
   ⍝ Put your code and comments below here
@@ -224,6 +245,7 @@
   ⍝ integer weights.
   weights←((1∘,)×(∧/÷))mat[;1]⌹1↓[2]mat
 
+

TODO

    :EndNamespace
 :EndNamespace
diff --git a/_site/posts/dyalog-apl-competition-2020-phase-2.html b/_site/posts/dyalog-apl-competition-2020-phase-2.html index fbe43aa..4ed376f 100644 --- a/_site/posts/dyalog-apl-competition-2020-phase-2.html +++ b/_site/posts/dyalog-apl-competition-2020-phase-2.html @@ -165,30 +165,42 @@ rr←{recur←{⍵[1]+⍺×1+⍵[2]} ⋄ 1↓⌽⊃{(⊂(⊃⍵)recur⍺),⍵}/⌽⍺,¨⍵}
⍝ Simply apply the formula for cashflow calculations.
 pv←{+/⍺÷×\1+⍵}
+

TODO

Problem 6 – Merge

-
∇ val←ns getval var
-  :If ''≡var ⍝ literal '@'
-      val←'@'
-  :ElseIf (⊂var)∊ns.⎕NL ¯2
-      val←⍕ns⍎var
-  :Else
-      val←'???'
-  :EndIf
-
-
∇ 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
-
+
∇ 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
+
+

We first read the template and the JSON values from their files. The ⎕NGET function read simple text files, and ⎕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.

+
∇ val←ns getval var
+  :If ''≡var ⍝ literal '@'
+      val←'@'
+  :ElseIf (⊂var)∊ns.⎕NL ¯2
+      val←⍕ns⍎var
+  :Else
+      val←'???'
+  :EndIf
+
+

This function takes the namespace matching the variable names to their respective values, and the name of the variable.

+

Problem 7 – UPC

CheckDigit←{10|-⍵+.×11⍴3 1}
+

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.

⍝ Left and right representations of digits. Decoding
 ⍝ the binary representation from decimal is more
 ⍝ compact than writing everything explicitly.
 lrepr←⍉(7⍴2)⊤13 25 19 61 35 49 47 59 55 11
 rrepr←~¨lrepr
+

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.

∇ bits←WriteUPC digits;left;right
   :If (11=≢digits)∧∧/digits∊0,⍳9
       left←,lrepr[1+6↑digits;]
@@ -198,6 +210,8 @@
       bits←¯1
   :EndIf
 
+

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.

∇ digits←ReadUPC bits
   :If 95≠⍴bits ⍝ incorrect number of bits
       digits←¯1
@@ -214,6 +228,12 @@
       :EndIf
   :EndIf
 
+

Problem 8 – Balancing the Scales

∇ parts←Balance nums;subsets;partitions
   ⍝ This is a brute force solution, running in
@@ -242,6 +262,7 @@
       parts←nums{((⊂,(⊂~))⊃↓⍉⍵)/¨2⍴⊂⍺}partitions
   :EndIf
 
+

TODO

Problem 9 – Upwardly Mobile

∇ weights←Weights filename;mobile;branches;mat
   ⍝ Put your code and comments below here
@@ -258,6 +279,7 @@
   ⍝ integer weights.
   weights←((1∘,)×(∧/÷))mat[;1]⌹1↓[2]mat
 
+

TODO

    :EndNamespace
 :EndNamespace
diff --git a/_site/rss.xml b/_site/rss.xml index cb8f9e5..cc7b717 100644 --- a/_site/rss.xml +++ b/_site/rss.xml @@ -127,30 +127,42 @@ rr←{recur←{⍵[1]+⍺×1+⍵[2]} ⋄ 1↓⌽⊃{(⊂(⊃⍵)recur⍺),⍵}/⌽⍺,¨⍵}
⍝ Simply apply the formula for cashflow calculations.
 pv←{+/⍺÷×\1+⍵}
+

TODO

Problem 6 – Merge

-
∇ val←ns getval var
-  :If ''≡var ⍝ literal '@'
-      val←'@'
-  :ElseIf (⊂var)∊ns.⎕NL ¯2
-      val←⍕ns⍎var
-  :Else
-      val←'???'
-  :EndIf
-
-
∇ 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
-
+
∇ 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
+
+

We first read the template and the JSON values from their files. The ⎕NGET function read simple text files, and ⎕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.

+
∇ val←ns getval var
+  :If ''≡var ⍝ literal '@'
+      val←'@'
+  :ElseIf (⊂var)∊ns.⎕NL ¯2
+      val←⍕ns⍎var
+  :Else
+      val←'???'
+  :EndIf
+
+

This function takes the namespace matching the variable names to their respective values, and the name of the variable.

+

Problem 7 – UPC

CheckDigit←{10|-⍵+.×11⍴3 1}
+

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.

⍝ Left and right representations of digits. Decoding
 ⍝ the binary representation from decimal is more
 ⍝ compact than writing everything explicitly.
 lrepr←⍉(7⍴2)⊤13 25 19 61 35 49 47 59 55 11
 rrepr←~¨lrepr
+

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.

∇ bits←WriteUPC digits;left;right
   :If (11=≢digits)∧∧/digits∊0,⍳9
       left←,lrepr[1+6↑digits;]
@@ -160,6 +172,8 @@
       bits←¯1
   :EndIf
 
+

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.

∇ digits←ReadUPC bits
   :If 95≠⍴bits ⍝ incorrect number of bits
       digits←¯1
@@ -176,6 +190,12 @@
       :EndIf
   :EndIf
 
+

Problem 8 – Balancing the Scales

∇ parts←Balance nums;subsets;partitions
   ⍝ This is a brute force solution, running in
@@ -204,6 +224,7 @@
       parts←nums{((⊂,(⊂~))⊃↓⍉⍵)/¨2⍴⊂⍺}partitions
   :EndIf
 
+

TODO

Problem 9 – Upwardly Mobile

∇ weights←Weights filename;mobile;branches;mat
   ⍝ Put your code and comments below here
@@ -220,6 +241,7 @@
   ⍝ integer weights.
   weights←((1∘,)×(∧/÷))mat[;1]⌹1↓[2]mat
 
+

TODO

    :EndNamespace
 :EndNamespace
diff --git a/posts/dyalog-apl-competition-2020-phase-2.org b/posts/dyalog-apl-competition-2020-phase-2.org index bdacfb8..2f08012 100644 --- a/posts/dyalog-apl-competition-2020-phase-2.org +++ b/posts/dyalog-apl-competition-2020-phase-2.org @@ -209,8 +209,29 @@ operator (~⍣~), with an initial value of 1. pv←{+/⍺÷×\1+⍵} #+end_src +TODO + * 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 ∇ val←ns getval var :If ''≡var ⍝ literal '@' @@ -223,15 +244,13 @@ operator (~⍣~), with an initial value of 1. ∇ #+end_src -#+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 +This function takes the namespace matching the variable names to their +respective values, and the name of the variable. +- If the variable name is empty, we matched the string ~@@~, which + corresponds to a literal ~@~. +- If the variable name is present in the namespace, we query the + namespace to get the required value. +- Otherwise, we have an unknown variable, so we replace it with ~???~. * Problem 7 -- UPC @@ -239,6 +258,15 @@ operator (~⍣~), with an initial value of 1. CheckDigit←{10|-⍵+.×11⍴3 1} #+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 ⍝ Left and right representations of digits. Decoding ⍝ the binary representation from decimal is more @@ -247,6 +275,12 @@ operator (~⍣~), with an initial value of 1. rrepr←~¨lrepr #+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 ∇ bits←WriteUPC digits;left;right :If (11=≢digits)∧∧/digits∊0,⍳9 @@ -259,6 +293,14 @@ operator (~⍣~), with an initial value of 1. ∇ #+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 ∇ digits←ReadUPC bits :If 95≠⍴bits ⍝ incorrect number of bits @@ -278,6 +320,17 @@ operator (~⍣~), with an initial value of 1. ∇ #+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 #+begin_src default @@ -310,6 +363,8 @@ operator (~⍣~), with an initial value of 1. ∇ #+end_src +TODO + * Problem 9 -- Upwardly Mobile #+begin_src default @@ -330,6 +385,8 @@ operator (~⍣~), with an initial value of 1. ∇ #+end_src +TODO + #+begin_src default :EndNamespace :EndNamespace