~jshholland/trybqn

trybqn/2018.bqn -rw-r--r-- 3.5 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Write an APL expression that, given a scalar or vector of skyscraper
# heights from closest to furthest, will return an integer representing
# the number of skyscrapers that can be seen.
Prob1 ⇐ +´∘∊⌈`

# Write an APL expression that, given a scalar real non-negative number,
# will return a two-element vector of the integer and fractional parts
# of the number.
Prob2 ⇐ ⌊≍1⊸|

# Using the key operator ⌸, write an APL expression that, given an
# integer scalar or vector representing the number of sides on each of
# a set of dice, will return a histogram showing the distribution curve
# for the possible totals that can be rolled using those dice.
Prob3 ⇐  ⥊⟜'*'⌾(1⊸⊑)˘(•Import "2014.bqn").Prob6


# The Chinese animal zodiac is a repeating cycle of 12 years, with each
# year being represented by an animal. 2018 is the year of the dog.
# Note that the year 1 AD (represented as 1) follows the year 1 BC
# (represented as ¯1) with no intervening “0” year.
# Write an APL expression that, given a scalar integer year, returns a
# character vector (string) of the Chinese zodiac sign for that year. For
# the purposes of this problem, assume that each year number corresponds
# to exactly one Chinese zodiac animal.
zodiac ← ⟨
  "Monkey"
  "Rooster"
  "Dog"
  "Pig"
  "Rat"
  "Ox"
  "Tiger"
  "Rabbit"
  "Dragon"
  "Snake"
  "Horse"
  "Goat"
⟩
Prob4 ⇐ ⊑⟜zodiac·12⊸|1⊸+⍟(<⟜0)

signs ← ⟨
  "Capricorn"
  "Aquarius"
  "Pisces"
  "Aries"
  "Taurus"
  "Gemini"
  "Cancer"
  "Leo"
  "Virgo"
  "Libra"
  "Scorpio"
  "Sagittarius"
  "Capricorn"
⟩
CodeMonth ← {𝕊m‿d: d+32×m}
bins ← CodeMonth¨ ⟨
  1‿20
  2‿19
  3‿21
  4‿20
  5‿21
  6‿21
  7‿23
  8‿23
  9‿23
  10‿23
  11‿22
  12‿22
⟩
Prob5 ⇐ ⊑⟜signs·⊑bins⊸⍋∘CodeMonth

# One easy validation is to check that the angle brackets are properly
# balanced – all left angle brackets < must be “closed” with right
# angle brackets > before another occurrence of a left angle bracket.
# Write an APL expression that, given a character scalar or vector
# representing some XML, returns 1 if the angle brackets are properly
# balanced and 0 if not.
Prob6 ⇐ (¯1⊸⊑⎊0⊸=⟜0 ∧ ·∧´≥⟜0∧≤⟜1)·+`'<'⊸=-'>'⊸=

# Write an APL expression that given a right argument of a Boolean
# scalar or vector, and left argument scalar integer of the shift amount,
# returns an appropriately shifted transformation of the right argument.
Prob7 ⇐ {0(𝕨<0)◶»‿«⍟(|𝕨)𝕩}

# Write an APL function that given a right argument Y of any array and a
# numeric scalar or vector left argument X returns a Boolean indicating
# if the left argument is a valid argument for X⍉Y, like the result of
# {0::0 ⋄ 1⊣⍺⍉⍵} but does not use ⍉ (to test the arguments).
Prob8 ⇐ { rr ← (=𝕩)-+´¬∊⥊𝕨 ⋄ 𝕨(=⊸≤⟜1 ∧ ≤○≠⟜≢ ∧ ·∧´⥊⊸<⟜rr)𝕩 }

# Write an APL expression that given left and right arguments of such
# timestamps returns a ¯1, 1, or 0 if the left argument represents a
# time that is respectively, earlier than, later than, or simultaneous
# with the right argument.
# (it's lex ordering)
Prob9 ⇐ ⊑⎊0 · |⊸/ ×∘-

# Write an APL expression that takes left and right arguments of character
# scalars or vectors returns a 1 if the arguments are anagrams of one
# another, 0 otherwise. You may assume that both arguments are both
# either upper-case or lower-case.
Prob10 ⇐ (•Import "2015.bqn").Prob1