## ~jshholland/trybqn

trybqn/2019.bqn -rw-r--r-- 3.1 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```
```# Write a function that, given a scalar or vector as the right argument
# and a positive (>0) integer chunk size n as the left argument, breaks
# the array’s items up into chunks of size n. If the number of elements
# in the array is not evenly divisible by n, then the last chunk will
# have fewer than n elements.
Prob1 ⇐ (/|∾˜⌊∘÷˜⥊⊣)⟜≠⊔⊢

# Write a function that, given an array of integer test scores in the
# inclusive range 0–100, returns an identically-shaped array of the
# corresponding letter grades according to the table to the left.
Prob2 ⇐ ⊏⟜"FDCBA" 65‿70‿80‿90⊸⍋

# The school’s administrative department wants to publish some simple
# statistics. Given a non-empty character vector of single-letter grades,
# produce a 3-column, 5-row, alphabetically-sorted matrix of each grade,
# the number of occurrences of that grade, and the percentage (rounded
# to 1 decimal position) of the total number of occurrences of that
# grade. The table should have a row for each grade even if there are
# no occurrences of a grade.
Round ← (⌊0.5⊸+)⌾(10⊸×)
Prob3 ⇐ (⊢∾˘(Round 100×⊢÷+´)∘(1⊸⊏˘))·("ABCDF"≍˘≠¨) "ABCDF"⊸(⊐∾≠∘⊣)⊸⊔

# Given a 2-element vector representing the current square for a knight,
# return a vector of 2-element vectors representing (in any order)
# all the squares that the knight can move to.
Prob4 ⇐ {(∧´≥⟜0∧<⟜8)¨⊸/ (<𝕩)+⥊(≍○<⟜⌽1‿2)×⌜≍⌜˜¯1‿1}

# Given a word or a list of words, return a Boolean vector where
# 1 indicates a word with one or more consecutive duplicated,
# case-sensitive, letters.
Prob5 ⇐ (≢⟜" "∧·∨´=⟜»)⚇1

# Write an APL function that takes a character vector right argument
# that consists of digits and uppercase letters and returns an integer
# vector of the corresponding digits on the keypad.
Prob6 ⇐ (∊⟜('0'+↕10)×-⟜'0')+∊⟜('A'+↕26)×2+"DGJMPTW"⊸⍋

# Given a right argument of a list of words (or possibly a single word)
# and a left argument of a width, return a character matrix that has width
# columns and one row per word, where each word is centered within the
# row. If width is smaller than the length of a word, truncate the word
# from the right. If there are an odd number of spaces to center within,
# leave the extra space on the right.
⟨centre⟩←•Import "util.bqn"
Prob7 ⇐ {∘‿𝕨⥊𝕨(>Centre¨)𝕩}

# Given a vector of (X Y) points, or a single X Y point, determine the
# total distance covered when travelling in a straight line from the
# first point to the next one, and so on until the last point, then
# returning directly back to the start.
Prob8 ⇐ +´·+´⌾(×˜)¨⊢-1⌽⊢

# Given a vector of (X Y) points, or a single X Y point, return a number
# indicating the area circumscribed by the points [using Gauss's shoelace
# formula].
Prob9 ⇐ (2÷˜·|·-´·+˝⊢×·⌽˘1⊸⌽)>

# Given a vector of words, separate the words into two vectors – one
# containing all the words that have an odd number of letters and the
# other containing all the words that have an even number of letters.
Prob10 ⇐ (2∾˜·¬2|≠¨)⊸⊔
```