This file contains 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
# https://mathspp.com/blog | |
from dataclasses import dataclass | |
@dataclass | |
class Cell: | |
"""Class that represents a Sudoku board cell.""" | |
row: int |
This file contains 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
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) |
This file contains 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
# 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() |
This file contains 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
\documentclass[11pt]{article} | |
\usepackage{fontspec} | |
\setmainfont{APL385 Unicode} | |
\setmonofont{APL385 Unicode}[Scale=MatchLowercase] | |
\begin{document} | |
I just want some ← +-×÷*⍟⌹○!? |⌈⌊⊥⊤⊣⊢ =≠≤<>≥≡≢ ∨∧⍲⍱ ↑↓⊂⊃⊆⌷⍋⍒ ⍳⍸∊⍷∪∩~ /\textbackslash⌿⍀ ,⍪⍴⌽⊖⍉ ¨⍨⍣.∘⍤⍥@ ⍞⎕⍠⌸⌺⌶⍎⍕ ⋄⍝→⍵⍺∇\& ¯⍬ |
This file contains 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 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 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 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 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 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. |
NewerOlder