CodeReportSolutions

From ZCubes Wiki
Jump to navigation Jump to search

Code Report Solutions

Following are some simple solutions to videos found on YouTube. Idea is to make it easier for comparing and learning z^3 and other beautiful languages.

Number of Different Integers in a String

Video: https://www.youtube.com/watch?v=59vAjBS3yZM [APL Wins (vs C++, Java & Python)]

z^3 Solution

s="ad3343sadfsd343434df343443sff";
(s#/[^\d]+/)∪

Result: 3343 343434 343443

# splits string using the postfix regexp pattern. ∪ extracts unique from the results
12 Character z^3 Solution vs. 14 Character APL Solution

Final Value of Variable After Performing Operations

Video: https://www.youtube.com/watch?v=8Njxgy4itts [4 APL Solutions in 10 Minutes!]

z^3 Solution

m=["--x","x++","x++"].print();
[0]@(m!)

Gives answer:

x	[--x,x++,x++]
0	-1	-1	0


m! makes this to a function which is evaluated using [0]@. 0 forms a single input combinatorial argument.
[0..10]@(m!) can be used to loop through a set of initial values
Interesting that this solution has far less characters than APL solution, and  can be made even shorter.

Count Negative Numbers in a Sorted Matrix

Video: https://www.youtube.com/watch?v=pDbDtGn1PXk [LeetCode 176 Problem 1 - Count Negative Numbers in a Sorted Matrix]

z^3 Solution

x=[-2,-1,0;-1,1,3;-1,2,4];
(x⍌).findv(NEGATIVE)#

Gives answer 4

⍌ Flatten Array
.findv Gives result of function call NEGATIVE 
# Counts the result
Also note simpler z^3 Array Notation used to create the variable x (with , and ;). Usual Javascript array notations also work.

Check if a matrix is an X-Matrix (X like diagonals are filled)

Video: https://www.youtube.com/watch?v=8ynsN4nJxzU [APL vs BQN vs J vs Q vs NumPy vs Julia vs R - A comparison of seven different programming languages across a few different language criteria.]

z^3 Solution

m=[2 0 0 1; 0 3 1 0; 0 5 2 0; 4 0 0 2];
n=(m#)[0];
xm=|n| |+| |n|.reverse();
xm|==|(m.$("x!=0?1:0"!))

Gives answer true

m# gives length of array m. 
|n| gives n dimensional Identity Matrix, which is added using |+| to the .reverse() of same array.
|==| is used to compare this to the m, .$ is used to apply the function "x!=0?1:0"! to every element. "x!=0?1:0"! shows how a function can be created from a string with the ! operator.
Also note simpler z^3 Array Notation used to create the variable x (with , (when positive numbers alone are used, the comma is also optional) and ;). Usual Javascript array notations also work.


Sign of Product of an Array

Video: https://www.youtube.com/watch?v=a7CSK7HNEWQ&t=297s [I ❤ APL and Haskell #2 ]

z^3 Solution

a=[-1,-2,-3,4,3,2,1];
∏±a

Gives answer -1

∏ is Product, ± is SIGN function. APL has 3 characters it its solution (selected as the best solution in the video because of its brevity). Interesting that the solution with z^3 has even less characters, just 2.

Powerful Solution to Indexing Problem (1 character in z^3 vs 3 in APL)

Video: https://www.youtube.com/watch?v=ctbGMuakpHk [Python vs 3 Character APL Solution ]

z^3 Solution


a=[0,2,1,5,3,4];
a#a

Gives answer 0 1 2 4 5 3


Actual logic is just # [Hash], which is an index access operator, if operated on any array. For this problem, the answer is just one character (3 to be generous). # operator is far more powerful and is polymorphic depending on the types of arguments it operators on.

Partitioning Odd and Even Numbers in an Array

Video: https://www.youtube.com/watch?v=A16w7qTfT68 [Implementing C++'s std::partition in BQN ]

z^3 Solution


a=[3,1,2,4];
(a ⑂ [ISEVEN,ISODD]) ⍌

Gives answer 2 4 3 1


Fork (⑂) with an array of functions, and flatten the result. Function Array can have any number of predicates/functions.

Rearrange Array to Maximize Prefix Score

Video: https://www.youtube.com/watch?v=mRT-yK2RTdg [Why I Love BQN So Much! (vs Python)]

z^3 Solution

a=[2,-1,0,1,-3,3,-3];
(a ⋱ ≡⧺ SUM)↥

Gives answer 6

Sort by Descending Order (⋱), followed by cumulative row SUM (≡⧺ SUM), followed by MAX (↥)

Hypotenuse

Video: https://www.youtube.com/watch?v=Wah-uQ9ou80 [Python vs Uiua vs BQN]

z^3 Solution

h:=√∑([a,b]^2);
h(3,4)

Gives answer 5

Filter Is Odd From List

Video: https://www.youtube.com/watch?v=XJ3QWOSZ8Nk [Lightning Talk: C++ vs Haskell vs BQN by Conor Hoekstra | Lambda Days 2023]

z^3 Solution

(1..5) ⑂ ISODD

Gives answer 1 3 5

GCD/LCM

Video: https://www.youtube.com/watch?v=UVUjnzpQKUo [1 Problem, 16 Programming Languages (C++ vs Rust vs Haskell vs Python vs APL...)]

z^3 Solution

LCM(20,15)
Gives: 60
GCD(20,15)
Gives: 5
GCD(5..20..5,15)
Gives:
-	GCD
5	15	5
10	15	5
15	15	15
20	15	5