Skip to content

Instantly share code, notes, and snippets.

View rodrigogiraoserrao's full-sized avatar
🐍

Rodrigo Girão Serrão rodrigogiraoserrao

🐍
View GitHub Profile
@rodrigogiraoserrao
rodrigogiraoserrao / P9Phase2_2020.dyalog
Created August 6, 2020 16:19
RGS's array-oriented solution to Problem 9, Phase 2 of 2020 APL competition (see https://mathspp.com/blog/2020-apl-competition for my thoughts on it)
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
@rodrigogiraoserrao
rodrigogiraoserrao / P9Phase2_2020.dyalog
Created August 6, 2020 16:13
RGS's recursive solution to Problem 9, Phase 2 of 2020 APL competition (see https://mathspp.com/blog/2020-apl-competition for my thoughts on it)
: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.
@rodrigogiraoserrao
rodrigogiraoserrao / P8Phase2_2020.dyalog
Created August 6, 2020 16:02
RGS's solution for Problem 8, Phase 2 of 2020 APL competition (see https://mathspp.com/blog/2020-apl-competition for my thoughts on it)
⍝ 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
@rodrigogiraoserrao
rodrigogiraoserrao / P7Phase2_2020.dyalog
Created August 6, 2020 15:55
RGS's solution for Problem 7, Phase 2 of 2020 APL competition (see https://mathspp.com/blog/2020-apl-competition for my thoughts on it)
⍝ 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
@rodrigogiraoserrao
rodrigogiraoserrao / P6Phase2_2020.dyalog
Created August 6, 2020 15:51
RGS's solution for Problem 6, Phase 2 of 2020 APL competition (see https://mathspp.com/blog/2020-apl-competition for my thoughts on it)
⍝ 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.
@rodrigogiraoserrao
rodrigogiraoserrao / P5Phase2_2020.dyalog
Created August 6, 2020 15:47
RGS's solution for Problem 5, Phase 2 of 2020 APL competition (see https://mathspp.com/blog/2020-apl-competition for my thoughts on it)
⍝ 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.
@rodrigogiraoserrao
rodrigogiraoserrao / P4Phase2_2020.dyalog
Last active August 6, 2020 15:32
RGS's solution for Problem 4, Phase 2 of 2020 APL competition (see https://mathspp.com/blog/2020-apl-competition for my thoughts on it)
⍝ 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 ⍵
}
@rodrigogiraoserrao
rodrigogiraoserrao / P3Phase2_2020.dyalog
Created August 6, 2020 15:30
RGS's solution for Problem 3, Phase 2 of 2020 APL competition (see https://mathspp.com/blog/2020-apl-competition for my thoughts on it)
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
}
@rodrigogiraoserrao
rodrigogiraoserrao / P2Phase2_2020.dyalog
Last active August 6, 2020 15:26
RGS's solution for Problem 2, Phase 2 of 2020 APL competition (see https://mathspp.com/blog/2020-apl-competition for my thoughts on it)
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) ← ⍵
@rodrigogiraoserrao
rodrigogiraoserrao / P1Phase2_2020.dyalog
Last active August 6, 2020 15:13
RGS's solution for Problem 1, Phase 2 of 2020 APL competition (see https://mathspp.com/blog/2020-apl-competition for my thoughts on it)
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∘≢)⌽⍋) ⍵
}