This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ⍝ PROBLEM 9.1, array-oriented solution, faster for the 3 examples provided. | |
| Weights ← { | |
| (⎕IO ⎕ML ⎕WX) ← 0 1 3 | |
| ⍝ Reads the mobile data from the file ⍵ and returns its weights. | |
| ⍝ Monadic function expecting a character vector and returning an integer vector. | |
| ⍝ Returns the weights ordered from left to right, top to bottom. | |
| ⍝ How it works: | |
| ⍝ We will build a square coefficient matrix where each variable is the weight of a left (┌) or right (┐) corner. | |
| ⍝ Let's say n is the number of leafs in the mobile; then n is also the number of pivots and by the lengths of the arms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| :Namespace Mobiles | |
| ⍝ This namespace defines a set of helper functions for the recursive solution of the last problem. | |
| (⎕IO ⎕ML ⎕WX) ← 0 1 3 | |
| ⍝ Monadic function to take a path to a mobile.txt file and return a character matrix. | |
| OpenToMatrix ← {↑⊃⎕nget ⍵ 1} | |
| Parse ← { | |
| (⎕IO ⎕ML ⎕WX) ← 0 1 3 | |
| ⍝ Monadic function to parse (recursively) a mobile in matrix form. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ⍝ PROBLEM 8.1 | |
| Balance ← { | |
| (⎕IO ⎕ML ⎕WX) ← 0 1 3 | |
| ⍝ Given an integer vector ⍵, find a 2-partition with equal sums. | |
| ⍝ Monadic function expecting an integer vector. | |
| ⍝ This algorithm is O(2*≢⍵); we compute possible partitions of ⍵ and check | |
| ⍝ if their sums satisfy the requirement. | |
| ⍝ Return a 2-element vector of integer vectors. | |
| S ← 1⊥⍵ ⍝ Check simple necessary properties: | |
| 2|S: ⍬ ⍝ odd sum can't be evenly split |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ⍝ PROBLEM 7.1 | |
| ⍝ Computes the check digit for a UPC-A barcode. | |
| ⍝ Monadic function expecting an 11-element integer vector. | |
| ⍝ Returns an integer. | |
| CheckDigit ← (10|∘-+.×∘(11⍴3 1)) | |
| ⍝ Decimal repr of the right digits of a UPC-A barcode. | |
| UPCRD ← 114 102 108 66 92 78 80 68 72 116 | |
| ⍝ Bit matrix with one right digit per row. | |
| bUPCRD ← ⍉2∘⊥⍣¯1⊢ UPCRD |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ⍝ PROBLEM 6.1 | |
| Merge ← { | |
| (⎕IO ⎕ML ⎕WX) ← 0 1 3 | |
| ⍝ Merges JSON data from a file named ⍵ into a template file named ⍺. | |
| ⍝ Dyadic function expecting a character vector on the left (template path) and a character vector on the right (data path). | |
| ⍝ Returns a character vector. | |
| t ← ⊃⎕NGET ⍺ | |
| G ← (⎕JSON⊃⎕NGET ⍵)_SafeGet | |
| R ← { | |
| ⍝ Convert matched regex patterns into the appropriate replacements. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ⍝ PROBLEM 5.1 | |
| rr ← { | |
| (⎕IO ⎕ML ⎕WX) ← 0 1 3 | |
| ⍝ Computes how much your investment is worth after cash flows and rate calculations. | |
| ⍝ Dyadic function expecting number vector on the left (deposits/withdrawals) and number vector on the right (corresponding rates). | |
| ⍝ At moment 1 you have r[1] ← ⍺[0] and at the subsequent moments you have r[n] ← ⍺[n-1] + r[n-1]×⍵[n-1]. | |
| ⍝ Expanding the recurrence relation we can determine beforehand all expressions. | |
| ⍝ Assuming ⎕IO←0 and traditional mathematical notation, | |
| ⍝ r[1]←⍺[0], r[2]←⍺[1]+⍺[0]⍵[1], r[3]←⍺[2]+⍺[1]⍵[2]+⍺[0]⍵[2]⍵[1], r[4]←⍺[3]+⍺[2]⍵[3]+⍺[1]⍵[3]⍵[2]+⍺[0]⍵[3]⍵[2]⍵[1] | |
| ⍝ Returns a number. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ⍝ PROBLEM 4.1 | |
| revp ← { | |
| (⎕IO ⎕ML ⎕WX) ← 0 1 3 | |
| ⍝ Find reverse palindromes of lengths between 4 and 12 in a DNA character vector. | |
| ⍝ Monadic function expecting a character vector. | |
| ⍝ A substring is a reverse palindrome if the substring is equal to its reverse complement. | |
| ⍝ The complement of a DNA string changes these pairs: 'A'←→'T', 'C'←→'G'. | |
| ⍝ Returns a 2 column integer matrix as if ⎕IO←1 | |
| (⊢+⍴∘1 0∘⍴) 4 12 _revp ⍵ | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| PastTasksBlast ← { | |
| ⍝ Function to scrape Dyalog's webpage for the URLs of past year's competition problem statements. | |
| ⍝ Monadic function expecting a character vector. | |
| ⍝ Assumes HttpCommand is loaded. | |
| ⍝ Returns a vector of character vectors. | |
| r ← #.HttpCommand.Get ⍵ | |
| r.HttpStatus≠200: 0⍴⊂,' ' | |
| base ← ⊃('<base +href="(.*?)"' ⎕S '\1') r.Data | |
| ('href="(.*?[.]pdf)"' ⎕S (base,'\1')) r.Data | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Steps ← { | |
| (⎕IO ⎕ML ⎕WX) ← 0 1 3 | |
| ⍝ Utility function to generate ranges of numbers. | |
| ⍝ Dyadic function expecting a number on the left (mode) and a 2-element number vector (s f) on the right (steps start and end points). | |
| ⍝ - if ⍺ is negative, return vector with |⌊⍺ equally-sized steps from s to f. | |
| ⍝ - if ⍺ is positive, ⍺ represents the step size. | |
| ⍝ - if ⍺ is 0, return s | |
| ⍝ Returns a vector of numbers between s and f. | |
| ⍺ ← 1 | |
| (s f) ← ⍵ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DiveScore ← { | |
| (⎕IO ⎕ML ⎕WX) ← 0 1 3 | |
| ⍝ Computes the olympic dive score up to two decimal places. | |
| ⍝ Dyadic function expecting a number (dive difficulty) on the left and a vector of numbers on the right (judge scores). | |
| ⍝ ⍵ is assumed to have odd length ≥ 3. | |
| ⍝ Returns a number with, at most, 2 decimal places. | |
| ⍎2⍕⍺×1⊥ (⊂⊃⍨¨3↑(÷∘2-∘3∘≢)⌽⍋) ⍵ | |
| } |