2005-12-29

J, seven-segment display

I've read about it on linux.org.ru


On http://www.pycontest.net/ occurs a competition.

The goal is to code the shortest possible python module that converts decimal numbers to the seven-segment display format.

I wonder, who will win. And here is my own code, written in J, which I wrote outside the competition.

seg=:7 3$' _ |_| | _||_ | |'
loc=:10 3$(7&#.^:_1)6115893582106107964921726x
seven_seq=:(,"2)@:((1 0 2)&|:)@:{&seg@:{&loc@:(10&#.^:_1)

I guess, it can be shorter in 10 symbols if do not care much about the readability.

For example, if to form a line at once instead of collecting results for individual numbers then the last verb can be rewritten as follows:


seven_seq=:,"2@:{&seg@:(|:@:]{&loc)@:(10&#.^:_1)

2005-12-20

But why?

I'm listening to the music all the day long — rarely I'm in the such mood.

Damn, it's uncomfortable. Why headphones are tend to be symmetric? To distinguish the left ear from the right one you should have a good look at a tiny label. They should make a little pimple at a dynamic. If you feel the pimple from the left then you got it right.

If I'll be in the same mood tomorrow, I'll stick a piece of tape.

2005-12-13

J Dirty hack

Thanks to dr_klm, who implicitly pointed to the unexpected feature:

Here is the expected behavior of the fork and operator @


load 'system/packages/misc/fndisplay.ijs'
setfnform 'Jv'
defverbs 'F G H'
defnouns 'y'

(F G@H ]) y
+---------------+
|G_ (F_ y) _H_ y|
+---------------+

If it were not for..

Exetuting:

(2&+@i.@# %@(+/\) ]) i.4
1 0.333333 0.2
0.333333 0.166667 0
0.166667 0 0
0 0 0
%((2&+@i.@# i.4) (+/\) (] i.4))
1 0.333333 0.2
0.333333 0.166667 _
0.166667 _ _
_ _ _

Obviously, records that supposed to be equivalent are really not, due to the presence of the implicit operation — filling with zeros.

I doubt whether I should enjoy this feature, or not. If there is no trouble in recognition of the operation in the code, it can even be a beautiful solution.

2005-12-12

Eclipse: Testing the multi-thread code.

A short snippet:

public void setUp() throws Exception {
finished = false;
}

public void testSomething() throws Exception{
new Thread(){
try{
doSomethingLongAndLogIt();
} finally { finished = true; }
}.start();

while(!finished && !Display.getCurrent().readAndDispatch()){
display.sleep();
}
}


There, it seems to be the full Eclipse emulation. Now one can be sure that inside doSomethingLongAndLogIt() method all communications with UI are going within Display thread.

Can't do without 'while' because the workbench might be already closed before it comes to UI calls.

2005-12-09

J The sum of ajacent numbers

Problem: Find the sum of adjacent twos, triples and so on in array. The result should be in ascending order.

After having dificulties and learned a lot of stuff, I've written:

diff=: 4 : 0
| (#x.) - #y.
)

summ=:(>:@:diff }. [) + (_1&}.@:])

allsums=: 3 : 0
x =. ''
m =. y.
while. #m ~: 0 do.
m=.y. summ m
x =. x,m
end.
/:~x
)


The usage:

summ^:2~ 2 _3 7 4 0 6 NB. Сумма смежных троек
6 8 11 10
allsums 2 _3 7 4 0 6 NB. Все возможные суммы
_1 4 4 6 6 8 8 10 10 10 11 11 14 16 17


It seems, I heard that it is possible to have your own participles in J. IMHO, to make the solution more beautiful, the verb 'allsums' should be transformed into a participle 'collect' so that one can use any other verb instead of 'summ'.

2005-12-08

J A small problem

While trying to solve this problem with J, I got a trouble. It will look elegantly if to write the implicit definition of the following operator H:

x H y
(x F y) G x


I do not get it...

The trivial explisit solution, which is not so elegant ;)

H =: 3 : 0
(F~ y.) G y.
:
(x. F y.) G x.
)


(09-12-2005), Update: Oops, I got it wrong...

H=:(f g [)

2005-12-05

Hello, J World

Just a week ago I was given a link to dr_klm's article «Programming language J, the introduction»... At the same time appeared a problem easy enough to be written in the newly learned language and, besides, seemed very suitable for J.

The problem.

N teams are playing «What? Where? When?» game. The game usually consists of 25 questions. Each team can give wrong or correct answer. It is required to summarize, that can be done in different ways.

The formal rules. The amount of correct answers is counted for each team. The maximum sum is set to the 100 points. Each team receives points in proportion to the number of given correct answers.

The informal version. We think, it is unfair that the question cost does not depend on its complexity. The more teams gives the right answer for a question, the less scores each of them should receive for it. The simplest case - if only one team has the right answer, then it receives one point. If all the teams answered correctly then the question cost is 0 points. So, it should be the linear approximation between these two points. Then points are added up and normalized at 100, as usual.

The Implementation.

First go I'd like to generate a matrix that best matches the final table of the tournament: rows — questions, columns — teams. The table is filled with ones and zeros depending on how the team answered to this question.

Not that soon, the following line was born:
L=: 2 (>?) 36 25$ 5


Now L is the result of a game, consisting of 25 questions and containing 36 teams. A team can give a correct answer with probability 2/5.

The official results are getting on-the-fly:

Add up:
res:=+/"1


Find the maximum and normalize:
norm=:*&100@%>./


Use:
norm res L


In the informal case the operators of the normalization and of the counting results remain the same, but they must act on the matrix where the question cost is no longer «1». In the case of the linear approximation described above, we have the following simple formula:



n-x
Cost= ------
n-1


where n is number or teams and x is amount of correct answers for the given question.

So:
fairq=:*"1.(#-+/)%(#-1:)


Try this:
norm res fairq L


.... Well, in the case of equal distribution the difference is very unnoticable :)


Finally

The program with the comments is no longer then the problem description itself. If to be honest, I was lazy to write it in Fortran-like language (C, Java, Php...). In the case of J the coding almost was not behind the flight of thougth. I had no time for boring :).

Ranks are really something! H'm... I'm curious, whether they can be beautifully realized in Lisp syntax?

It turned out that participles, forks and hooks have nothing in common with macroses. It is easy to understand by executing several times
#~?100

A couple will be always consist of equal numbers. That's why the only way to create a matrix, filled with random elements, is to use inner property of the operator itself?

Yet, I can fink of no beautiful operator generates a random matrix filled with zeros and ones. It would be something a-la
createL 39 25;<2 5

But the introduction of unpacking operator and operator of access to the list elements is complicating the initial simple formula
p (>?) x y $ q


Working in the Interpreter is very convinient in case of input pure functional definitions. But let's do so:
p=:2
powP=.^&2

After changing p you have to run the whole program from the start. Actually, compilable languages do so.

After typing a formula in the Interpreter it was not so easy to give it a name. In 'norm' and 'fairq' definitions there are «redundant» dots and 'at' signs (@), which are unnecessary when writing an operatior explicitly.

In general, the language is very cool. I finish reading «Easy J». Proceed to «J for C Programmers».