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.