~jshholland/trybqn

trybqn/2015.bqn -rw-r--r-- 4.6 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
79
80
81
82
83
84
85
# Write a function that takes two character vectors as its left and right
# arguments and returns 1 if they are anagrams of each other. An anagram
# of a string uses all of the letters of the string ignoring word spacing,
# capitalisation, and punctuation.
⟨lowercase⟩ ← •Import "util.bqn"
Prob1 ⇐ ≡○(∧ ∊⟜('a'+↕26)⊸/ ∘ Lowercase)

# Write a function that takes a numeric vector and returns the length
# of the longest streak of positive growth.
Prob2 ⇐ {0⌈⌈´≠¨(⊢-˜¬×+`)⊸⊔≥´˘2↕𝕩}

# In mathematics, the Farey sequence of order n is the sequence of
# completely reduced fractions between 0 and 1 which, when in lowest
# terms, have denominators less than or equal to n, arranged in order
# of increasing size. Each Farey sequence starts with the value 0,
# denoted by the fraction 0⁄1, and ends with the value 1, denoted by
# the fraction 1⁄1.
# Write a function that takes an integer right argument and returns a
# vector of the terms in the Farey sequence of that order. Each element
# in the returned vector is itself a 2-element vector of numerator and
# denominator for the corresponding term.
Prob3 ⇐ {(⍋÷´¨)⊸⊏⟨0‿1⟩∾∾{=⟜1∘{𝕨(|𝕊⍟(>⟜0)⊣)𝕩}´¨⊸/≍⟜𝕩¨1+↕𝕩}¨1+↕𝕩}

# Progressive dyadic iota is similar to ⍳ except that it returns
# the index of subsequent matches in the left argument until they are
# exhausted. Write a function that implements progressive dyadic iota.
Prob4 ⇐ ⊒ # lol

# Write a function that returns the population standard deviation of
# its numeric array right argument.
Prob5 ⇐ (√≠÷˜·+´·×˜¨⊢-+´÷≠)⥊
# BQNcrate gives this solution which is both shorter and makes the
# "sqrt of the mean squared error" definition clearer.
# Prob5 ⇐ (+´÷≠){𝔽⌾(ט)-⟜𝔽}⥊

# Write a function that takes, as each of its right and left arguments,
# a pair of co-ordinates representing diagonally opposite corners of two
# rectangles and returns a 1 if the rectangles overlap. The co-ordinates
# could be either (upper-left, lower-right) or (upper-right, lower left)
Prob6 ⇐ {w1‿w2 𝕊 x1‿x2:
  wll‿wur ← w1 (⌊¨⋈⌈¨) w2
  xll‿xur ← x1 (⌊¨⋈⌈¨) x2
  wll‿xll‿wur (∨○((⊢≡∧)⊑¨) ∧ ∨○((⊢≡∧)1⊸⊑¨)) xll‿wll‿xur
}

# Membership X∊Y returns a boolean array of shape ⍴X with 1s
# indicating where elements of Y occur in X. For a vector X this results
# in a very convenient boolean mask.
# In many instances, it is desirable to perform case-insensitive
# comparisons and operations. Write a function to perform case-insensitive
# membership between two arrays.
# For full marks, consider membership of cells in high-rank arrays,
# your function should still return a vector when comparing cells of
# the same rank, for example (3 2⍴⎕A) and (1 2⍴'cd').
Prob7 ⇐ ∊○Lowercase

# You’ve just been hired by a new company and you’ve learned that the
# owner has triskaidekaphobia (fear of the number 13). As such he’s
# issued a corporate mandate that no output from any program shall
# include the number 13 – 12.99 will be used instead. Your boss wants
# you to implement a function to process output and change the 13s to
# 12.99. For now, you need only concern yourself with numeric data and
# not worry about 13s that are formatted in text.
Prob8 ⇐ ⊢-·0.01⊸×13⊸=

# There is an urban myth about some research that was supposedly done at
# Cambridge University that purports changing the order of the interior
# letters in the words of a sentence does not significantly hamper the
# readability of the sentence.
# In the above example, for words that contain more than 3 letters,
# the first and last letters remain the same while all of the interior
# letters are transposed in groups of two, ignoring punctuation. If
# there are an odd number of letters in the interior of the word, then
# the last letter is left as it is.
# Write a function that takes a character vector word as its left argument
# and returns the word’s letters juxtaposed as described above.
Prob9 ⇐ (↕+⊢↑0∾·⥊⟜1‿¯1·⌊⌾(÷⟜2)0⌈-⟜2)∘≠⊸⊏

# Write a function that takes an integer right argument representing
# the order of the Pascal’s triangle to be created and returns a
# “nicely” formatted character matrix. Because we’re using a
# mono-spaced font and can only format whole character positions, lines do
# not have to be perfectly centered and may vary by a character position.
centre‿choose←•Import "util.bqn"
Prob10 ⇐ > · (≠∘(¯1⊸⊑)Centre¨⊢) · 1⊸↓∘∾¨ · ' '⊸∾∘•Repr∘Choose¨⟜(↕1⊸+)¨ ↕