This is a place where I did not test first, but instead got caught up in
the urge to hurl out J code. If I had put my impressions of the search
space, I'd have probably used the cases _1 0 1 = item_price | total_bill .
You say I got the wrong answer. I was just pointing out why, which is a
form of agreement that I did get the wrong answer. Of course, it
remains to be seen whether I would have gotten the right answer by
chance. The only real test would be:
expected_total_price = item_counts +/ . * item_prices
Which I recall you demonstrating. After the fact corrections to the
code are only justified by reworking the terms of the problem: different
prices, different total, what is the smallest total price that can be
obtained in more than one way.
Roger Hui wrote:
Randy MacDonald writes:
This is why I test first. A simple example would have washed out my
off-by-ones.
I don't know what you tested, but your proposed expression gives
the wrong (or, at least, incomplete) answers whereas my original
expression gives the correct answers:
Original problem: http://xkcd.com/287/
a=: 215 275 335 355 420 580
ord=: 3 : 0
b=. 1505 = ((#: i.@(*/)) y) +/ .* a
z=. y #: I. b
assert. 1505 = z +/ .* a
)
ord >. 1505 % a NB. MacDonald
1 0 0 2 0 1
ord 1 + <. 1505 % a NB. Hui
1 0 0 2 0 1
7 0 0 0 0 0
1505 % a
7 5.47273 4.49254 4.23944 3.58333 2.59483
>. 1505 % a
7 6 5 5 4 3
1 + <. 1505 % a
8 6 5 5 4 3
Even if no element of a divides m so that >.m%a is
equivalent to 1+<.m%a , I would not have used the
first expression. I might have considered <.(1+m)%a .
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
--
------------------------------------------------------------------------
|\/| Randy A MacDonald | APL: If you can say it, it's done.. (ram)
|/\| ramacd <at> nbnet.nb.ca |
|\ | | The only real problem with APL is that
BSc(Math) UNBF'83 | it is "still ahead of its time."
Sapere Aude | - Morten Kromberg
Natural Born APL'er |
-----------------------------------------------------(INTP)----{ gnat }-
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm