[aplusdev] Neophyte to A+ tries out New Scientist puzzle

Neil L. Roeth neil at occamsrazor.net
Sat Mar 10 22:38:44 EST 2001


My comments on your code follow immediately, and my solution to the
puzzle follows that.  Your comments have a single comment character,
mine have a triple comment character.  You don't really need any
explicit loops.

------------------------------------------------------------------------
UNIQUE{x}:{((xÉx)=É#x)/x}

(f reduce) v:{rû>0#v; (iû¢1+#v) do rûr f >(1+i)#v} ã operators
ã newsci uses ,/ =/ which are not part of a+

a (f outer) b:{rû((#a),#b)Ò<0#a;(iû#a) do r[i;]û(i#a) f¡ b;r}
ã similarly newsci uses Ê., and =., which are not part of a+

newsci{}:{rû,reduce (<4)î¡r ,outer rû(31+1+É68)*2;
       ã  AAAAAAA BBBBB CCCCCCCCCC DDDDDDDDDDDD
       ãD:compute possible squares; C: form pairs of squares
       ãB:format each pair; A: catenate 4624 pairs to length 8 strings

	ããã You can create the possible pairs by rûr~@1 0r, then
	ããã reshape it with rû0 2 1ôr (no reduce operator with its
	ããã loop necessary)

	ããã Then you can create the formatted concatenated strings
	ããã with >,<@1, at 1Û4î@1r

       ã
        rû(^reduce at 1 =reduce at 1¡ (r[;0 3];r[;1 6]))/<@1 r;
       ã   AAAAAAAAA BBBBBBBBBB  CCCCCCCCCCCCCCC  DDDD
       ã D:require 'NEUNVIEA' pattern,eg dups at (IOûý0) 0,3 and 1,6.
       ã   Remove rows of r missing these dups by; BC:checking pairwise 
       ã   identity; A: checking both identities hold.

	ããã You could do (r[;0]=r[;3])^(r[;1]=r[;6])/r

       ã
        rû(6=,>Ò¡UNIQUE¡ r)/r; 
       ã Pattern requires 6 unique characters

	ããã (6=Ø#¡UNIQUE¡r) is slightly simpler
       ã
        rû(2=+/>u/¡uû+/¡>¡^/¡¡t =outer¡ uûUNIQUE¡ tû(Òr)Ú,ô4Ú@1 >r)/r;
       ã  KA BBCCCCC DDDEEFFFFGGGGGGGGGGG IIIIIII   JJJJJJJJJJJJJJKKK 
       ã K:select row from r with unique entries in each group of 4 columns
       ã J:4Ú split strings into two words, (Òr)Ú nests (1st wrds; 2nd wrds)
       ã GI: find occurrance of unique 1st and 2nd wrds; DEF: count frequency
       ã C:expand u so each has Òr elements; B:add freqs of 1st and 2nd wrds
       ã A:unique rows in r should have 2 unique words

	ããã rû>Ø(<^/>tÅ¡(y/¡1=¡yûÚ¡1,¡(1Õ¡x)¨¡¢1Õ¡x)/¡xû(è¡>¡t)#¡t)/¡t
	ããã i.e., sort, partition, count partitions where num=1, find these
	ããã nums in t.  Does not require 'outer' operator.

       r}
------------------------------------------------------------------------
u_mask{x}:{ (xÉx)=É#x }
unique{x}:{ u_mask{x}/x }
newsci{}:{
	ã Form all squares that result in four digit results
	rûr«rûmin+É1+(maxûÄ9999*.5)-minûÓ1000*.5;
	ã turn into strings so we can compare individual digits
	rû1Õ@1î@0r;
	ã create mask of NEUN candidates
	neunûr[;0]=r[;3];
	ã create mask of VIER candidates
	vierûr[;2]=@0 1neun/r[;1];
	ã get NEUN candidates, expanded out to match the number of
	ã corresponding VIER candidates for each
	nrû(+/vier)/neun/r;
	ã get VIER candidates
	vrûØ(<@1ôvier)/¡<r;
	ã reshape for ease of manipulation
	sû1 0 2ônr~vr;
	ã each digit must be unique, so there must be 6 unique digits out of 8
	sû(6=Ø#¡unique¡<@1, at 2s)/s;
	ã get cases where each number uniquely identifies the others,
	ã i.e., where each appears only once in all pairs
	sû(u_mask{0#@2s}^(÷u_mask{÷0#@2s})^u_mask{1#@2s}^(÷u_mask{÷1#@2s}))/s;
	s
}
------------------------------------------------------------------------


On Mar  5, Andrew Reilly (aar at wadsworth.org) wrote:
 > At the risk of being scolded to RTFM I am asking for feedback
 > on the script: http://www.albany.edu/~areilly/newsci.jpg. Although
 > it returns the correct answer I am wondering if someone familiar with
 > A+ might provide pointers to improve the code....Thanks.
 > 
 > >This puzzle was originally posted on a mailing list for the Icon
 > >programming language.  Thought members of this group might also
 > >want to give it a shot.
 > 
 > >VIER and NEUN represent 4-digit squares, each letter denoting a
 > >distinct digit. You are asked to find the value of each, given the
 > >further requirement that each uniquely determines the other.
 > 
 > >The "further requirement" means that of the numerous pairs of
 > >answers, choose the one in which each number only appears once
 > >in all of the pairs.
 > 
 > >Morten Kromberg <mkromberg at INSIGHT.DK> wrote:
 > 
 > >Take a look at http://www.ckkronborg.dk/nsapl.htm for one APL solution
 > >I've put an image of iton the web page to avoid problems with APL special 
 > >symbols.
 > 
 > 
 > > Morten
 > =================
 >                   Andrew A. Reilly
 > 
 >            Internet: Andrew.Reilly at Wadsworth.Org 
 >            Voice: 518-473-3493 FAX:518-474-2769
 >                     Beeper:518-498-9394.  
 >       Wadsworth Center for Labs and Research. Rm. C543
 >              Empire State Plaza. P.O. Box 509.  
 >                    Albany, NY 12201-0509
 > 
 > 
 > 

-- 
Neil L. Roeth
neil at occamsrazor.net



More information about the apluslist mailing list