# Write exercises

Submit APL code for the following exercises. Your submissions are graded using TMC.

Make sure to log in to be able to submit your work!

Remember, you can test out ideas and develop you solution at TryAPL before submitting it here.

You can submit as many solutions as you like. If you submit a correct solution at least once, you will receive points on the TMC server and be able to see the model solution.

If the write problem is red, it is unsolved. If it it green, you have solved it and received points for it on the server.

Create vector Manipulate vector Index vector Index vector, multiple

Create matrix Manipulate matrix Index matrix (1) Index matrix (2) Index matrix, square

Write problem 1

Create a vector consisting of the first ten prime numbers called PRIMES

Write problem 2

Create a vector called POWER of value 2 to the power of PRIMES, minus two

Write problem 3

Divide the vector POWER by PRIMES

Write problem 4

Create the word "BUNNY" by indexing the alphabet vector ⎕A

Write problem 5

The following matrix CIPHER consists of shifted versions of the alphabet for every row.

```
CIPHER
ABCDEFGHIJKLMNOPQRSTUVWXYZ
BCDEFGHIJKLMNOPQRSTUVWXYZA
CDEFGHIJKLMNOPQRSTUVWXYZAB
DEFGHIJKLMNOPQRSTUVWXYZABC
EFGHIJKLMNOPQRSTUVWXYZABCD
FGHIJKLMNOPQRSTUVWXYZABCDE
GHIJKLMNOPQRSTUVWXYZABCDEF
HIJKLMNOPQRSTUVWXYZABCDEFG
IJKLMNOPQRSTUVWXYZABCDEFGH
JKLMNOPQRSTUVWXYZABCDEFGHI
KLMNOPQRSTUVWXYZABCDEFGHIJ
LMNOPQRSTUVWXYZABCDEFGHIJK
MNOPQRSTUVWXYZABCDEFGHIJKL
NOPQRSTUVWXYZABCDEFGHIJKLM
OPQRSTUVWXYZABCDEFGHIJKLMN
PQRSTUVWXYZABCDEFGHIJKLMNO
QRSTUVWXYZABCDEFGHIJKLMNOP
RSTUVWXYZABCDEFGHIJKLMNOPQ
STUVWXYZABCDEFGHIJKLMNOPQR
TUVWXYZABCDEFGHIJKLMNOPQRS
UVWXYZABCDEFGHIJKLMNOPQRST
VWXYZABCDEFGHIJKLMNOPQRSTU
WXYZABCDEFGHIJKLMNOPQRSTUV
XYZABCDEFGHIJKLMNOPQRSTUVW
YZABCDEFGHIJKLMNOPQRSTUVWX
ZABCDEFGHIJKLMNOPQRSTUVWXY
```

Write problem 6

Consider the following ASCII art stored in a matrix called ART

```
APL
._________________.
|.---------------.|
|| __ ____ __ ||
|| / _\( _ ( ) ||
||/ \) __/ (_/||
||\_/\_(__) \____||
||_______________||
/.-.-.-.-.-.-.-.-.\
/.-.-.-.-.-.-.-.-.-.\
/.-.-.-.-.-.-.-.-.-.-.\
/______/__________\_____\
\_______________________/
```

```
__ ____ __
/ _\( _ ( )
/ \) __/ (_/
\_/\_(__) \____
```

Write problem 7

Create a 3 by 3 grid of 'X's and 'O's called BOARD

Write problem 8

Considering the above array as a game of noughts and crosses, change a single element to make the O player win.

Write problem 9

Given the coefficients list C of a quadratic $P(x) = C[1] x^2 + C[2] x + C[3]$, evaluate the polynomial for a value X. For example, for C ← 1 2 3, and X ← 10, this should evaluate to $ 1 \cdot 10^2 + 2 \cdot 10 + 3 = 123 $.

Write problem 10

Consider the following list of GAMES

```
GAMES
CHESS
CHECKERS
BACKGAMMON
POKER
GLOBAL THERMONUCLEAR WAR
```

Write problem 11

Get the first three letters of the second and fifth game listed above using indexing

Write problem 12

Consider a tape-controlled computer which operates by reading a certain section of a tape, changing its internal state based on the value of that section, writing a certain value to that section, then possibly moving the tape along to the next or previous section. For this specific machine, the value of the tape sections is either a 1 or a 2, the internal state of the machine is either 1 2 or 3, and the matrix which specifies the actions to be taken is the following

```
INSTRUCTIONS
┌─────┬─────┬─────┐
│1 R 2│1 L 1│1 L 2│
├─────┼─────┼─────┤
│1 L 3│1 R 2│1 N H│
└─────┴─────┴─────┘
```

The first (second) row of the matrix specifies the possible instructions when the value of the section of tape is a 1 (2). The columns similarly specify the possible instructions depending on the state of the machine.

Using indexing, find the instruction for the tape section having value 1 when the machine is in state 3.