posted on 2018-03-20
Recall that the symbol Z2 denotes the set of all coordinate pairs with integer components.
A spiral of length n≥1 is a counter-clockwise spiral starting at (0,0) extending to the right to (n,0), wrapping around so as to cover Z2 completely.
Consider the following spiral of length 2.
*--*--*--*--*--*
| |
* *--*--*--* *
| | (0,0) | |
* * x--*--x *
| | (2,0) |
* *--*--*--*--*
|
*--*--*--*--*--* ...
The points of the spiral have a natural ordering. The above spiral can be written as a sequence s where s0=(0,0),s3=(2,1),s1=(1,0),s4=(1,1),s2=(2,0),s5=(0,1), and so on.
There are three challenges. Given an n≥1, do the following.
Given a k≥0, find sk.
Given a point (α,β)∈Z2, find the k such that sk=(α,β).
Print the spiral of length 2 to the terminal by printing the values of k in their correct positions. (And make it look nice by ensuring the numbers are aligned and reasonably spaced out.)
posted on 2018-02-10
The symbol Zn denotes the set of all coordinate n-tuples with integer components. These are one kind of n-dimensional lattice.
Given two points s=(s1,…,sn) and e=(e1,…,en), their taxicab distance is ‖s−e‖1:=n∑i=1|si−ei|.
Given a complex number z, we denote its normalization as N(z):={0if z=0,z/|z|otherwise.
A staircase path between two points, informally, is any path by going only right or up.
Formally, given the starting point s∈Zn and ending point e∈Zn with s≤e (i.e., each component of s is less than or equal to each corresponding component of e), a staircase path is a sequence of ℓ:=1+‖s−e‖1 coordinates σ0,…,σℓ with the following facts:
σ0=s,
s≤σk≤e (in the sense described above), and
exactly one component of σk+1−σk is 1, with the other n−1 components equal to 0.
Denote the set of all staircase paths from s to e as {s→e}.
Part 1: Verify that facts 1–3 imply σℓ=e.
Next, consider the set Tm×n defined as n×m matrices whose entries are complex numbers of unit modulus. For consistency with the above, let's suppose the bottom-left element of each matrix is identified by (1,1) and top-right element is (n,m).
Part 2: Implement the function f:Tm×n→T∪{0} defined by f(M):=N(∑Σ∈{(1,1)→(m,n)}∏(x,y)∈ΣMx,y).
Extra Credit: Generalize f to work on a tensor of any order.
posted on 2018-01-30
In Challenge #15, we wrote a few simple combinators. Here we write a few more.
In addition to combinator practice, this exercise will be written so as to practice math notation comprehension.
For each of these, implement the combinator and write the most general type for that combinator.
Combinator C1 which "flips" the arguments of binary function. It takes f:X×Y→Z to C1f=f′:Y×X→Z such that for all x∈X and y∈Y, f(x,y)=f′(y,x). This combinator is an involution because C21 is the identity.
Combinator C2 which duplicates an argument to a binary function. It takes f:X×X→Z to C2g=f′:X→Z such that for all x∈X, f(x,x)=f(x).
Combinator C3 which takes two functions with the same domain X, and produces a function taking x∈X and returning (f(x),g(x)).
Combinator C4 which takes a function, and produces a curried function of two arguments, such that the second argument supplied is effectively ignored.
Fun Fact: In Haskell, the combinators above are called flip
, join
,
liftM2 (,)
, and (const .)
respectively.