### Nickolas's blog

By Nickolas, 3 years ago, translation, ,

All solutions use the same verb definitions to read and write data as the example:

print =: 1!:2&2


Besides, the scripts have to end with verb exit ''.

#### 640A - Lazy Caterer Sequence

As usual, the first problem tests competitor's ability to do basic arithmetics. Even in a language so unusual as J arithmetic verbs look pretty standard. Well, except for the fact that in absence of brackets operations are performed right-to-left, without any operator precedence, and division is denoted with %. Verb ". converts a string to a number or an array of numbers.

n =. ". (read-.LF)-.CR
print 1 + (n * (n + 1)) % 2


#### 640B - Seasons

This problem hints the competitors that J also has common imperative constructs for loops and conditional execution. In this case it's convenient to use select. case. do..

month =. (read-.LF)-.CR

select. y
case. 'December';'January';'February' do. 'winter'
case. 'March';'April';'May' do. 'spring'
case. 'June';'July';'August' do. 'summer'
case. 'September';'October';'November' do. 'autumn'
end.
)

print season month


#### 640C - Array Sum

A+B program from the example can actually calculate sum of arbitrary quantity of numbers, as long as all of them are written in a single line. J reads the input file as a whole, so the contents of different lines is separated with characters CR (#13) and LF (#10). To find sum of numbers written in different lines, one has to split this string into an array of numbers. A convenient way to do this is to remove CR characters after reading, and then use verb ;._2 to split string into parts between characters LF (which is conveniently last character of the string).

inraw =. read -. CR
NB. we know that last character will be LF, so use _2 to use last character as delimiter and exclude it from intervals
print +/ ". ;._2 inraw


#### 640D - Maximal Difference

The beauty of J language is that all operations apply to arrays. In this problem the answer can be represented as follows: array a1 is original array without the first element }., array a2 is original array without the last element }:, subtract these arrays from each other (i.e. create array of element-wise differences) and find maximal absolute value of elements of this array.

arr =. ". (read-.LF)-.CR
diffs =. (}. arr) - (}: arr)
print >./ | diffs


#### 640E - Divisibility Check

If such a number exists, it equals least common multiple of all numbers of the array (it has to be divisible not only by other numbers but also by itself). Besides, it will be the greatest of the numbers of the array. Verbs [*.](http://code.jsoftware.com/wiki/Vocabulary/stardot#dyadic) and >. allow to find LCM and maximum of the array, all that is left is to compare them. Comparison result will be 1 if these values are equal, and 0 otherwise.

arr =. ". (read-.LF)-.CR
print (*. / arr) = (>. / arr)


#### 640F - Primes in Interval

Verb p: returns prime number with the given index. Its inverse p:^:_1 returns the number of primes less than the given number.

in =. (read -.LF) -.CR

NB. split on space and convert to integers
ab =. ". ;._1 ' ',in

NB. first element is A, second is B - need to increment B to get upper bound inclusive
abinc =. ab + (0 1)

NB. apply inverse of primes
nprimes =. p:^:_1 abinc

NB. subtract first element (head) from second (tail)
print ({: nprimes) - ({. nprimes)


#### 640G - Hungarian Notation

In this problem one has to use any conditional construct to check whether variable value contains a decimal dot.

inraw =. read -. CR
a =. <;._2 inraw

name =. > {. a
content =. > {: a

capName =. (toupper {. name),(}. name)

if. (y +./@:E.~ '.') do. 'f' elseif. do. 'i' end.
)

print (typeChar content),capName


#### 640H - Rotate Matrix

This problem shows the power of J in matrix processing — J solution is much shorter than C++ one :-) To rotate a matrix 90 degrees clockwise, one can transpose it (verb |:) and then reverse each row separately (глагол |."1).

inraw =. read -. CR
NB. first split on LF, then convert to numbers (splitting on space)
m =. ". ;._2 inraw

print |."1 |: m


•
• +56
•

 » 3 years ago, # |   +79 Now perl seems to be very readable language :)
 » 3 years ago, # |   +4 In C, I couldn't figure out how to take multiline input, so I used regexp to replace all newlines with spaces and then split the string to form a array. :D in =. ('\r';' ') rxrplc (read-.LF) print +/ ". in