[Jprogramming] guidelines for writing J code that is less-dependenton data layout

Miller, Raul D rdmiller at usatoday.com
Thu Jul 20 07:04:19 HKT 2006


Here's a version of Sashikanth Chandrasekaran's solution for
Einstein's problem which uses names instead of numbers in
code to select rows/columns.

Beware line wrap, some of these lines are too long for some email
software.

Note that I made the decision to make row selectors be functions of
the form n&{ which required some minor conceptual changes (and might
have resulted in some fractional efficiency loss).

I need to study this solution a bit more, as there are some aspects
which still aren't completely obvious to me.

Finally, note that I changed all assignments to global so that
the result can be inspected more easily.

--------------


NB. Sashikanth Chandrasekaran's brute-force solution for Einstein's
Riddle
grid =: ;:;._2 (0 : 0)
  Brit Sweede Dane German Norwegian
  Red Green White Yellow Blue
  Dogs Birds Cats Horses Fish
  Tea Coffee Milk Beer Water
  Pall_Mall Dunhill Blends Blue_Master Prince
)

NB. atom -> gerund of constant function
constF=:3 :'y"_`'''''"0

NB. define above names as attribute index functions
('`',;:inv,grid)=:,constF 5#i.1 5

NB. row selectors
'`nation color pet drink smoke'=:,3 :'(y&{)`'''''"0 i.5

NB. Table of permutations, copied from J vocabulary page for A..
tap =: i.@! A. i.

NB. The Brit lives in a red house. 
h =: (#~ ((color i. Red) = nation i. Brit)"2) ,/ ,:"1"1 _~tap 5

NB. The Swede keeps dogs as pets. 
NB. #inv...>:tap 4 implies Dogs as this is the third h row
h =: ,/ h ,"_ 1"2 ((nation ~: Sweede)"2 h) #inv"1"1 _ >:tap 4

NB. The Dane drinks tea. 
NB. #inv...>:tap 4 implies Tea as this is the fourth h row
h =: ,/ h ,"_ 1"2 ((nation ~: Dane)"2 h) #inv"1"1 _ >:tap 4

NB. The Green house is next to, and on the left of the White house. 
h =: (#~ ([: (1 = (i.White) - (i.Green)) color)"2) h

NB. The owner of the Green house drinks coffee. 
h =: (#~ ((drink i. Coffee) =  color i.Green)"2) h

NB. The person who smokes Pall Mall rears birds.
NB. #inv...>:tap 4 implies Pall Mall as this is the fifth h row 
h =: ,/ h ,"_ 1"2 ((pet ~: Birds)"2 h) #^:_1"1"1 _ >:tap 4

NB. The owner of the Yellow house smokes Dunhill. 
h =: (#~ ((smoke i. Dunhill) = color i. Yellow)"2) h

NB. The man living in the centre house drinks milk. 
h =: (#~ Milk = 2{"1 drink"2) h

NB. The Norwegian lives in the first house. 
h =: (#~ (Norwegian = 0{"1 nation"2)) h

NB. The man who smokes Blends lives next to the one who keeps cats. 
h =: (#~ ([: e.&_1 1 (pet i. Cats) - smoke i. Blends)"2) h

NB. The man who keeps horses lives next to the man who smokes Dunhill. 
h =: (#~ ([: e.&_1 1 (smoke i. Dunhill) - pet i.Horses)"2) h

NB. The man who smokes Blue Master drinks beer. 
h =: (#~ ((drink i. Beer) = smoke i. Blue_Master)"2) h

NB. The German smokes Prince. 
h =: (#~ ((smoke i. Prince) = nation i.German)"2) h

NB. The Norwegian lives next to the blue house. 
h =: (#~ ([: e.&_1 1 (color i.Norwegian) - nation i.Blue)"2) h

NB. The man who smokes Blends has a neighbour who drinks water.
h =: (#~ ([: e.&_1 1 (drink i.Water) - smoke i.Blends)"2) h

NB. display the valid combinations
h {"1"_1 _ grid

-- 
Raul



More information about the Programming mailing list