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 / logdict.py
Created September 3, 2021 13:31
A simple Python🐍 3.8+ `dict` subclass that prints a debug message before setting/getting values.
class LogDict(dict):
def __setitem__(self, key, value):
print(f"Setting {key = } with {value = }")
return super().__setitem__(key, value)
def __getitem__(self, key):
print(f"Getting {key = }")
return super().__getitem__(key)
@rodrigogiraoserrao
rodrigogiraoserrao / ocalc.py
Last active July 13, 2021 12:16
One-expression calculator.
# A Tkinter calculator in a single Python *expression*
# See https://www.reddit.com/r/Python/comments/ojab0n/i_see_your_17_loc_calculator_and_i_raise_you_a/
(tk := __import__("tkinter")) and (bt_draw := lambda k, c, l: (bt := tk.Button(w, text=k, command=lambda: bt_press(k), width=6)) and bt.grid(column=c, row=l)) and (update := lambda t: d.config(text=t)) and (bt_press := lambda k: update("") if k == "C" else update(d["text"][:-1]) if k == "<" else update(str(round(eval(d["text"]), 6))) if k == "=" else update(d["text"] + k)) and (w := tk.Tk()) and w.title("TKalc") or (d := tk.Label(w, text="")) and d.grid(column=0, row=0, columnspan=5) or [bt_draw(k, n%4 + 1, n//4 + 1) for n, k in enumerate("()C<789/456*123-.0=+")] and w.mainloop()
\documentclass[11pt]{article}
\usepackage{fontspec}
\setmainfont{APL385 Unicode}
\setmonofont{APL385 Unicode}[Scale=MatchLowercase]
\begin{document}
I just want some ← +-×÷*⍟⌹○!? |⌈⌊⊥⊤⊣⊢ =≠≤<>≥≡≢ ∨∧⍲⍱ ↑↓⊂⊃⊆⌷⍋⍒ ⍳⍸∊⍷∪∩~ /\textbackslash⌿⍀ ,⍪⍴⌽⊖⍉ ¨⍨⍣.∘⍤⍥@ ⍞⎕⍠⌸⌺⌶⍎⍕ ⋄⍝→⍵⍺∇\& ¯⍬
@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 ⍵
}