## ~jshholland/trybqn

trybqn/2021.bqn -rw-r--r-- 3.9 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
67
68
69
70
71
72
73
74
75
76
77
78```
```# A DNA string is composed of the letters 'A','C','G' and 'T'. The
# GC-content of a DNA string is given by the percentage of the symbols
# in the string that are either 'C' or 'G'. Very small discrepancies
# in GC-content can be used to distinguish species; for instance, most
# bacteria have a GC-content significantly higher than 50%.
# Write a function that:
#  has a right argument that is a non-empty character vector representing a DNA string.
#  returns the percentage of GC-content in the string.
Prob1 ⇐ 100×+´∘∊⟜"GC"÷≠

# Write a function that behaves like the APL index-of function R←X⍳Y
# except that it returns 0 instead of 1+≢X for elements of Y not found
# in X.
Prob2 ⇐ ⊐-¬∘∊˜×1+≠∘⊣

# Write a function that finds which elements of Y are multiples of each
# element of X and returns them as a vector (in the order of X) of vectors
# (in the order of Y).
Prob3 ⇐ (<˘0=|⌜)/¨<∘⊢

# Write a function that takes a right argument which is an array
# of positive numbers representing circle diameters and returns a
# numeric array of the same shape as the right argument representing
# the difference between the areas of the circles and the areas of the
# largest squares that can be inscribed within each circle.
Prob4 ⇐ (4÷˜π××˜)-·÷⟜2×˜

# Suppose you have a number of trees that you want to plant in a
# rectangular pattern with complete rows and columns, meaning all rows
# have the same number of trees. You also want that rectangular pattern
# to be as "square" as possible, meaning there is a minimal difference
# between the number of rows and columns in the pattern
Prob5 ⇐ { ≍⟜(𝕩⊸÷) ⊑ (0=|⟜𝕩)⊸/ ⌽1+↕⌊√𝕩 }

# Write a function that:
# has a character vector right argument that is a permutation of 'KQRRBBNN'
# returns 1 if the following are true:
#  the K is between the two Rs
#  the Bs occupy one odd and one even position
# otherwise a 0 is returned.
Prob6 ⇐ (⊑1=/∘=⟜'R'⍋⊐⟜'K')∧·≠´2|/∘=⟜'B'

# Wikipedia states that, in recreational mathematics, a square array of
# numbers, usually positive integers, is called a magic square if the
# sums of the numbers in each row, each column, and both main diagonals
# are the same.
# Write a function to test whether an array is a magic square.
Prob7 ⇐ (⊒≡⊒˜)+˝∾+˝˘∾·(+´0‿0⊸⍉)¨⌽⍟(↕2)

# Write a function that:
#  has a right argument that is a numeric scalar or vector of length up
#   to 3, representing a number of [[[days] hours] minutes] – a single
#   number represents minutes, a 2-element vector represents hours and
#   minutes, and a 3-element vector represents days, hours, and minutes.
#  has a similar left argument, although not necessarily the same length
#   as the right argument.
#  returns a single number representing the magnitude of the difference
#   in minutes between the arguments.
Prob8 ⇐ |-○(1440‿60‿1⊸(+´∘×)¯3⊸↑)

# Write a function that has a right argument that is a numeric vector of
# 2 or more elements representing daily prices of a stock and returns an
# integer singleton that represents the highest number of consecutive
# days where the price increased, decreased, or remained the same,
# relative to the previous day.
Prob9 ⇐ {⌈´∾<‿=‿>{≠¨(⊢-˜¬×+`)⊸⊔¬𝕎´˘𝕩}¨<2↕𝕩}

# Write a function that has a right argument T that is a character scalar,
# vector or a vector of character vectors/scalars, a left argument W that
# is a positive integer specifying the width of the result.  and returns
# a right-aligned character array R< of shape ((2=|≡T)/≢T),W meaning
# R is one of the following:
#  a W-wide vector if T is a simple vector or scalar.
#  a W-wide matrix with the same number rows as elements of T if T is
#  a vector of vectors/scalars.
# If an element of T has length greater than W, truncate it after
# W characters.
Prob10 ⇐ (2≤≡∘⊢)◶⟨-⊸↑⋄>-⊸↑¨⟩
```