aboutsummaryrefslogtreecommitdiff
path: root/examples/fifty.bqn
blob: c235b24c10319465502f505013f3e1123af464f0 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# A history of APL in 50 functions
# But they're BQN functions
# Still about APL history though
# Also not all the functions are here yet
# https://www.jsoftware.com/papers/50/

# Utilities
Words  (⊢-˜¬×+`)=' '  # Split string on spaces

# 0
ArrayLogic  (>-<)0
! 1¯101  ArrayLogic 5¯2.706

# 1
Average  +´÷≠
! 3  Average 216

# 2
# x⌹x=x is an alternate way to get the average of vector x.
# There's no reason to do it this way in BQN (or APL, really).

# 3
IndexOfSelfie  ˜
! 012341  IndexOfSelfie "Selfie"

# 4
BarChart  ".⎕"˜>(↕⌈´)
! {
  bc  BarChart 314159
  bc  69"⎕⎕⎕......⎕........⎕⎕⎕⎕.....⎕........⎕⎕⎕⎕⎕....⎕⎕⎕⎕⎕⎕⎕⎕⎕"
}

# 5
ParenthesesNesting  +`1¯10˜"()"⊐⊢
! {
  pn  ParenthesesNesting "⍵((∇<S),=S,(∇>S))⍵⌷⍨?≢⍵"
  pn  01222211111222210000000
}

# 6
PerfectShuffle  ⊢⊏˜·⍒≠⥊01˜
! "IAJBKCLDMENFOGPHQ"  PerfectShuffle "ABCDEFGHIJKLMNOPQ"

# 7
_quicksort  {Cmp𝔽  S{p𝕩Cmp˜𝕩˜•Rand≠𝕩  101𝕩{S𝕨𝕗/˜0𝕏p}¨<=>}(1<≠)}
! 1223579101010111416  -_quicksort 2271010113101459116
! {
  Cmp  0(-´⍋∾<)¨
  srt  Words "Anna Fi JD Jay Jd John Morten Roger Scott Zeus"
  srt  Cmp _quicksort Words "Fi Jay John Morten Roger JD Jd Anna Scott Zeus"
}

# 8
PascalsTriangle  {>𝕩¨0(∾+∾˜)(𝕩)1}
! (>1000110012101331)  PascalsTriangle 4
! 11235813213455  { 𝕩  +´¨ (+⌜˜𝕩)  PascalsTriangle 𝕩 } 10

# 9
GoldenRatio  +÷´¨(1↓↑) 1
! 1e¯5 ´> | (GoldenRatio 16) - 121.51.666671.61.6251.615381.619051.617651.618181.617981.618061.618031.618041.618031.61803

# 10
NewtonsMethod  (2 ÷˜  + ÷)´¨(1↓↑) 
! 1e¯5 ´> | (7 NewtonsMethod 2) - 21.51.416671.414221.414211.414211.41421

# 11
InnerProduct  +˝×12
! (>2341)  (>10¯11) InnerProduct >2364

# 12
CayleysTheorem  {R⥊⊐⊢  (R𝕩)  R ˜⌜˜ <˘R𝕩}
! {
  t  22¨ (2|⌊÷2(4))¨ 967111314
  g  ˝12⌜˜t
  CayleysTheorem g
}

# 14
IntervalIndex  1-˜(⊣↓⊢⍋⊏+`>)  # Or ⍋, of course
! 02324102¯1  ¯12378.5 IntervalIndex 047611213¯5
! 010¯144  "Fi Jay John Morten Roger" IntervalIndexWords "JD Jd Geoff Anna Scott Zeus"

# 15
CentralLimit  { 41  / (5×↕40)  +˝ •Rand 𝕩21 }
# 5‿8 ⥊ CentralLimit 10 1e3

# 17
SeventeenTwentyNine  {F:
  c  3˜1+↕200
  t  (<⌜˜200) × +⌜˜c
  d   t
  ´ ¨ (2=≠¨)/ d
}
# ! 1729 ≡ SeventeenTwentyNine⟨⟩  # slow

# 19
Permutations  {𝕊0:100; ∾<˘(0˘1+𝕊𝕩-1)˘˘=⌜˜𝕩}
! (63012021102120201210)  Permutations 3

# 20
InversePermutation    # Or ⊐⟜(↕≠) or ∾∘⊔
! (10)  InversePermutation 1452683709

# 21
IndexFromPermutationIfp,PermutationFromIndexPfi  {
  Rfd  ⊢+´>¨¯1↓↓
  Dfr  (⍋∾)´
  Ifp  {𝕊:𝕨Pfi𝕩; (⌽×`1+»↕≠𝕩) +´× Rfd𝕩}
  Pfi  {𝕊:𝕨Ifp𝕩; w𝕊𝕩: Dfr (1+↕w) (⌽⊣|¯1·÷`˜) 𝕩}
}
! {
  p  1307654982
  ! p  10 PermutationFromIndex i  IndexFromPermutation p
  ! p  (10IndexFromPermutation) p
  ! i  (10PermutationFromIndex) i
}

# 22
Combinations  {𝕨(=∨0=⊣)(0˘𝕊(-1))1+𝕊(-1), ↕⊣𝕩}
! (3 Combinations 5)  103012013014023024034123124134234

# 23
IndexFromCombinationIc,CombinationFromIndexCi  {
  C  ((-˜+↕)÷(×´1+))˘  # Combination function (APL's dyadic !)
  Ic  {
    𝕊: 𝕨Ci𝕩;
    mn𝕊𝕩:
    ⊑-˝(m-↕m) +˝(C˘) n-(»1+𝕩)˘𝕩
  }
  Ci  {
    𝕊: 𝕨Ic𝕩;
    0n𝕊𝕩: ⟨⟩;
    mn𝕊𝕩:
    v+`(m-1)C(1-m)↓⌽↕n
    k(v>𝕩)1
    k(1+k)+(𝕨-11+k)𝕊(𝕩-k0v)
  }
}
! 11  46 IndexFromCombination 1235
! 1235  46 CombinationFromIndex 11
! 11  46 CombinationFromIndex 46 CombinationFromIndex 11

