Alexander (inv2004) wrote:
> Description I am trying to calculate:
> http://www.usacycling.org/news/user/story.php?id=580
> source data: https://dl.dropboxusercontent.com/u/34917039/2013.txt
> J source: https://dl.dropboxusercontent.com/u/34917039/1.j
I knocked together an implementation in J (based on my perhaps-mistaken
understanding of your problem):
http://jsoftware.com/svn/DanBron/trunk/uncategorized/scoreRoadRace.ijs
It didn't take very long to code, but unfortunately it would take much
longer to explain it in prose. Also, the style is very peculiar to me,
which means it's dense, and uses some sneaky tricks, so even veteran J
programmers may find it obscure (e.g. executing the first paragraph, which
defines 'RE', in reverse order).
I know your goal wasn't to solve this little race scoring puzzle, but to use
the puzzle as a means to learn J. So, if I get some more time over the week,
I may select and dissect pieces of the code.
In the meantime, it may be worth your while to play with it on your own. If
you want to do that, it'll probably be easiest and most fruitful to break it
into pieces and try them out, interactively, in the J session.
So I recommend you replace all the local assignments (=.) with global
assignments (=:), so all the intermediate names will be available to inspect
and play with individually*.
For example, I think you'll be interested in 'score' and 'catSumCount' (and
their respective dependencies), because these are the heart of the problem
and also highlight how J expresses such ideas differently, and perhaps more
elegantly, than other programming languages:
score 1+i. 3 NB. Fewer than 5 racers
0 0 0
score 1+i. 5 NB. 5 racers
3 2 1 0 0
score 1+i. 10 NB. 10 racers
3 2 1 0 0 0 0 0 0 0
score 1+i. 11 NB. 11 racers
7 5 4 3 2 1 0 0 0 0 0
pointGroup 1+i. 3 NB. Fewer than 5 racers
0
pointGroup 1+i. 5 NB. 5 racers
3 2 1
pointGroup 1+i. 10 NB. 10 racers
3 2 1
pointGroup 1+i. 11 NB. 11 racers
7 5 4 3 2 1
pointGroup
(0;3 2 1;7 5 4 3 2 1;8 6 5 4 3 2 1;10 8 7 6 5 4 3 2 1) {::~ 4 10 20
49 I. #
catSumCount 8 7 10 0 3 0 0 0 1 NB. 9 races, score=29, category=3
+-+--+-+
|3|29|9|
+-+--+-+
catSumCount 5 4 0 7 NB. 4 races, score=16, category=4
+-+--+-+
|4|16|4|
+-+--+-+
catSumCount 3000 NB. 1 race, score=super high, but still
category=4
+-+----+-+
|4|3000|1|
+-+----+-+
catSumCount
+/ (category ; ;) #
(+/ category #) 8 7 10 0 3 0 0 0 1
3
(+/ , #) 8 7 10 0 3 0 0 0 1
29 9
29 category 9 NB. x=sum of scores, y=# of races
3
35 category 7
1
35 category 2
1
33 category 5
2
category
4 <. 1 + 35 30 20 I. (* 2&<:)
It's worth noting the core parts of the scoring algorithm, 'pointGroup' and
'category', are both based on the dyad I. [1]. This is because those parts
of the scoring system are defined in terms of ranges (a race of 5 to 10
riders, a race of 11 to 20 riders vs. a total score from 0 to 20, a total
score from 21 to 30, etc).
The dyad I., interval index, is a sterling example of the expressivity and
economy of J's notation. Here, it allows us to define these ranges by their
essential characteristics, the demarcation lines between levels, rather than
the (repetitive, superfluous) contents of each level. Not only is this
convenient, but critically, it allows these essential characteristics stand
out in our code, rendering it at once briefer and clearer. The fundamentals
are immediately exposed.
For example, compare 'category' with the original 'cat' or 'pointGroup' with
the original 'points'. Heck, compare the code defining GROUPs to the table
in the official scoring site you linked! Sometimes, formalizing a problem
makes it even clearer than the specification. And, that little itch you
feel to start playing with these rules, now that they're exposed and
accessible?* That's what we mean when we say the notation is "suggestive".
I hope this gives you a flavor of the benefits, and joys, of J.
-Dan
* And because it's really easy to do, given J's short feedback loop and
interactive nature.
[1] Definition of the dyad I., "Interval Index"
http://www.jsoftware.com/help/dictionary/dicapdot.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm