{"id":7043,"date":"2022-03-25T10:11:19","date_gmt":"2022-03-25T10:11:19","guid":{"rendered":"https:\/\/www.dyalog.com\/blog\/?p=7043"},"modified":"2022-03-25T14:30:20","modified_gmt":"2022-03-25T14:30:20","slug":"the-2021-apl-problem-solving-competition-phase-i-best-of-breed","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2022\/03\/the-2021-apl-problem-solving-competition-phase-i-best-of-breed\/","title":{"rendered":"The 2021 APL Problem Solving Competition: Phase I \u2013 Best of Breed"},"content":{"rendered":"<p>By: <a href=\"https:\/\/apl.wiki\/User:Xpqz\" target=\"_blank\" rel=\"noopener\">Stefan Kruger<\/a><br \/>\n<em>Stefan works for IBM making databases. He tries to learn at least one new programming language a year, and a few years ago he got hooked on APL and participated in the competition. This is his perspective on some solutions that the judges picked out \u2013 call it the &#8220;Judges&#8217; Pick&#8221;, if you like; smart, novel, or otherwise noteworthy solutions that can serve as an inspiration.<\/em><\/p>\n<hr \/>\n<p>Congratulations to all the <a href=\"https:\/\/www.dyalog.com\/news\/146\/420\/2021-APL-Problem-Solving-Competition-Winners.htm\" target=\"_blank\" rel=\"noopener\">winners<\/a> of the 2021 APL Problem Solving Competition (you can learn more about the phase 2 winners <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/www.dyalog.com\/news\/147\/420\/Meet-The-2021-APL-Problem-Solving-Competition-Winners.htm\">in this article<\/a>) and well done to Dzintars Klu\u0161s who won the Grand Prize. At the recent <a href=\"https:\/\/www.dyalog.com\/user-meetings\/dyalog21.htm\" target=\"_blank\" rel=\"noopener\">Dyalog &#8217;21<\/a> user meeting, we got to enjoy the runner-up, Victor Ogunlokun, walking us through his solutions live.<\/p>\n<p>In this post I&#8217;ll go through some great solutions that were submitted (and some that weren&#8217;t submitted) to the Phase I problems so that we can all marvel in the ingenuity and perhaps learn a thing or two. If you&#8217;re feeling inspired by the end, go ahead and participate in <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/contest.dyalog.com\">this year&#8217;s round<\/a> which just launched.<\/p>\n<p>If you&#8217;re new to the <a href=\"https:\/\/www.dyalog.com\/student-competition.htm\" target=\"_blank\" rel=\"noopener\">APL Problem Solving Competition<\/a>, Phase I problems tend to be short and the expectation is that solutions will be &#8220;one-liners&#8221; (dfns). However, although it might seem like it from some of the solutions here, this isn&#8217;t a <a href=\"https:\/\/apl.wiki\/code_golf\" target=\"_blank\" rel=\"noopener\">code golf<\/a> competition! Solutions are judged holistically: do they solve the problem, are they efficient, and are they clear? Even though a few test cases are given, there is no guarantee that your solution is correct just because it works for the example data. The judging process involves running the code on many hidden test cases too. Crucially, just because your code is accepted, it doesn&#8217;t necessarily mean that you&#8217;ll get full marks.<\/p>\n<p>As with my blog post that <a href=\"https:\/\/www.dyalog.com\/blog\/2021\/04\/2020-problem-solving-competition-phase-ii-highlights\/\">reviewed the 2020 Phase II solutions<\/a>, I&#8217;ve included a more in-depth examination of one or two problems.<\/p>\n<h2 id=\"problem-1-are-you-a-bacteria-\">Problem 1: Are You a Bacteria?<\/h2>\n<p>Something from the excellent <a href=\"http:\/\/rosalind.info\/problems\/locations\/\" target=\"_blank\" rel=\"noopener\">Project Rosalind<\/a> problem collection, the task is to compute the combined percentage of guanine (G) and cytosine (C) in a given DNA-string.<\/p>\n<p>Efficiency can vary a lot, depending on whether summation or multiplication (or even division!) is performed first. Some solutions were also <a href=\"https:\/\/apl.wiki\/Leading_axis_theory\" target=\"_blank\" rel=\"noopener\">leading-axis<\/a> oriented.<\/p>\n<p>Here&#8217;s my solution:<\/p>\n<pre><code class=\"lang-apl\">      {100\u00d7(+\u233f\u2375\u220a'CG')\u00f7\u2262\u2375} 'ACGTACGTACGTACGT'\r\n50<\/code><\/pre>\n<p>which several competitors made more tacit with:<\/p>\n<pre><code class=\"lang-apl\">      {100\u00d7(+\u233f\u00f7\u2262)\u2375\u220a'GC'} 'ACGTACGTACGTACGT'\r\n50<\/code><\/pre>\n<p>or even went further:<\/p>\n<pre><code class=\"lang-apl\">      (100\u00d7\u2262\u00f7\u23681\u22a5\u220a\u2218'GC') 'ACGTACGTACGTACGT'\r\n50<\/code><\/pre>\n<p>If you&#8217;re unfamiliar with the <code class=\"lang-apl\">1\u22a5<\/code> trick, it&#8217;s a way of summing a vector:<\/p>\n<pre><code class=\"lang-apl\">      1\u22a56 3 9 8 12 62\r\n100<\/code><\/pre>\n<p>It&#8217;s perhaps not immediately obvious why this should work. Here&#8217;s one explanation. Assume we want to sum the vector <code class=\"lang-apl\">1 0 2 0 0<\/code>. We can do this in a very convoluted way by using a sum inner product with a vector of exponentials: [1<sup>4<\/sup>, 1<sup>3<\/sup>, 1<sup>2<\/sup>, 1<sup>1<\/sup>, 1<sup>0<\/sup>]:<\/p>\n<pre><code class=\"lang-apl\">      (1*4 3 2 1 0)+.\u00d71 0 2 0 0\r\n3<\/code><\/pre>\n<p>If we expand the exponentials to the left we get a vector of <code class=\"lang-apl\">1<\/code>s. We can then break apart the inner product by turning <code class=\"lang-apl\">+.<\/code> to a <code class=\"lang-apl\">+\u233f<\/code> to the left:<\/p>\n<pre><code class=\"lang-apl\">      +\u233f1 1 1 1 1\u00d71 0 2 0 0\r\n3<\/code><\/pre>\n<p>This is the textbook definition of <code class=\"lang-apl\">1\u22a5<\/code>! Look:<\/p>\n<pre><code class=\"lang-apl\">      1\u22a51 0 2 0 0\r\n3<\/code><\/pre>\n<p>which, to be clear, is just the sum-reduce-first:<\/p>\n<pre><code class=\"lang-apl\">      +\u233f1 0 2 0 0\r\n3<\/code><\/pre>\n<p>Using <code class=\"lang-apl\">1\u22a5<\/code> to sum has two advantages over the more obvious formulation <code class=\"lang-apl\">+\u233f<\/code>. Firstly, it&#8217;s easier to use in tacit formulations as it doesn&#8217;t require an operator, and secondly, it&#8217;s usually faster. The reasons for it being quicker is somewhat beyond the scope of this post, but it&#8217;s to do with <code class=\"lang-apl\">1\u22a5<\/code> making no guarantees about the ordering of operations, meaning that the interpreter is free to vectorise more efficiently.<\/p>\n<h2 id=\"problem-2-index-of-modified\">Problem 2: Index-Of Modified<\/h2>\n<p>This problem wanted us to write a function that behaves like the APL <em>Index Of<\/em> function <code class=\"lang-apl\">R\u2190X\u2373Y<\/code> except that it should return <code class=\"lang-apl\">0<\/code> for elements of <code class=\"lang-apl\">Y<\/code> not found in <code class=\"lang-apl\">X<\/code>.<\/p>\n<p>I wrote:<\/p>\n<pre><code class=\"lang-apl\">      p2 \u2190 {0@((\u2262\u237a)\u2218&lt;)\u22a2\u237a\u2373\u2375}\r\n      2 3 p2 \u23735\r\n0 1 2 0 0\r\n<\/code><\/pre>\n<p>which is basically saying &#8220;change all instances of numbers greater than the length of the argument to zero&#8221;, which is how <code class=\"lang-apl\">X\u2373Y<\/code> presents values that are not found.<\/p>\n<p>Some very different solutions were submitted, for example:<\/p>\n<pre><code class=\"lang-apl\">      p2 \u2190 \u2373|\u23681+\u2262\u2364\u22a3\r\n      2 3 p2 \u23735\r\n0 1 2 0 0\r\n<\/code><\/pre>\n<p>which is simply:<\/p>\n<pre><code class=\"lang-apl\">      p2 \u2190 {(1+\u2262\u237a)|\u237a\u2373\u2375} \u235d dfn of the above\r\n      2 3 p2 \u23735\r\n0 1 2 0 0\r\n<\/code><\/pre>\n<p>Another option would have been to multiply <code class=\"lang-apl\">\u237a\u2373\u2375<\/code> with <code class=\"lang-apl\">\u2262\u237a<\/code>, although no-one submitted exactly this:<\/p>\n<pre><code class=\"lang-apl\">      p2 \u2190 \u2262\u2364\u22a3(\u2265\u00d7\u22a2)\u2373\r\n      2 3 p2 \u23735\r\n0 1 2 0 0\r\n<\/code><\/pre>\n<p>which could have been written explicitly as:<\/p>\n<pre><code class=\"lang-apl\">      p2 \u2190 {m\u00d7(\u2262\u237a)\u2265m\u2190\u237a\u2373\u2375} \u235d dfn of the above\r\n      2 3 p2 \u23735\r\n0 1 2 0 0\r\n<\/code><\/pre>\n<h2 id=\"problem-3-multiplicity\">Problem 3: Multiplicity<\/h2>\n<p>Write a function that:<\/p>\n<ul>\n<li>has a right argument <code class=\"lang-apl\">Y<\/code> which is an integer vector or scalar<\/li>\n<li>has a left argument <code class=\"lang-apl\">X<\/code> which is also an integer vector or scalar<\/li>\n<li>finds which elements of <code class=\"lang-apl\">Y<\/code> are multiples of each element of <code class=\"lang-apl\">X<\/code> and returns them as a vector (in the order of <code class=\"lang-apl\">X<\/code>) of vectors (in the order of <code class=\"lang-apl\">Y<\/code>).<\/li>\n<\/ul>\n<p>Some test data was provided:<\/p>\n<pre><code class=\"lang-apl\">      X \u2190 2 4 7 3 9\r\n      Y \u2190 5 7 8 1 12 10 20 16 11 4 2 15 3 18 14 19 13 9 17 6\r\n<\/code><\/pre>\n<p>I wrote something that, in retrospect, looks somewhat clumsy:<\/p>\n<pre><code class=\"lang-apl\">      p3 \u2190 {\u2375\u2218{\u2375\/\u237a}\u00a8\u21930=\u237a\u2218.|,\u2375}\r\n      X p3 Y\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2510\r\n\u25028 12 10 20 16 4 2 18 14 6\u25028 12 20 16 4\u25027 14\u250212 15 3 18 9 6\u250218 9\u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<p>which can be expressed more compactly as:<\/p>\n<pre><code class=\"lang-apl\">      p3 \u2190 {\/\u2218\u2375\u00a8\u21930=\u237a\u2218.|\u2365,\u2375}\r\n      X p3 Y\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2510\r\n\u25028 12 10 20 16 4 2 18 14 6\u25028 12 20 16 4\u25027 14\u250212 15 3 18 9 6\u250218 9\u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<p>or:<\/p>\n<pre><code class=\"lang-apl\">      X (\u22a2\u2282\u2364\/\u23641\u23680=\u2218.|\u2365,) Y\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2510\r\n\u25028 12 10 20 16 4 2 18 14 6\u25028 12 20 16 4\u25027 14\u250212 15 3 18 9 6\u250218 9\u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<p>although no-one actually submitted that, <em>to everyone&#8217;s credit<\/em>.<\/p>\n<h2 id=\"problem-4-square-peg-round-hole\">Problem 4: Square Peg, Round Hole<\/h2>\n<p>Write a function that:<\/p>\n<ul>\n<li>takes a right argument which is an array of positive numbers representing circle diameters<\/li>\n<li>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.<\/li>\n<\/ul>\n<p>I had to read that many times before it sank in. The key to achieve something snappy is to really work through the maths until it is as compact as possible, which, if you&#8217;re anything like me, you didn&#8217;t bother to do.<\/p>\n<p>My attempt was:<\/p>\n<pre><code class=\"lang-apl\">      p4 \u2190 {(\u25cb2*\u2368\u2375\u00f72)-2\u00f7\u2368\u2375*2}\r\n<\/code><\/pre>\n<p>but there are much neater solutions if you did your homework. Here&#8217;s one that no-one found:<\/p>\n<pre><code class=\"lang-apl\">      p4 \u2190 (\u25cb-+\u2368)4\u00f7\u2368\u00d7\u2368\r\n<\/code><\/pre>\n<p>and a nice explicit version:<\/p>\n<pre><code class=\"lang-apl\">      p4 \u2190 {\u2375\u00d7\u2375\u00d70.5-\u2368\u25cb\u00f74}\r\n<\/code><\/pre>\n<p>which can be derived from this simplified mathematical expression, suggested by <a href=\"https:\/\/www.dyalog.com\/meet-team-dyalog.htm#Rodrigo\" target=\"_blank\" rel=\"noopener\">Rodrigo<\/a>:<br \/>\n<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?><br \/>\n<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"80.256px\" height=\"39.744px\" viewBox=\"0 -1509.9 4434 2195.9\" xmlns:xlink=\"http:\/\/www.w3.org\/1999\/xlink\"><defs><path id=\"MJX-4-TEX-N-28\" d=\"M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z\"><\/path><path id=\"MJX-4-TEX-I-3C0\" d=\"M132 -11Q98 -11 98 22V33L111 61Q186 219 220 334L228 358H196Q158 358 142 355T103 336Q92 329 81 318T62 297T53 285Q51 284 38 284Q19 284 19 294Q19 300 38 329T93 391T164 429Q171 431 389 431Q549 431 553 430Q573 423 573 402Q573 371 541 360Q535 358 472 358H408L405 341Q393 269 393 222Q393 170 402 129T421 65T431 37Q431 20 417 5T381 -10Q370 -10 363 -7T347 17T331 77Q330 86 330 121Q330 170 339 226T357 318T367 358H269L268 354Q268 351 249 275T206 114T175 17Q164 -11 132 -11Z\"><\/path><path id=\"MJX-4-TEX-N-2212\" d=\"M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z\"><\/path><path id=\"MJX-4-TEX-N-32\" d=\"M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z\"><\/path><path id=\"MJX-4-TEX-N-29\" d=\"M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z\"><\/path><path id=\"MJX-4-TEX-I-64\" d=\"M366 683Q367 683 438 688T511 694Q523 694 523 686Q523 679 450 384T375 83T374 68Q374 26 402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487H491Q506 153 506 145Q506 140 503 129Q490 79 473 48T445 8T417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157Q33 205 53 255T101 341Q148 398 195 420T280 442Q336 442 364 400Q369 394 369 396Q370 400 396 505T424 616Q424 629 417 632T378 637H357Q351 643 351 645T353 664Q358 683 366 683ZM352 326Q329 405 277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q233 26 290 98L298 109L352 326Z\"><\/path><path id=\"MJX-4-TEX-N-34\" d=\"M462 0Q444 3 333 3Q217 3 199 0H190V46H221Q241 46 248 46T265 48T279 53T286 61Q287 63 287 115V165H28V211L179 442Q332 674 334 675Q336 677 355 677H373L379 671V211H471V165H379V114Q379 73 379 66T385 54Q393 47 442 46H471V0H462ZM293 211V545L74 212L183 211H293Z\"><\/path><\/defs><g stroke=\"currentColor\" fill=\"currentColor\" stroke-width=\"0\" transform=\"matrix(1 0 0 -1 0 0)\"><g data-mml-node=\"math\"><g data-mml-node=\"mo\"><use xlink:href=\"#MJX-4-TEX-N-28\"><\/use><\/g><g data-mml-node=\"mi\" transform=\"translate(389, 0)\"><use xlink:href=\"#MJX-4-TEX-I-3C0\"><\/use><\/g><g data-mml-node=\"mo\" transform=\"translate(1181.2, 0)\"><use xlink:href=\"#MJX-4-TEX-N-2212\"><\/use><\/g><g data-mml-node=\"mn\" transform=\"translate(2181.4, 0)\"><use xlink:href=\"#MJX-4-TEX-N-32\"><\/use><\/g><g data-mml-node=\"mo\" transform=\"translate(2681.4, 0)\"><use xlink:href=\"#MJX-4-TEX-N-29\"><\/use><\/g><g data-mml-node=\"mfrac\" transform=\"translate(3070.4, 0)\"><g data-mml-node=\"msup\" transform=\"translate(220, 676)\"><g data-mml-node=\"mi\"><use xlink:href=\"#MJX-4-TEX-I-64\"><\/use><\/g><g data-mml-node=\"mn\" transform=\"translate(520, 363) scale(0.707)\"><use xlink:href=\"#MJX-4-TEX-N-32\"><\/use><\/g><\/g><g data-mml-node=\"mn\" transform=\"translate(431.8, -686)\"><use xlink:href=\"#MJX-4-TEX-N-34\"><\/use><\/g><rect width=\"1123.6\" height=\"60\" x=\"120\" y=\"220\"><\/rect><\/g><\/g><\/g><\/svg><br \/>\nExplanation: The area of the circle is <code class=\"lang-apl\">\u25cbr*2<\/code>, which is <code class=\"lang-apl\">\u25cb(\u2375\u00f72)*2<\/code>, in turn equivalent to <code class=\"lang-apl\">\u2375\u00d7\u2375\u00d7\u25cb\u00f74<\/code>. The area of the square <code class=\"lang-apl\">[ABCD]<\/code> is twice the area of the triangle <code class=\"lang-apl\">[ABC]<\/code>. Given that the area of the triangle is <code class=\"lang-apl\">0.5\u00d7\u2375\u00d7\u2375\u00f72<\/code>, the area of the square becomes <code class=\"lang-apl\">0.5\u00d7\u2375\u00d7\u2375<\/code>. Putting both together, we get <code class=\"lang-apl\">(\u2375\u00d7\u2375\u00d7\u25cb\u00f74)-\u2375\u00d7\u2375\u00d70.5<\/code>, the same as <code class=\"lang-apl\">\u2375\u00d7\u2375\u00d7(\u25cb\u00f74)-0.5<\/code>, which is <code class=\"lang-apl\">\u2375\u00d7\u2375\u00d70.5-\u2368\u25cb\u00f74<\/code>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-7033\" src=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2021\/12\/circ-sq-300x280.png\" alt=\"Square inside a circle with its diagonal as the circle's diameter\" width=\"300\" height=\"280\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2021\/12\/circ-sq-300x280.png 300w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2021\/12\/circ-sq-322x300.png 322w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2021\/12\/circ-sq.png 448w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<h2 id=\"problem-5-rect-ify\">Problem 5: Rect-ify<\/h2>\n<p>For this problem, we were asked to plant a number of trees in a rectangular pattern with complete rows and columns, meaning all rows have the same number of trees. That rectangular pattern also needed to be as &#8220;square as possible&#8221;, meaning there is a minimal difference between the number of rows and columns in the pattern.<\/p>\n<p>Here&#8217;s a smart solution, based on the observation that the &#8220;most square&#8221; choice must have one factor being the largest factor less than or equal to the square root:<\/p>\n<pre><code class=\"lang-apl\">      p5 \u2190 {N,\u2375\u00f71\u2308N\u2190\u2308\/0,\u2375\u2228\u2373\u230a\u2375*\u00f72}\r\n<\/code><\/pre>\n<p>This solution works well on large numbers of trees, too:<\/p>\n<pre><code class=\"lang-apl\">      p5 98776512304\r\n280888 351658\r\n<\/code><\/pre>\n<p>Someone even offered up a recursive solution:<\/p>\n<pre><code class=\"lang-apl\">      p5rec \u2190 {\u2375=0:2\u23740 \u22c4 \u2375 {0=\u2375|\u237a: \u2375,\u237a\u00f7\u2375 \u22c4 \u237a\u2207\u2375-1} \u230a\u2375*\u00f72}\r\n<\/code><\/pre>\n<p>So is one solution better than the other? Well, they both work correctly, but one is a lot faster than the other. Do you want to guess which was faster before we test it?<\/p>\n<pre><code class=\"lang-apl\">      'cmpx'\u2395CY'dfns'\r\n      cmpx 'p5 98776512304' 'p5rec 98776512304'\r\n  p5 98776512304    \u2192 8.7E\u00af2 |   0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\r\n  p5rec 98776512304 \u2192 6.1E\u00af3 | -94% \u2395\u2395\u2395\r\n<\/code><\/pre>\n<p>Surprised? I was! So, what is going on here? The non-recursive solution relies on a rather crude way to find the factors, which is a fairly large number to factorise even if it only needs to go up to the square root. The recursive version just tries each number in turn, up to the square root.<\/p>\n<p>Can we be even smarter? This version was offered up by <a href=\"https:\/\/apl.chat\" target=\"_blank\" rel=\"noopener\">APL Orchard<\/a> regular <a href=\"https:\/\/chat.stackexchange.com\/users\/470812\/rak1507\" target=\"_blank\" rel=\"noopener\">@rak1507<\/a>:<\/p>\n<pre><code class=\"lang-apl\">      p5rak1507 \u2190 {a,\u2375\u00f71\u2308a\u2190\u2283\u233d\u23780=\u2375|\u2368\u2373\u230a\u2375*.5} \u235d @rak1507\r\n<\/code><\/pre>\n<pre><code class=\"lang-apl\">      cmpx 'p5 98776512304' 'p5rec 98776512304' 'p5rak1507 98776512304'\r\n  p5 98776512304        \u2192 8.7E\u00af2 |   0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\r\n  p5rec 98776512304     \u2192 6.3E\u00af3 | -93% \u2395\u2395\u2395                                     \r\n  p5rak1507 98776512304 \u2192 4.2E\u00af3 | -96% \u2395\u2395\r\n<\/code><\/pre>\n<p>Basically, <code class=\"lang-apl\">(\u22a2\u2228\u2373)<\/code> is neat as a code-golf trick, but not great in terms of efficiency.<\/p>\n<h2 id=\"problem-6-fischer-random-chess\">Problem 6: Fischer Random Chess<\/h2>\n<p>According to Wikipedia, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Fischer_random_chess\" target=\"_blank\" rel=\"noopener\">Fischer random chess<\/a> is a variation of the game of chess invented by former world chess champion <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bobby_Fischer\" target=\"_blank\" rel=\"noopener\">Bobby Fischer<\/a>. Fischer random chess employs the same board and pieces as standard chess, but the starting position of the non-pawn pieces on the players&#8217; home ranks is randomised, following certain rules.<\/p>\n<p>White&#8217;s non-pawn pieces are placed on the first rank according to the following rules:<\/p>\n<ul>\n<li>the Bishops must be placed on opposite-colour squares<\/li>\n<li>the King must be placed on a square between the rooks.<\/li>\n<\/ul>\n<p>The task was to write a function that verifies that a given board placement is valid according to these rules.<br \/>\nThis was my solution for this:<\/p>\n<pre><code class=\"lang-apl\">      q6 \u2190 {(1=+\/(\u2375\u2373'K')&gt;\u2378'R'=\u2375)\u22271=+\/2|\u2378'B'=\u2375}\r\n<\/code><\/pre>\n<p>but there was a lot of variety in the solutions submitted to this problem. For example:<\/p>\n<pre><code class=\"lang-apl\">      q6i  \u2190 {\u2260\/2|\u2378'B'=\u2375}\u2227'RKR'\u2261\u2229\u2218'RK'        \u235d Intersection<\/code><\/pre>\n<pre><code class=\"lang-apl\">      q6ii \u2190 {(\u2260\/2|\u2378'B'=\u2375)\u22271=(\u2378'R'=\u2375)\u2378\u2375\u2373'K'}  \u235d Interval index<\/code><\/pre>\n<pre><code class=\"lang-apl\">      q6w  \u2190 {(\u2260\/2|\u2378'B'=\u2375)\u2227\u2260\/(\u2378'K'=\u2375)&lt;\u2378'R'=\u2375} \u235d Where (similar to mine above)<\/code><\/pre>\n<p>The last version there is very amenable to <em>Over<\/em>:<\/p>\n<pre><code class=\"lang-apl\">            q6over \u2190 {I\u2190\u2378=\u2218\u2375 \u22c4 (2|I'B')\u2227\u2365(\u2260\/)'K'&lt;\u2365I'R'}<\/code><\/pre>\n<p>And for masochists, there is always the famous <a href=\"https:\/\/dyalog.tv\/Webinar\/?v=6SAsgEvUmkU\" target=\"_blank\" rel=\"noopener\">Progressive Dyadic Iota<\/a>:<\/p>\n<pre><code class=\"lang-apl\">      pd \u2190 {((\u2374\u237a)\u2374\u234b\u234b\u237a,\u2375)\u237a\u237a(\u2374\u2375)\u2374\u234b\u234b\u2375,\u237a}\r\n      q6pdi \u2190 {(\u2227\/2\\&lt;\/\u2375\u2373pd'RKR')\u2227\u2260\/2|\u2375\u2373pd'BB'}<\/code><\/pre>\n<h2 id=\"problem-7-can-you-feel-the-magic-\">Problem 7: Can You Feel the Magic?<\/h2>\n<p>A square matrix is &#8216;magic&#8217; if all of its rows and columns and both diagonals sum to the same number.<\/p>\n<p>One hero came up with the following:<\/p>\n<pre><code class=\"lang-apl\">      q7 \u2190 (\u2227\/2=\/\u2218\u220a+\/,(+\/1 2 2\u2218\u2349))\u2349,[0.5]\u233d<\/code><\/pre>\n<p>Here is how it works:<\/p>\n<pre><code class=\"lang-apl\">      q7 magic\u2190\u2395\u21903 3\u23744 9 2 3 5 7 8 1 6\r\n4 9 2\r\n3 5 7\r\n8 1 6\r\n1\r\n      q7 nonmagic\u2190\u2395\u21903 3\u23744 9 2 7 5 3 8 1 6\r\n4 9 2\r\n7 5 3\r\n8 1 6\r\n0<\/code><\/pre>\n<p>The problem statement suggested that dyadic transpose might come in handy, but that&#8217;s just showing off! So, how does it work? It&#8217;s certainly tacit:<\/p>\n<pre><code class=\"lang-apl\">              q7            \u235d Ouch...\r\n    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n  \u250c\u2500\u2534\u2500\u2510               \u250c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2510\r\n  \/ \u250c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2510        \u2349 [0.5] \u233d\r\n\u250c\u2500\u2518 2 \u2218    \u250c\u2500\u253c\u2500\u2500\u2500\u2510    \u250c\u2500\u2518\r\n\u2227    \u250c\u2534\u2510   \/ , \u250c\u2500\u2534\u2500\u2500\u2510 ,\r\n     \/ \u220a \u250c\u2500\u2518   \/    \u2218\r\n   \u250c\u2500\u2518   +   \u250c\u2500\u2518 \u250c\u2500\u2500\u2534\u2500\u2500\u2510\r\n   =         +   1 2 2 \u2349<\/code><\/pre>\n<p>The fork <code class=\"lang-apl\">\u2349,[0.5]\u233d<\/code> takes the argument matrix \u2013 a square array of rank-2, shape <code class=\"lang-apl\">A A<\/code> \u2013 and returns an array of rank-3, shape <code class=\"lang-apl\">2 A A<\/code>, where the first cell is the transposed original array and the second is the original array with its rows reversed:<\/p>\n<pre><code class=\"lang-apl\">      ]display (\u2349,[0.5]\u233d) magic\r\n\u250c\u250c\u2192\u2500\u2500\u2500\u2500\u2510\r\n\u2193\u21934 3 8\u2502\r\n\u2502\u25029 5 1\u2502\r\n\u2502\u25022 7 6\u2502\r\n\u2502\u2502     \u2502\r\n\u2502\u25022 9 4\u2502\r\n\u2502\u25027 5 3\u2502\r\n\u2502\u25026 1 8\u2502\r\n\u2514\u2514~\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<p>We only need to know about the <em>main<\/em> diagonal of each cell; as you can see, the main diagonal in the second cell is the reverse diagonal of the first cell. We can extract both diagonals with a single <em>dyadic transpose<\/em>:<\/p>\n<pre><code class=\"lang-apl\">      1 2 2\u2349(\u2349,[0.5]\u233d) magic\r\n4 5 6\r\n2 5 8\r\n<\/code><\/pre>\n<p>The same result can be achieved using slightly less showy <code class=\"lang-apl\">\u2364<\/code> instead, which has the same byte count but is a little easier to understand when first seen:<\/p>\n<pre><code class=\"lang-apl\">      1 1\u2349\u23642(\u2349,[0.5]\u233d) magic \u235d Diagonals of each major cell love \u2364\r\n4 5 6\r\n2 5 8\r\n<\/code><\/pre>\n<p>The remaining part of the tacit formulation untangles easily. Impressive and creative.<\/p>\n<p>Here&#8217;s another good one that is slightly shorter:<\/p>\n<pre><code class=\"lang-apl\">      q7 \u2190 (1=\u2262\u2218\u222a)\u2349+\u233f\u2364,\u2365(1 1\u2218\u2349,\u22a2)\u233d\r\n      q7 magic\r\n1\r\n      q7 nonmagic\r\n0<\/code><\/pre>\n<p>How does that work? The phrase <code class=\"lang-apl\">1 1\u2218\u2349,\u22a2<\/code> prepends the diagonal as a column to the argument matrix:<\/p>\n<pre><code class=\"lang-apl\">      (1 1\u2218\u2349,\u22a2)magic \u235d Explicit: {(1 1\u2349\u2375),\u2375}\r\n4 4 9 2\r\n5 3 5 7\r\n6 8 1 6\r\n<\/code><\/pre>\n<p>Clever application of <code class=\"lang-apl\">\u2365<\/code> says &#8220;take the matrix, append its reverse <em>over<\/em> the diagonal-append operation&#8221;:<\/p>\n<pre><code class=\"lang-apl\">      {\u2375,\u2365{(1 1\u2349\u2375),\u2375}\u233d\u2375} magic \u235d love \u2365\r\n4 4 9 2 2 2 9 4\r\n5 3 5 7 5 7 5 3\r\n6 8 1 6 8 6 1 8\r\n<\/code><\/pre>\n<p>We can emphasise the location of the diagonals by using <em>Partitioned Enclose<\/em> <code class=\"lang-apl\">\u2282<\/code> to make them stand out a bit:<\/p>\n<pre><code class=\"lang-apl\">      1 1 0 0 1 1 0 0 \u2282 {\u2375,\u2365{(1 1\u2349\u2375),\u2375}\u233d\u2375} magic\r\n\u250c\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u25024\u25024 9 2\u25022\u25022 9 4\u2502\r\n\u25025\u25023 5 7\u25025\u25027 5 3\u2502\r\n\u25026\u25028 1 6\u25028\u25026 1 8\u2502\r\n\u2514\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<p>Summing along the leading axis gives:<\/p>\n<pre><code class=\"lang-apl\">      {+\u233f\u2375,\u2365{(1 1\u2349\u2375),\u2375}\u233d\u2375} magic\r\n15 15 15 15 15 15 15 15\r\n<\/code><\/pre>\n<p>Finally, check that all items are equal:<\/p>\n<pre><code class=\"lang-apl\">      {1=\u2262\u222a+\u233f\u2375,\u2365{(1 1\u2349\u2375),\u2375}\u233d\u2375} magic \u235d Length of vector of unique values = 1?\r\n1\r\n<\/code><\/pre>\n<p>In summary, there are two things to note here: using <code class=\"lang-apl\">\u2365<\/code> to get both diagonals and the use of <code class=\"lang-apl\">1=\u2262\u2218\u222a<\/code> to check that all items are equal. If you attended the <a href=\"https:\/\/www.dyalog.com\/apl-seeds-user-meetings\/aplseeds21.htm\" target=\"_blank\" rel=\"noopener\">APL Seeds &#8217;21<\/a> conference last March, you&#8217;ll recognise this as one of the many ways of solving this problem that <a href=\"https:\/\/twitter.com\/code_report\" target=\"_blank\" rel=\"noopener\">Conor Hoekstra<\/a> presented \u2013 see <a href=\"https:\/\/dyalog.tv\/APLSeeds21\/?v=GZuZgCDql6g\" target=\"_blank\" rel=\"noopener\">https:\/\/dyalog.tv\/APLSeeds21\/?v=GZuZgCDql6g<\/a> to watch his presentation.<\/p>\n<p>Any solution that makes use of both of my favourite glyphs (<code class=\"lang-apl\">\u2364<\/code> and <code class=\"lang-apl\">\u2365<\/code>) is a winner in <a href=\"https:\/\/xpqz.github.io\/learnapl\">my book<\/a>.<\/p>\n<h2 id=\"problem-8-time-to-make-a-difference\">Problem 8: Time to Make a Difference<\/h2>\n<p>Write a function that:<\/p>\n<ul>\n<li>has a right argument that is a numeric scalar or vector of length up to 3, representing a number of <code class=\"lang-apl\">[[[days] hours] minutes]<\/code> \u2013 a single number represents minutes, a 2-element vector represents hours and minutes, and a 3-element vector represents days, hours, and minutes<\/li>\n<li>has a similar left argument, although not necessarily the same length as the right argument<\/li>\n<li>returns a single number representing the magnitude of the difference between the arguments in minutes.<\/li>\n<\/ul>\n<p>Here&#8217;s a cool version (several submissions were similar):<\/p>\n<pre><code class=\"lang-apl\">      p8 \u2190 |-\u2365(1 24 60\u22a5\u00af3\u2218\u2191)<\/code><\/pre>\n<p>Nothing too mysterious here. A slight complication is the need to handle a right argument that can be a scalar or a vector of length 2 or 3. The <em>decode<\/em> function <code class=\"lang-apl\">\u22a5<\/code> expects the argument vector to always be length 3, so we use the <em>take<\/em> function, dyadic <code class=\"lang-apl\">\u2191<\/code>, with <code class=\"lang-apl\">\u00af3<\/code> as the left argument to ensure that the argument is always a vector of the correct length, padding from the left with zeros as required. The mixed radix vector <code class=\"lang-apl\">1 24 60<\/code> as the left argument to <em>decode<\/em> converts to minutes.<\/p>\n<h2 id=\"problem-9-in-the-long-run\">Problem 9: In the Long Run<\/h2>\n<p>Write a function that:<\/p>\n<ul>\n<li>has a right argument that is a numeric vector of 2 or more elements representing daily prices of a stock<\/li>\n<li>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.<\/li>\n<\/ul>\n<p>I&#8217;d like to compare and contrast two solutions, neither of which are tacit for a change:<\/p>\n<pre><code class=\"lang-apl\">      p9a \u2190 {\u2262\u2349\u2191\u2282\u23681,2\u2260\/\u00d72-\/\u2375}\r\n      p9b \u2190 {\u2308\/\u00af2-\/0,\u23781,\u23682\u2260\/\u00d72-\/\u2375} \u235d Flat efficiency\r\n<\/code><\/pre>\n<p>Starting with the first of the two (p9a), from the right, we use a windowed difference reduction to calculate pairwise differences:<\/p>\n<pre><code class=\"lang-apl\">      2-\/1 3 5 6 6 6 6 6 3 2 1\r\n\u00af2 \u00af2 \u00af1 0 0 0 0 3 1 1\r\n<\/code><\/pre>\n<p>and then apply the <em>direction<\/em> function, monadic <code class=\"lang-apl\">\u00d7<\/code>, to turn this into a vector of <code class=\"lang-apl\">\u00af1<\/code>, <code class=\"lang-apl\">0<\/code> and <code class=\"lang-apl\">1<\/code> if the corresponding item is negative, zero or positive respectively:<\/p>\n<pre><code class=\"lang-apl\">      \u00d72-\/1 3 5 6 6 6 6 6 3 2 1\r\n\u00af1 \u00af1 \u00af1 0 0 0 0 1 1 1\r\n<\/code><\/pre>\n<p>Another pairwise windowed reduction, this time with <code class=\"lang-apl\">\u2260<\/code>, gives us the points of change:<\/p>\n<pre><code class=\"lang-apl\">      2\u2260\/\u00d72-\/1 3 5 6 6 6 6 6 3 2 1\r\n0 0 1 0 0 0 1 0 0\r\n<\/code><\/pre>\n<p>Prepending a 1, this Boolean vector can be used as the left argument to <em>partitioned enclose<\/em>, <code class=\"lang-apl\">\u2282<\/code>; a common pattern. But what of the right argument? We can use the same vector as the right argument by using a clever <em>commute<\/em>, <code class=\"lang-apl\">\u2368<\/code>:<\/p>\n<pre><code class=\"lang-apl\">      \u22a2m\u2190\u2282\u23681,2\u2260\/\u00d72-\/1 3 5 6 6 6 6 6 3 2 1 \u235d Commute to use the same argument left and right\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u25021 0 0\u25021 0 0 0\u25021 0 0\u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<p>What remains is to find the longest cell in this vector. We could do <code class=\"lang-apl\">\u2308\/\u2262\u00a8<\/code>, but instead this submission found the length of the transpose-mix:<\/p>\n<pre><code class=\"lang-apl\">      \u2262\u2349\u2191 m\r\n4\r\n<\/code><\/pre>\n<p>A code-golfer&#8217;s trick shot, perhaps, and somewhat dubious in terms of efficiency, but certainly cute. If you don&#8217;t see why it works, work it through right to left!<\/p>\n<p>The second solution (p9b) uses a lot of the same ideas, but this time we add a 1 to the end of the points-of-change vector:<\/p>\n<pre><code class=\"lang-apl\">      {1,\u23682\u2260\/\u00d72-\/\u2375}1 3 5 6 6 6 6 6 3 2 1\r\n0 0 1 0 0 0 1 0 0 1\r\n<\/code><\/pre>\n<p>and use <em>where<\/em>, monadic <code class=\"lang-apl\">\u2378<\/code>, to get the indices, prepending a <code class=\"lang-apl\">0<\/code> so that we can calculate the length of each segment:<\/p>\n<pre><code class=\"lang-apl\">      {0,\u23781,\u23682\u2260\/\u00d72-\/\u2375}1 3 5 6 6 6 6 6 3 2 1\r\n0 3 7 10\r\n<\/code><\/pre>\n<p>The pairwise difference now represents the length of each segment, and by using a negative window we can commute each pair to get a positive number out for each pair:<\/p>\n<pre><code class=\"lang-apl\">      {\u00af2-\/0,\u23781,\u23682\u2260\/\u00d72-\/\u2375}1 3 5 6 6 6 6 6 3 2 1\r\n3 4 3\r\n<\/code><\/pre>\n<p>and so, for the maximum:<\/p>\n<pre><code class=\"lang-apl\">      {\u2308\/\u00af2-\/0,\u23781,\u23682\u2260\/\u00d72-\/\u2375}1 3 5 6 6 6 6 6 3 2 1\r\n4\r\n<\/code><\/pre>\n<p>Shall we race them? Of course!<\/p>\n<pre><code class=\"lang-apl\">      data \u2190 10000?10000\r\n      cmpx 'p9a data'\r\n  p9a data \u2192 2.7E\u00af4 |   0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\r\n  p9b data \u2192 2.1E\u00af5 | -92% \u2395\u2395\u2395\r\n<\/code><\/pre>\n<p>The second version is faster for several reasons. We suspected already that the &#8216;cute&#8217; way to find the longest vector in a nested vector was likely to be slow, as it has to create a huge matrix first, chasing pointers. The second version uses flat numeric vectors throughout, and cuts the work considerably by using <em>where<\/em> initially to do length calculations on the shorter vector of indices. Flat is fast.<\/p>\n<h2 id=\"problem-10-on-the-right-side\">Problem 10: On the Right Side<\/h2>\n<p>Write a function that:<\/p>\n<ul>\n<li>has a right argument <code class=\"lang-apl\">T<\/code> that is a character scalar, vector or vector of character vectors\/scalars<\/li>\n<li>has a left argument <code class=\"lang-apl\">W<\/code> that is a positive integer specifying the width of the result<\/li>\n<li>returns a right-aligned character array <code class=\"lang-apl\">R<\/code> of shape <code class=\"lang-apl\">((2=|\u2261T)\/\u2262T),W<\/code> meaning that <code class=\"lang-apl\">R<\/code> is one of the following:\n<ul>\n<li>a <code class=\"lang-apl\">W<\/code>-wide vector if <code class=\"lang-apl\">T<\/code> is a simple vector or scalar<\/li>\n<li>a <code class=\"lang-apl\">W<\/code>-wide matrix with the same number rows as elements of <code class=\"lang-apl\">T<\/code> if <code class=\"lang-apl\">T<\/code> is a vector of vectors\/scalars<\/li>\n<\/ul>\n<\/li>\n<li>if an element of <code class=\"lang-apl\">T<\/code> has length greater than <code class=\"lang-apl\">W<\/code>, truncate it after <code class=\"lang-apl\">W<\/code> characters.<\/li>\n<\/ul>\n<p>The last point is perhaps a bit misleading, but the intention is clear from one of the examples given:<\/p>\n<pre><code class=\"lang-apl\">      \u2374\u2395\u21908 (your_function) 'Longer Phrase' 'APL' 'Parade'\r\nr Phrase\r\n     APL\r\n  Parade\r\n3 8\r\n<\/code><\/pre>\n<p>In this case, &#8220;truncate after <code class=\"lang-apl\">W<\/code> characters&#8221; means &#8220;remove from the left&#8221;.<br \/>\nConceptually, we need to (over)take <code class=\"lang-apl\">W<\/code> characters from the right of each element and mix that into a rank-2 array. To make it work for the edge cases, we should ensure that we can always treat the right argument as a vector of character vectors, using <em>nest<\/em>, monadic <code class=\"lang-apl\">\u2286<\/code>. This works because if we take more characters than the vector contains, it gets padded using a character-vector&#8217;s prototype element, a space.<\/p>\n<pre><code class=\"lang-apl\">            8 {\u2191(-\u237a)\u2191\u00a8\u2286\u2375} 'Longer Phrase' 'APL' 'Parade'\r\nr Phrase\r\n     APL\r\n  Parade\r\n<\/code><\/pre>\n<p>An equivalent tacit formulation would be:<\/p>\n<pre><code class=\"lang-apl\">            8 (\u2191-\u2364\u22a3\u2191\u00a8\u2286\u2364\u22a2) 'Longer Phrase' 'APL' 'Parade'\r\nr Phrase\r\n     APL\r\n  Parade\r\n<\/code><\/pre>\n<p>Here&#8217;s a slight variation:<\/p>\n<pre><code class=\"lang-apl\">            8 {\u233d\u2349\u237a\u2191\u2349\u2191\u233d\u00a8\u2286\u2375}'Longer Phrase' 'APL' 'Parade'\r\nr Phrase\r\n     APL\r\n  Parade\r\n<\/code><\/pre>\n<p>This starts by reversing each cell, then applies a <em>mix<\/em> and <em>transpose<\/em>. We then take items from the <em>left<\/em> before backing out of the transpose and reverse by applying them again.<br \/>\nIt can be done in a flatter manner, too:<\/p>\n<pre><code class=\"lang-apl\">            8{\u2349(-\u237a)\u2191\u2349(\u2286\u2375)\u233d\u2218\u2191\u2368(\u22a2-\u2308\/)\u2262\u00a8\u2286\u2375} 'Longer Phrase' 'APL' 'Parade'\r\nr Phrase\r\n     APL\r\n  Parade\r\n<\/code><\/pre>\n<p>If we flip the selfie and add a few spaces it gets a bit easier to see what&#8217;s going on:<\/p>\n<pre><code class=\"lang-apl\">            8 {\u2349(-\u237a)\u2191\u2349 ((\u22a2-\u2308\/)\u2262\u00a8\u2286\u2375) \u233d\u2191\u2286\u2375} 'Longer Phrase' 'APL' 'Parade'\r\nr Phrase\r\n     APL\r\n  Parade\r\n<\/code><\/pre>\n<p>From the right, we turn our input into a character array and then <em>Rotate<\/em> each row by its length minus the length of the longest row, which implements the right alignment:<\/p>\n<pre><code class=\"lang-apl\">            {((\u22a2-\u2308\/)\u2262\u00a8\u2286\u2375) \u233d\u2191\u2286\u2375} 'Longer Phrase' 'APL' 'Parade'\r\nLonger Phrase\r\n          APL\r\n       Parade\r\n<\/code><\/pre>\n<p>What remains is the truncation, which follows similar lines to the earlier versions.<\/p>\n<p>For completeness we can race a couple of these variants. Let&#8217;s generate a chunkier data set first: 10,000 random strings of varying lengths up to 50:<\/p>\n<pre><code class=\"lang-apl\">      data\u2190{\u2395A[?(?50)\u237426]}\u00a8\u237310000      'cmpx'\u2395CY'dfns'\r\n      cmpx '20{\u2191(-\u237a)\u2191\u00a8\u2286\u2375}data' '20{\u2349(-\u237a)\u2191\u2349(\u2286\u2375)\u233d\u2218\u2191\u2368(\u22a2-\u2308\/)\u2262\u00a8\u2286\u2375}data' '20{\u233d\u2349\u237a\u2191\u2349\u2191\u233d\u00a8\u2286\u2375}data'\r\n  20{\u2191(-\u237a)\u2191\u00a8\u2286\u2375}data                 \u2192 9.3E\u00af4 |   0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395             \r\n  20{\u2349(-\u237a)\u2191\u2349(\u2286\u2375)\u233d\u2218\u2191\u2368(\u22a2-\u2308\/)\u2262\u00a8\u2286\u2375}data \u2192 9.0E\u00af4 |  -3% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395              \r\n  20{\u233d\u2349\u237a\u2191\u2349\u2191\u233d\u00a8\u2286\u2375}data                \u2192 1.4E\u00af3 | +46% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\r\n<\/code><\/pre>\n<p>The complex, flat version wins, but not by a significant amount.<\/p>\n<p>With that we&#8217;ve reached the end. A nice set of problems, with a lot of creative solutions submitted. Watch this space for a review of the Phase II problems&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By: Stefan Kruger Stefan works for IBM making databases. He tries to learn at least one new programming language a year, and a few years ago he got hooked on APL and participated in the competition. This is his perspective &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2022\/03\/the-2021-apl-problem-solving-competition-phase-i-best-of-breed\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-7043","post","type-post","status-publish","format-standard","hentry","category-student-competition"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/7043","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=7043"}],"version-history":[{"count":69,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/7043\/revisions"}],"predecessor-version":[{"id":7126,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/7043\/revisions\/7126"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=7043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=7043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=7043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}