Hot answers tagged

11

Initial impression Your code is already quite good, using idiomatic APL in short clear lines that each do a single job well. Your variable names are such that you don't really need comments other than the fine description you already have at the top. Describe your result You might want to add a third comment describing the result structure: ⍝ Returns a ...


8

On your previous version you commented, "This works and gives the expected result for a series of test cases." But you never provided those test cases, right? I think the biggest thing missing here is test cases. Especially since test cases would quickly clarify the expected behavior of the function on weird inputs, and then you could maybe even get rid of ...


7

Generating possible moves Assuming that the order of the elements in the output does not matter (e.g. (2 3)(3 2) and (3 2)(2 3) are equally valid outputs for the input 1 1), it suffices to generate some permutation of (1 2)(2 1)(¯1 2)(2 ¯1)(1 ¯2)(¯2 1)(¯1 ¯2)(¯2 ¯1). Using the signs-and-offsets method you used, we want the equivalent of signs ← (1 1)(1 ¯1)...


6

Answers to OP's questions Are the extra comments ok or are they too much? Looks good to me overall, though some of them contain redundant information (that is already explained as variable names): ⍝ aux train to check if position is inside chessboard isInsideBoard ← ∧/(1∘≤∧≤∘8) Compare it with, e.g. ⍝ checks if position is inside chessboard, i.e....


5

Redundant parenthesis isInsideBoard ← ∧/(1∘≤∧≤∘8) was converted from inline explicit code. Back then, the train 1∘≤∧≤∘8 needed parenthesising. However, now that you've broken out this code to a separate tacit function, the ∧/ actually forms an atop (a 2-train) with the existing train, and since the original train was a fork (has 3 parts), it can simply be a ...


5

Regarding the primary question, your code is very close to being a well-defined function. Simply wrap the entire code in a function, making filename the right argument, fpath the left argument, and allUsed the result like so (with a couple of unreference lines omitted): GetAllUsed←{ filename←⍵ fpath←⍺ getxml←{⎕XML⊃⎕NGET ⍵} ...


4

I think your code generally looks good. Comments I recommend annotating functions with what the structure of their argument(s) and result are, especially when not just simple arrays, at the top of the function, rather than relying on code comments to reveal this. Take benefit of dyadic functions If you define Insert and Pop as dyadic functions both code ...


3

I am particularly interested in a better way to create the classes variable, [...] this ordering depends obviously on the order of splits. Because ASCII codes of A..Z come later than those of 0..9, we can't shorten the classes array itself. Instead, we can lengthen it, so that classes becomes simply two copies of 0..9. You can create unused intervals by ...


3

Think about edge cases Although the original problem statement doesn't mention it (nor the test cases provided), I can think of at least two kinds of edge cases: Handling extraneous spaces (leading spaces, trailing spaces, or multiple spaces between words, e.g. __I___like__blanks___) Handling capitalization (e.g. Creep -> eepCray or Eepcray?) Notably, ...


3

Overall Your approach is fine, and your code (including ≠⊆⊢) is fairly idiomatic. Handling the edge case by always appending a space and dropping it at the end is standard procedure, so no, you don't need a branch here. Split up your code in sections You begin with setting up a couple of constants. Consider inserting a blank line to gently separate these ...


3

I believe the DoubleDegrees and ConnectedComponents functions are sub-optimal since they use simple algorithms but make use of matrix multiplications and search algorithms would be faster (in other languages). Is this still efficient code for APL? Or would a search-based solution be more efficient? Many APL implementations, especially Dyalog's, are heavily ...


2

I think using regular expressions is a perfectly sensible approach. What you're missing is probably that Dyalog APL allows setting a Match Limit with ⍠'ML' n where a positive n limits to the first n matches and a negative n limits to the (absolute value of) nth match. With this in mind, I'd use regular expressions extensively: AbbreviateIPv6 ← { ...


2

APL-specific Keep the nesting levels consistent At this line: (key1 left right)←1↓t1 ⋄ key2←1⌷t2 key1 is effectively disclosed one level, while key2 is not. It doesn't matter in this code because both key1 and key2 are assumed to be scalars, but they are semantically different: ⍝ Assume ⎕IO←1 (a b c)←nested←(1 2 3)(4 5 6)(7 8 9) 1 2 3≡a 1 ...


2

Having taken into account the feedback I got from the three answers that were posted, plus using my own brain, I think a good revision of the code in the question entails: ensuring signs is a vector instead of a matrix by using , right before assigning; moving the definition of the function isInsideBoard closer to where it is used; renaming the function ...


2

How could I make it look more like APL (and less like Python)? Assuming you mean "in a more functional and less imperative way" by this line, I don't think you can largely achieve that, for a good reason. Basically, the array-based heap (and other common algorithms you see on algorithm textbooks) is designed for imperative languages. Translating it into a ...


Only top voted, non community-wiki answers of a minimum length are eligible