Vocabulary/TestYourJ

From J Wiki
Jump to navigation Jump to search

Back to: Vocabulary

Test Your J

You may refer freely to any J reference material, but not to a running J session.

If you get less than half right, you need NuVoc, The Accessible Dictionary. Check it out! You will see clear answers to all these actual questions.

If you get at least half right, NuVoc needs you! Make it better by adding what you know.

1. Suppose P is a tacit verb that calculates nPr, the number of permutations of n things taken r at a time:

   5 P 3
60
   7 P 5
2520

Write P using a single application of a single J primitive verb.

2. What is the result?

   _3 q: 2*3*5*17

3. What are the results, and what is displayed, in the following sequence?

   u =: smoutput M.
   u 'Line 1'
   u '2'
   u '2'
   u '3'

4. What is the result?

   ]n =: <^:1 2 3 (0 1 2)
+-----+-------+---------+
|0 1 2|+-----+|+-------+|
|     ||0 1 2|||+-----+||
|     |+-----+|||0 1 2|||
|     |       ||+-----+||
|     |       |+-------+|
+-----+-------+---------+
   |.L:1 n

5. What is the result?

   ]m =: (; 2&+L:0)^:4 (0 1)
+---------------------------------------+-------------------+---------+---+---+
|+-------------------+---------+---+---+|+---------+---+---+|+---+---+|6 7|8 9|
||+---------+---+---+|+---+---+|4 5|6 7|||+---+---+|4 5|6 7|||4 5|6 7||   |   |
|||+---+---+|2 3|4 5|||2 3|4 5||   |   ||||2 3|4 5||   |   ||+---+---+|   |   |
||||0 1|2 3||   |   ||+---+---+|   |   |||+---+---+|   |   ||         |   |   |
|||+---+---+|   |   ||         |   |   ||+---------+---+---+|         |   |   |
||+---------+---+---+|         |   |   ||                   |         |   |   |
|+-------------------+---------+---+---+|                   |         |   |   |
+---------------------------------------+-------------------+---------+---+---+
   |.L:_2 m

6. The phrase 0 { y can be used to index into an atom:

   0 { 5
5

but 0 {:: y fails:

   0 {:: 5
|length error
  • What value of x could you use in x {:: <5 to select the single atom 5?
  • What value of x could you use in either x { 5 or x {:: <5 to select the single atom 5?

7. Using either value of x you found for problem 6, what is the result of each of the following?

   x {:: <5
   x {:: ,<5
   x {:: 5;6

8. What J primitive verb (call it v) is defined so that v does not always give the same result as v"v?

9. What is the result?

   ; 4;(i. 3 2);i. 2 4

10. What is the result of each of the following?

   $ '' ,: 0
   {. '' ,: 0
   {. {. '' ,: 0

11. What is the result of each of the following?

   {. '' , 0$0
   {. '' , 0$4

12. What is the result?

   5 0 _5 #: 23

13. How can the result of x {. y be an atom?

14. Show by an example x that (C. x) C. 0 1 2 3 4 5 is not always the same as x C. 0 1 2 3 4 5.

15. At what place(s), if any, in the character list a below will inserting a NUL character (0{a.) change the display of a?

   a =: 'It ran in 5',(8 u: u: 16bb5),'sec.',CR,LF,'Pretty fast!'
   a
It ran in 5µsec.
Pretty fast!

16. What is the result of each of the following?

   (1 j. 1e_20) > 1
   (1 j. 1e_20) >!.0 (1)

17. What is the result?

   i. 3
0 1 2
   (<<<3) { i. 3

18. What is the result?

   ]a =. i. 4 4
 0  1  2  3
 4  5  6  7
 8  9 10 11
12 13 14 15
   (<(i. 2 2);0) { a

19. Verb M is a short tacit verb.

   a =: 1000000 ?@$ 0  NB. One million uniformly distributed random variates
   M a
0.499146
   M a
0.499146
   M a
0.499146

M, as you can see, produces the same value every time it is called; that is, it has no side effects. Yet:

   7!:2 'M a'
4195776
   7!:2 'M a'
4195776
   7!:2 'M a'
8390080
   7!:2 'M a'
4195776
   7!:2 'M a'
8390080

7!:2 gives the space required to execute a sentence. The space required by M a is not constant; in fact it assumes a number of discrete values, some of which are smaller than the size of a (as 4195776 is here).

M contains no foreigns or instances of the ? or ?. verbs.

a. What is M calculating?

a. What is verb M?

20. Defining the verb substr which extracts substrings given as (startingpoint,length):

   substr =: 4 : '(,. x) ];.0 y'
   2 2 substr 'abcde'
cd

what are the results?

   2 6 substr 'abcde'
   5 6 substr 'abcde'
   5 0 substr 'abcde'
   6 0 substr 'abcde'

Extra credit question: find a nonempty list L such that /:~ L is not the same as /:~&.:(<"0) L.