661.md (5236B)
1 Source: https://codeforces.com/blog/entry/44292 2 3 All solutions use the same verb definitions to read and write data as the example: 4 5 ``` 6 print =: 1!:2&2 7 read =: 1!:1[3 8 ``` 9 10 Besides, the scripts have to end with verb exit ''. 11 12 #### [640A - Lazy Caterer Sequence](/contest/640/problem/A "VK Cup 2016 - Wild Card Round 1") 13 14 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 [".](http://code.jsoftware.com/wiki/Vocabulary/quotedot#dyadic) converts a string to a number or an array of numbers. 15 16 ``` 17 n =. ". (read-.LF)-.CR 18 print 1 + (n * (n + 1)) % 2 19 ``` 20 21 #### [640B - Seasons](/contest/640/problem/B "VK Cup 2016 - Wild Card Round 1") 22 23 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.](http://www.jsoftware.com/help/dictionary/csel.htm). 24 25 ``` 26 month =. (read-.LF)-.CR 27 28 season =: monad define 29 select. y 30 case. 'December';'January';'February' do. 'winter' 31 case. 'March';'April';'May' do. 'spring' 32 case. 'June';'July';'August' do. 'summer' 33 case. 'September';'October';'November' do. 'autumn' 34 end. 35 ) 36 37 print season month 38 ``` 39 40 #### [640C - Array Sum](/contest/640/problem/C "VK Cup 2016 - Wild Card Round 1") 41 42 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](http://code.jsoftware.com/wiki/Vocabulary/semidot1#negative) to split string into parts between characters LF (which is conveniently last character of the string). 43 44 ``` 45 inraw =. read -. CR 46 NB. we know that last character will be LF, so use _2 to use last character as delimiter and exclude it from intervals 47 print +/ ". ;._2 inraw 48 ``` 49 50 #### [640D - Maximal Difference](/contest/640/problem/D "VK Cup 2016 - Wild Card Round 1") 51 52 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 [}.](http://code.jsoftware.com/wiki/Vocabulary/curlyrtdot), array a2 is original array without the last element [}:](http://code.jsoftware.com/wiki/Vocabulary/curlyrtco), subtract these arrays from each other (i.e. create array of element-wise differences) and find maximal absolute value of elements of this array. 53 54 ``` 55 arr =. ". (read-.LF)-.CR 56 diffs =. (}. arr) - (}: arr) 57 print >./ | diffs 58 ``` 59 60 #### [640E - Divisibility Check](/contest/640/problem/E "VK Cup 2016 - Wild Card Round 1") 61 62 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 [>.](http://code.jsoftware.com/wiki/Vocabulary/gtdot#dyadic) 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. 63 64 ``` 65 arr =. ". (read-.LF)-.CR 66 print (*. / arr) = (>. / arr) 67 ``` 68 69 #### [640F - Primes in Interval](/contest/640/problem/F "VK Cup 2016 - Wild Card Round 1") 70 71 Verb [p:](http://code.jsoftware.com/wiki/Vocabulary/pco) returns prime number with the given index. Its inverse p:^:_1 returns the number of primes less than the given number. 72 73 ``` 74 in =. (read -.LF) -.CR 75 76 NB. split on space and convert to integers 77 ab =. ". ;._1 ' ',in 78 79 NB. first element is A, second is B - need to increment B to get upper bound inclusive 80 abinc =. ab + (0 1) 81 82 NB. apply inverse of primes 83 nprimes =. p:^:_1 abinc 84 85 NB. subtract first element (head) from second (tail) 86 print ({: nprimes) - ({. nprimes) 87 ``` 88 89 #### [640G - Hungarian Notation](/contest/640/problem/G "VK Cup 2016 - Wild Card Round 1") 90 91 In this problem one has to use any conditional construct to check whether variable value contains a decimal dot. 92 93 ``` 94 inraw =. read -. CR 95 a =. <;._2 inraw 96 97 name =. > {. a 98 content =. > {: a 99 100 capName =. (toupper {. name),(}. name) 101 102 typeChar =: monad define 103 if. (y +./@:E.~ '.') do. 'f' elseif. do. 'i' end. 104 ) 105 106 print (typeChar content),capName 107 ``` 108 109 #### [640H - Rotate Matrix](/contest/640/problem/H "VK Cup 2016 - Wild Card Round 1") 110 111 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 [|:](http://code.jsoftware.com/wiki/Vocabulary/barco)) and then reverse each row separately (глагол [|."1](http://code.jsoftware.com/wiki/Vocabulary/bardot#dyadic)). 112 113 ``` 114 inraw =. read -. CR 115 NB. first split on LF, then convert to numbers (splitting on space) 116 m =. ". ;._2 inraw 117 118 print |."1 |: m 119 ```