i have a suggestion that feels like a bit of a cheat (because it
converts the number to a string. But, i reckon, once we're dealing with
digits we're regarding the number as a string anyway. i.e as a sequence
of digits, there's nothing to suggest that the number base is relevant.
So really, it's a string.)
isDistinct =. =/@$@=@": ("0)
isDistinct 1231 1234
NB. 0 1
On 2011-08-17 11:55, Alex Giannakopoulos wrote:
> Hi
>
> I am trying to write a verb that will tell me if the digits in a number are
> distinct,
> e.g. return 1 for 1234 but return 0 for 1231
>
> So far I have the following, but I feel it could be tidied up a bit.
> Any suggestions welcome
>
> filter =: 1 : #~ x
> digits =: 10&#.^:_1
> distinct =: [: *./ "1 [: ~:"1 digits
> distinct filter 100 101 102 103
> 102 103
>
> It seems to work OK (I appreciate that number lengths must all be the same
> to avoid padded zero problems), but are there any ways to tidy it up,
> especially re the "1 directives?
>
> Many Thanks
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm