Better display of problems and solutions
This commit is contained in:
parent
7edc20f556
commit
02a1a96ed6
2 changed files with 18 additions and 30 deletions
|
@ -23,9 +23,7 @@ integer so that its absolute value is less or equal to ~≢Y~, splits
|
|||
and the first vector contains the remaining elements.
|
||||
#+end_quote
|
||||
|
||||
#+begin_src default
|
||||
split←(0>⊣)⌽((⊂↑),(⊂↓))
|
||||
#+end_src
|
||||
*Solution:* ~(0>⊣)⌽((⊂↑),(⊂↓))~
|
||||
|
||||
There are three nested trains here. The first one, ~((⊂↑),(⊂↓))~, uses
|
||||
the two functions [[https://help.dyalog.com/18.0/index.htm#Language/Primitive%20Functions/Take.htm][Take]] (~↑~) and [[https://help.dyalog.com/18.0/index.htm#Language/Primitive%20Functions/Drop.htm][Drop]] (~↓~) to build a nested array
|
||||
|
@ -73,9 +71,7 @@ character, like the result of ~'UTF-8'∘⎕UCS¨'UTF-8'∘⎕UCS~ but does not
|
|||
use any system functions (names beginning with ~⎕~)
|
||||
#+end_quote
|
||||
|
||||
#+begin_src default
|
||||
characters←{(~⍵∊127+⍳64)⊂⍵}
|
||||
#+end_src
|
||||
*Solution:* ~{(~⍵∊127+⍳64)⊂⍵}~
|
||||
|
||||
First, we build a binary array from the string, encoding each
|
||||
continuation character as 0, and all the others as 1. Next, we can use
|
||||
|
@ -95,9 +91,7 @@ scalar or non-empty vector representing a valid character Excel column
|
|||
identifier between A and XFD, returns the corresponding column number
|
||||
#+end_quote
|
||||
|
||||
#+begin_src default
|
||||
columns←26⊥⎕A∘⍳
|
||||
#+end_src
|
||||
*Solution:* ~26⊥⎕A∘⍳~
|
||||
|
||||
We use the alphabet ~⎕A~ and [[https://help.dyalog.com/latest/#Language/Primitive%20Functions/Index%20Of.htm][Index Of]] (~⍳~) to compute the index in
|
||||
the alphabet of every character. As a train, this can be done by
|
||||
|
@ -116,9 +110,7 @@ indicates that the corresponding year is a leap year (0 otherwise).
|
|||
A leap year algorithm can be found [[https://en.wikipedia.org/wiki/Leap_year#Algorithm][here]].
|
||||
#+end_quote
|
||||
|
||||
#+begin_src default
|
||||
leap←1 3∊⍨(0+.=400 100 4∘.|⊢)
|
||||
#+end_src
|
||||
*Solution:* ~1 3∊⍨(0+.=400 100 4∘.|⊢)~
|
||||
|
||||
According to the algorithm, a year is a leap year in two situations:
|
||||
- if it is divisible by 4, but not 100 (and therefore not 400),
|
||||
|
@ -139,9 +131,7 @@ vector of the integers from the first element of the right argument to
|
|||
the second, inclusively.
|
||||
#+end_quote
|
||||
|
||||
#+begin_src default
|
||||
stepping←{(⊃⍵)+(-×-/⍵)×0,⍳|-/⍵}
|
||||
#+end_src
|
||||
*Solution:* ~{(⊃⍵)+(-×-/⍵)×0,⍳|-/⍵}~
|
||||
|
||||
First, we have to compute the range of the output, which is the
|
||||
absolute value of the difference between the two integers ~|-/⍵~. From
|
||||
|
@ -161,9 +151,7 @@ right argument so any elements equal to the left argument come first
|
|||
while all other elements keep their order.
|
||||
#+end_quote
|
||||
|
||||
#+begin_src default
|
||||
movefront←{⍵[⍋⍺≠⍵]}
|
||||
#+end_src
|
||||
*Solution:* ~{⍵[⍋⍺≠⍵]}~
|
||||
|
||||
~⍺≠⍵~ will return a binary vector marking as 0 all elements equal to
|
||||
the left argument. Using this index to sort in the usual way with
|
||||
|
@ -187,9 +175,7 @@ that you want to query, returns 1 if all of the codes in the left
|
|||
argument are found in the right argument (0 otherwise).
|
||||
#+end_quote
|
||||
|
||||
#+begin_src default
|
||||
bits←{f←⍸∘⌽(2∘⊥⍣¯1)⋄∧/(f⍺)∊f⍵}
|
||||
#+end_src
|
||||
*Solution:* ~{f←⍸∘⌽(2∘⊥⍣¯1)⋄∧/(f⍺)∊f⍵}~
|
||||
|
||||
The difficult part is to find the set of states for an integer. We
|
||||
need a function that will return ~1 8 128~ (or an equivalent
|
||||
|
@ -215,9 +201,7 @@ Write a function that takes a single integer greater than or equal to
|
|||
integer is a zigzag number, 0 otherwise.
|
||||
#+end_quote
|
||||
|
||||
#+begin_src default
|
||||
zigzag←∧/2=∘|2-/∘×2-/(10∘⊥⍣¯1)
|
||||
#+end_src
|
||||
*Solution:* ~∧/2=∘|2-/∘×2-/(10∘⊥⍣¯1)~
|
||||
|
||||
TODO
|
||||
|
||||
|
@ -233,9 +217,7 @@ conform to the following pattern (0 otherwise):
|
|||
- After the apex, any remaining values decrease or remain the same
|
||||
#+end_quote
|
||||
|
||||
#+begin_src default
|
||||
risefall←{∧/(⍳∘≢≡⍋)¨(⊂((⊢⍳⌈/)↑⊢),⍵),⊂⌽((⊢⍳⌈/)↓⊢),⍵}
|
||||
#+end_src
|
||||
*Solution:* ~{∧/(⍳∘≢≡⍋)¨(⊂((⊢⍳⌈/)↑⊢),⍵),⊂⌽((⊢⍳⌈/)↓⊢),⍵}~
|
||||
|
||||
TODO
|
||||
|
||||
|
@ -250,9 +232,7 @@ displays identically to what ~{⎕←⍵}¨~ displays when applied to the
|
|||
right argument.
|
||||
#+end_quote
|
||||
|
||||
#+begin_src default
|
||||
stacking←{↑⊃,/↓¨⍕¨⍵}
|
||||
#+end_src
|
||||
*Solution:* ~{↑⊃,/↓¨⍕¨⍵}~
|
||||
|
||||
TODO
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue