Part 4
Nested Arrays and Array Transformations
-
Write a monadic function to retrieve the last element of a vector
Last 'hello' o Last ⍳7 7 Last (1 2 3)(4 5)(8)(42 43) ⍝ Disclosed result 42 43
-
Given
vec ← (1 2 3) (1 (2 3)) (1 2 3)
- What is the shape, rank and depth of
vec
? - Use a single multiplication (
×
) to get the following results:-
┌─────┬───────┬─────┐ │2 4 6│┌─┬───┐│2 4 6│ │ ││2│4 6││ │ │ │└─┴───┘│ │ └─────┴───────┴─────┘
-
┌─────┬───────┬─────┐ │2 4 6│┌─┬───┐│2 4 6│ │ ││1│2 3││ │ │ │└─┴───┘│ │ └─────┴───────┴─────┘
-
┌─────┬───────┬─────┐ │2 4 6│┌─┬───┐│1 2 3│ │ ││1│4 6││ │ │ │└─┴───┘│ │ └─────┴───────┴─────┘
-
- What is the shape, rank and depth of
-
Write a monadic function
Join
which joins a nested vector of character vectors⍵
into a single, non-nested character vector in which elements from⍵
are separated by the character scalar,
(comma).Join 'join' 'these' 'words' join,these,words
BONUS Can you write
Join
as a dyadic function which accepts a separator scalar as⍺
?' ' Join 'join' 'these' 'words' join these words '|' Join 'join' 'these' 'words' join|these|words
-
Given a right argument of a list of words (or possibly a single word), return a character matrix that is as wide as the longest word and one row per word, where each word is centered within the row. If there are an odd number of spaces to center within, leave the extra space on the right.
💡 Hint: The mix
↑Y
and rotateX⌽Y
functions will probably be useful here.Center 'APL' 'Problem' 'Solving' APL Problem Solving Center 'APL' 'Problem' 'Solving' 'Specialist' APL Problem Solving Specialist ⍴Center 0⍴⊂'' 0 0
Finding and Replacing Values
-
Write a function test if there are any vowels
'aeiou'
in text vector⍵
AnyVowels 'this text is made of characters' 1 AnyVowels 'bgxkz' 0
-
Write a function to count the number of vowels in its character vector argument
⍵
CountVowels 'this text is made of characters' 9 CountVowels 'we have twelve vowels in this sentence' 12
-
Write a function
Clean
that changes all non-digits into stars.Clean 'Easy as 1, 2 and 3' ********1**2*****3 Clean '1000' 1000 Clean 'APL works!' **********
-
Create a function
ReplaceHead
which returns its left argument vector⍺
, but with the first⍴⍵
elements replaced with the contents of⍵
.'apple' ReplaceHead 'Eat' Eatle 'apple' ReplaceHead 'rang' range 'apple' ReplaceHead 'ENTERPRISE' ENTER
-
Bus stops in a town are labelled A to E.
We can define a graph using a nested list of character arrays. For each position in our graph, we say which bus stops are reachable directly from this stop.
For example,
('BE' 'C' 'AE' 'BCE' 'A')
means that there is a bus that goes from stop A to stops B and E, from stop B to stop C, from C to A and E, etc.Define a function RouteMatrix which returns a Boolean matrix where
1
s indicate that buses go from the stop indicated by the column position to stops indicated by row positions.RouteMatrix 'BE' 'C' 'AE' 'BCE' 'A' 0 0 1 0 1 1 0 0 1 0 0 1 0 1 0 0 0 0 0 0 1 0 1 1 0 ' ABCDE','ABCDE'⍪RouteMatrix 'C' 'CDE' 'ABDE' 'E' 'B' A B C D E A 0 0 1 0 0 B 0 0 1 0 1 C 1 1 0 0 0 D 0 1 1 0 0 E 0 1 1 1 0
-
These are the heights of some students in 3 classes.
student ← 10 7⍴'Kane Jonah JessicaPadma Katie CharlieAmil David Zara Filipa ' class ← 'CBACCCBBAB' height ← 167 177 171 176 178 164 177 177 173 160
Use APL to:
- Find the height of the tallest student
- Find the name of the tallest student
- Find the class to which the tallest student belongs
- Find the average height of students in class
B
-
Anna, Ben and Charlie are having a competition. They want to see who can eat the most fruit in a week.
fruits ← 4 7⍴'Apples MangoesOrangesBananas' days ← 7 3⍴'SunMonTueWedThuFriSat' names ← 3 7⍴'Anna Ben Charlie' ⎕RL ← 42 1 ⋄ ate ← ?3 4 7⍴3
What is
⎕RL
?The roll function
?⍵
generates random numbers for each simple scalar number in⍵
.Setting the Random Link system variable
⎕RL
lets us generate the same random numbers repeatedly.- Compute the names of the people who ate the most fruit on Tuesday and Sunday combined.
- Compute the name of the person who ate the most mangoes and bananas combined.
- What is the name of the person who ate the most fruit overall?