# 24
SymmetricArray    1
! SymmetricArray +⌜´ 3⥊<23711

# 25
NQueens  {
  Arr  {>(𝕩1)¨(𝕨)1𝕩+12(-¯1⊑≢𝕩)ׯ101}
  𝕩 Arr(𝕩-1) ˘𝕩
}
QueenCheck  {
  ! 1=≠≢𝕩
  ! (↕≠𝕩)≡∧𝕩
  ! ´(=⌜˜↕≠𝕩)(|-⌜˜𝕩)=|-⌜˜↕≠𝕩
}
QueenCheck˘ NQueens 8

# 26
KnightsTour  {
  Kmoves  {
    t  (⥊↕𝕩𝕩)+<˘82212¯1121¯2¯12¯1¯2¯21¯2¯1
    (˝1(>t)∊↕𝕩) /¨<˘ 𝕩{+(𝕨×)´𝕩}¨t
  }
  m  >↑˜¨(´¨) Kmoves 𝕩
  b  (𝕩×𝕩)1
  F  {b0(𝕩)b  (⊑⍋+˝˘(jm)b)jb/𝕩m}
  𝕩𝕩⥊⍋F(𝕩2) 0
}
! (KnightsTour 6)  6609203561121327101926813425125332231162718215242941323303141728

# 34
# Fails when 𝕩 is 1; the APL version did this too!
_pow  {𝔽´𝔽˜(/2|⌊÷2(·2)𝕩)𝕨}
! 847288609443  3 ×_pow 25

# 38
Hanoi  {¯1(⥊⊢≍0˜≠⥊⊑⊑152034˜)𝕩1}
! 0130450  Hanoi 3