~jshholland/trybqn

trybqn/2020.bqn -rw-r--r-- 3.4 KiB
4e81c000Josh Holland use new ⋈ builtin 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Write a function that, given a right argument Y which is a scalar or
# a non-empty vector and a left argument X which is a single non-zero
# integer so that its absolute value is less or equal to ≢Y, splits
# Y into a vector of two vectors according to X, as follows:
# If X>0, the first vector contains the first X elements of Y and the
# second vector contains the remaining elements.
# If X<0, the second vector contains the last |X elements of Y and the
# first vector contains the remaining elements.
Prob1 ⇐ ⊣1⊸⌽⍟(<⟜0)↑⋈↓

# Write a function that, given a right argument which is a simple integer
# vector representing valid UTF-8 text, encloses each sequence of integers
# that represent a single character.
# Continuation bytes run from 128 to 191 inclusive.
Prob2 ⇐ (¯1+`<⟜128∨>⟜191)⊸⊔

# Write a function that, given a right argument which is a character
# scalar or non-empty vector representing a valid character Excel column
# identifier between A and XFD, returns the corresponding column number.
Prob3 ⇐ +´-⟜'@'×26⋆·⌽∘↕≠

# Write a function that, given a right argument which is an integer
# array of year numbers greater than or equal to 1752 and less than
# 4000, returns a result of the same shape as the right argument where
# 1 indicates that the corresponding year is a leap year (0 otherwise).
Prob4 ⇐ (0=400⊸|)∨(0≠100⊸|)∧0=4⊸|

# Write a function that, given a right argument of 2 integers, returns
# a vector of the integers from the first element of the right argument
# to the second, inclusively.
Prob5 ⇐ {𝕊from‿to: from+(××·↕1+|)to-from}

# Write a function that, given a right argument which is an integer
# vector and a left argument which is an integer scalar, reorders the
# right argument so any elements equal to the left argument come first
# while all other elements keep their order.
Prob6 ⇐ {(⍒=⟜𝕨)⊸⊏𝕩}

# A common technique for encoding a set of on/off states is to use a
# value of 2⋆n for the state in position n (origin 0), 1 if the state
# is “on” or 0 for “off” and then add the values.
# Write a function that, given a non-negative right argument which is an
# integer scalar representing the encoded state and a left argument which
# is an integer scalar representing the encoded state settings that you
# want to query, returns 1 if all of the codes in the left argument are
# found in the right argument (0 otherwise).
Prob7 ⇐ {
  bits ← ⌈𝕨⌈○(2⋆⁼1⊸+)𝕩
  ∧´𝕨≤○{𝕊𝕩:⟨⟩𝕊𝕩;𝕩=0?bits↑𝕨;(𝕨∾2|𝕩)𝕊⌊𝕩÷2}𝕩
}

# A zigzag number is an integer in which the difference in magnitude of
# each pair of consecutive digits alternates from positive to negative
# or negative to positive.
# Write a function that takes a single integer greater than or equal to
# 100 and less than 10⋆15 as its right argument and returns a 1 if the
# integer is a zigzag number, 0 otherwise.
Prob8 ⇐ ∨´·≠⊸(⥊˘)⟜(¯1‿1≍1‿¯1)⊸(≡⎉1‿∞)·×·-˝˘2↕'0'-˜•Repr

# Write a function that, given a right argument which is an integer
# scalar or vector, returns a 1 if the values of the right argument
# conform to the following pattern (0 otherwise):
#  The elements increase or stay the same until the “apex” (highest
#  value) is reached
#  After the apex, any remaining values decrease or remain the same
Prob9 ⇐ ∧´⌈`⊸={𝔽∨𝔽⌾⌽}