I tried this and got
out of memory: script
| h=. ,/h,"_ 1"2(([:~:&1(2&{))"2 h)#^:_1"1"1 _>:tap 4
|[-28] c:\j601\temp\17.ijs
2006/7/19, Sashikanth Chandrasekaran <[EMAIL PROTECTED]>:
Assume that there are N entities each of which has M
attributes and I save the values as an N*M array. If I
want to work on the first attribute, it is tempting to
use {."1 . Similarly, if I want to work on the 3rd
attribute it is tempting to use 2{"1 and so on. But
this makes the program dependent on the data layout -
if I add one more attribute in the middle of the row,
the program breaks.
Are there any best practices/style guidelines to make
the code we write less dependent on data layout
without sacrificing performance? I presume that
"hiding" data inside a class and using trivial get/put
functions to access the data is not the J style.
To use a concrete example, I coded a simple solution
for the so-called Einstein riddle
(http://www.naute.com/puzzles/puzzle13.phtml) below.
As you can see the code is completely tied to the
location of the possible values in the grid - i.e. the
code will break if I switch 'Red' and 'Green' or
switch two rows etc. What is the recommended method
for making it less fragile? Thx, -sashi.
NB. 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. Table of permutations, copied from J vocabulary
page for A..
tap =. [EMAIL PROTECTED] A. i.
NB. The Brit lives in a red house.
h =. (#~ (([: i.&0 {:) = [: i.&0 {.)"2) ,/ ,:"1"1 _~
tap 5
NB. The Swede keeps dogs as pets.
h =. ,/ h ,"_ 1"2 (([: ~:&1 {.)"2 h) #^:_1"1"1 _ >:
tap 4
NB. The Dane drinks tea.
h =. ,/ h ,"_ 1"2 (([: ~:&2 {.)"2 h) #^:_1"1"1 _ >:
tap 4
NB. The Green house is next to, and on the left of the
White house.
h =. (#~ ([: (1: = i.&2 - i.&1) 1&{)"2) h
NB. The owner of the Green house drinks coffee.
h =. (#~ (([: i.&1 {:) = [: i.&1 (1&{))"2) h
NB. The person who smokes Pall Mall rears birds.
h =. ,/ h ,"_ 1"2 (([: ~:&1 (2&{))"2 h) #^:_1"1"1 _ >:
tap 4
NB. The owner of the Yellow house smokes Dunhill.
h =. (#~ (([: i.&1 {:) = [: i.&3 (1&{))"2) h
NB. The man living in the centre house drinks milk.
h =. (#~ (2: = (<3 2)&{)"2) h
NB. The Norwegian lives in the first house.
h =. (#~ (4: = (<0 0)&{)"2) h
NB. The man who smokes Blends lives next to the one
who keeps cats.
h =. (#~ ([: e.&_1 1 ([: i.&2 (2&{)) - [: i.&2 {:)"2)
h
NB. The man who keeps horses lives next to the man who
smokes Dunhill.
h =. (#~ ([: e.&_1 1 ([: i.&1 {:) - [: i.&3 (2&{))"2)
h
NB. The man who smokes Blue Master drinks beer.
h =. (#~ (([: i.&3 (3&{)) = [: i.&3 {:)"2) h
NB. The German smokes Prince.
h =. (#~ (([: i.&4 {:) = [: i.&3 {.)"2) h
NB. The Norwegian lives next to the blue house.
h =. (#~ ([: e.&_1 1 ([: i.&4 (1&{)) - [: i.&4 {.)"2)
h
NB. The man who smokes Blends has a neighbour who
drinks water.
h =. (#~ ([: e.&_1 1 ([: i.&4 (3&{)) - [: i.&2 {:)"2)
h
NB. display the valid combinations
h {"1"_1 _ grid
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
--
Björn Helgason, Verkfræðingur
Fugl&Fiskur ehf, Þerneyjarsund 23,
Skype: gosiminn, gsm: +3546985532
801 Grímsnes ,t-póst: [EMAIL PROTECTED]
Landslags og skrúðgarðagerð, gröfuþjónusta
http://groups.google.com/group/J-Programming
Tæknikunnátta höndlar hið flókna, sköpunargáfa er meistari einfaldleikans
góður kennari getur stigið á tær án þess að glansinn fari af skónum
/|_ .-----------------------------------.
,' .\ / | Með léttri lund verður |
,--' _,' | Dagurinn í dag |
/ / | Enn betri en gærdagurinn |
( -. | `-----------------------------------'
| ) | (\_ _/)
(`-. '--.) (='.'=)
`. )----' (")_(")
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm