Skip to content

Commit d75293e

Browse files
committed
ch8 exercises
1 parent 36c8d29 commit d75293e

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

06-numbers-and-logic.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ repeats their indices by their values:
5656
What makes `&` interesting is that it can also repeat an index 0 times, thereby discarding it.
5757

5858
`&` can be used to filter elements based on a condition, like `{y@&x'y}`, which keep elements in `y` that satisfy predicate `x`.
59-
59+
+/
6060
If you want the index of the first element that holds true under a condition, you can use `*&`. But a simpler and more efficient alternative is `?` (find).
6161

6262
### `x?y` Find
@@ -115,5 +115,5 @@ Since these null values are not very easy to keep track of in an array, there ar
115115
## Exercises
116116
1. Given an array `x` and a 'mask' array `y` consisting of zeroes and ones only, get the elements in `x` that are at the same positions
117117
as the ones in `y`. `f[2 7 89 92 -123;0 1 0 1 1]` -> `7 92 -123`.
118-
2. Create a dictionary from an array where they keys are the types of the elements in the array, and the values are the elements of the array.
118+
2. Create a dictionary from an array where the keys are the types of the elements in the array, and the values are the elements of the array.
119119
3. Create a function with two arguments `x` and `y`, which takes the sum of `x[y]` without nulls.

08-dicts-table-strings.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# Dictionaries, Tables, Strings and other Miscellaneous functions
22

3-
Dictionaries and strings are not highly related, just that this chapter aims to show the rest of the verbs we have not covered in K, which happen to be mainly relating to dictionaries and strings. Think of it as a rapid-fire explanation of
3+
Dictionaries and strings are not highly related, just that this chapter aims to show the rest of the verbs we have not covered in K,
4+
which happen to be mainly relating to dictionaries and strings. Think of it as a rapid-fire explanation of their quirks.
45

56
## Dictionary Trivia
6-
Dictionaries are an *ordered collection*. this means that many primitives which work on arrays work similarly on dictionaries. This includes:
7+
Dictionaries are an *ordered collections*. This means that many primitives which work on arrays work similarly on dictionaries. This includes:
78

89
- concat `,` (values in `y` overwrite values in `x`)
910
- reverse `|` (reverses key array and value array separately)
1011
- map `'` (applies on values)
1112
- first `*` (first value from key-value pair only)
1213
- not `~`
14+
- at `@` preserves the structure of the dictionary when given as the second argument.
1315
- filter `#` and filter-out `_` (filter by value)
1416
- where `&` (keys are repeated by values)
1517
- grade up `<` and grade down `>` (sort keys by values)
@@ -120,6 +122,8 @@ Finally, `0N?x` where `x` is an array will shuffle `x`. If `x` is a number, then
120122
#### Base Decode and Encode (`\` and `/`)
121123

122124
ngn/k's base conversion adverbs are generalized: this means that you can do mixed-radix conversion and other cool things.
125+
These adverbs will be familiar to APL programmers. Remember that these are adverbs, and not verbs. Some things you do with them may not work exactly as
126+
expected.
123127

124128
The simplest use, and the most often use for this, is conversion to and from base 2:
125129

@@ -184,10 +188,17 @@ Monadic `.` when given a string is eval. This will evaluate any string as K code
184188
3
185189
```
186190

187-
Dyadic `:` is right. it returns its right argument. `:` has many special meanings , so it is better to call
191+
Dyadic `:` is right. it returns its right argument. `:` has many special meanings, so it is required to call
188192
`:` with an M-Expression:
189193

190194
```
191195
:[1 2; 3 4]
192196
3 4
193197
```
198+
199+
*no new vocabulary for this chapter.*
200+
201+
## Exercises
202+
1. Write a function to convert seconds into hours, minutes and seconds, using `\`.
203+
2. Write a function that takes a list of numbers `x` and a number `y`. Group the numbers in `x` into a dictionary based on whether they are multiples
204+
of `y`. `f[1 3 5 9;3]` -> `(0;1)!(1 5;3 9)`

code/08.k

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/ 1. Write a function to convert seconds into hours, minutes and seconds, using `\`.
2+
bs:24 60 60\ / mixed base conversion.
3+
4+
/ 2. Write a function that takes a list of numbers `x` and a number `y`. Group the numbers in `x` into a dictionary based on whether they are multiples
5+
/ of `y`. `f[1 3 5 9;3]` -> `(0;1)!(1 5;3 9)`
6+
dct:{x@~=y!x}
7+

0 commit comments

Comments
 (0)