0

The particular question asks to find all palindrome numbers between 900 and 1000.

new_set ← (900 + ⍳100)
901, 902, 903, 904, ..., 999, 1000

⍕¨(new_set ∘.× new_set)
{⍵≡⌽⍵} ⍕¨(new_set ∘.× new_set)

The first line creates the new set of numbers.

The second line multiplies all numbers in new_set by every other number including itself. Creates a sort of outer product multiplication table, then converts the numbers into strings.

Last line attempts to find all palindromes, but fails and returns 0..

Any and all help is appreciated!

ArbIn
  • 43
  • 5

1 Answers1

2

⍕¨(new_set ∘.× new_set) has shape 100 100, and Match () compares entire arrays, so your final dfn there is comparing a 100 by 100 array against the Reverse () of the whole array.

What it looks like you're wanting is to reverse individual strings. Trying to copy your style, we get something like this:

{(⊃⍵)≡⌽⊃⍵}¨ ⊂∘⍕¨ (new_set ∘.× new_set)

Notice the double usage of Each (¨) and needing to Enclose () the strings just for the second Each and then immediately Disclose () in the dfn. This should probably feel dirty.

We can do better.

First, why not merge the two Each iterations?

{(⊃⍵)≡⌽⊃⍵}∘⊂∘⍕¨ (new_set ∘.× new_set)

But, in fact, Outer Product (∘.×) is already operating on exactly the items we want, so it make sense to just do everything there:

new_set ∘.{(⊢≡⌽)⍕⍺×⍵} new_set

N.B. Instead of repeating new_set on the left and right, it's probably more idiomatic to use Commute (). I'll also throw in another spelling of (⊢≡⌽), just for fun:

∘.{≡∘⌽⍨⍕⍺×⍵}⍨ new_set

Turning this binary mask into the actual set of palindromes is pretty straightforward, so I'll not spoil that fun.

B. Wilson
  • 66
  • 3
  • Thank you for your help, B. Wilson! I attempted to figure out the binary mask trick... Right now, I have: ∘.{≡∘⌽⍨⍕⍺×⍵}⍨ new_set / (new_set ∘.× new_set) as I am compressing all the 0s and 1s into the order product table that I originally had. However, it gives me a "Rank Error". Would you mind helping me a little more? – ArbIn Oct 17 '22 at 15:33
  • The second command I ran in TryAPL is: {(⊃⍵)≡⌽⊃⍵}¨ ⊂∘⍕¨ (new_set ∘.× new_set) ⊆ ∘⍕¨ (new_set ∘.× new_set). This command works, but doesn't give me what I want :(.. When I make it into a compressor {(⊃⍵)≡⌽⊃⍵}¨ ⊂∘⍕¨ (new_set ∘.× new_set) / ∘⍕¨ (new_set ∘.× new_set), there is a warning "WS FULL: Maximum workspace is 512 kilobytes". – ArbIn Oct 17 '22 at 16:02
  • Let's see. First, be careful with order of evaluation. Your left argument to `/` needs to be parenthesized. With that, we know the left and right arguments are fine, by executing them independently, so the rank error is happening in side the Replicate (`/`) call. Have you checked the docs for Replicate over at help.dyalog.com? In particular, the left argument needs to be a *vector*, but here you're feeding it a 2D array. Since you don't actually need the shape information, Enlisting (`∊`) both arguments should get you what you want! – B. Wilson Oct 18 '22 at 01:23
  • Good advice. I have done that. I appreciate the helpful suggestions. I have accepted your answer, B. Wilson. – ArbIn Oct 18 '22 at 02:12