Skip to content

Commit a07eac3

Browse files
add practice exercises (#228)
- diamond - flower-field - grade-school - intergalactic-transmission - piecing-it-together - roman-numerals - series
1 parent 37ac3a0 commit a07eac3

64 files changed

Lines changed: 2170 additions & 18 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

concepts/assocs/introduction.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ set-at ( value key assoc -- )
2020
delete-at ( key assoc -- )
2121
inc-at ( key assoc -- )
2222
change-at ( key assoc quot -- )
23+
keys ( assoc -- keys )
24+
values ( assoc -- values )
2325
>alist ( assoc -- alist )
2426
```
2527

config.json

Lines changed: 114 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,18 +1011,6 @@
10111011
],
10121012
"difficulty": 3
10131013
},
1014-
{
1015-
"slug": "robot-name",
1016-
"name": "Robot Name",
1017-
"uuid": "1f3cded8-c357-448d-9640-63e8e6ea8efe",
1018-
"practices": [],
1019-
"prerequisites": [
1020-
"dynamic-variables",
1021-
"randomness",
1022-
"hash-sets"
1023-
],
1024-
"difficulty": 3
1025-
},
10261014
{
10271015
"slug": "rotational-cipher",
10281016
"name": "Rotational Cipher",
@@ -1053,6 +1041,20 @@
10531041
],
10541042
"difficulty": 3
10551043
},
1044+
{
1045+
"slug": "series",
1046+
"name": "Series",
1047+
"uuid": "3bb91e4a-d234-40e9-b56c-fb06a9d84985",
1048+
"practices": [],
1049+
"prerequisites": [
1050+
"strings",
1051+
"sequences",
1052+
"combinators",
1053+
"locals",
1054+
"errors"
1055+
],
1056+
"difficulty": 3
1057+
},
10561058
{
10571059
"slug": "sum-of-multiples",
10581060
"name": "Sum of Multiples",
@@ -1147,6 +1149,19 @@
11471149
],
11481150
"difficulty": 4
11491151
},
1152+
{
1153+
"slug": "diamond",
1154+
"name": "Diamond",
1155+
"uuid": "3da8114d-a1d4-4b45-9cb6-30bbef6624d3",
1156+
"practices": [],
1157+
"prerequisites": [
1158+
"strings",
1159+
"tabulation",
1160+
"higher-order-sequences",
1161+
"locals"
1162+
],
1163+
"difficulty": 4
1164+
},
11501165
{
11511166
"slug": "flatten-array",
11521167
"name": "Flatten Array",
@@ -1162,6 +1177,21 @@
11621177
],
11631178
"difficulty": 4
11641179
},
1180+
{
1181+
"slug": "grade-school",
1182+
"name": "Grade School",
1183+
"uuid": "fb1fe9a5-d2ce-4e44-850f-7c0518e0422c",
1184+
"practices": [],
1185+
"prerequisites": [
1186+
"tuples",
1187+
"mutation",
1188+
"assocs",
1189+
"higher-order-sequences",
1190+
"curry-compose-fry",
1191+
"locals"
1192+
],
1193+
"difficulty": 4
1194+
},
11651195
{
11661196
"slug": "kindergarten-garden",
11671197
"name": "Kindergarten Garden",
@@ -1273,6 +1303,20 @@
12731303
],
12741304
"difficulty": 4
12751305
},
1306+
{
1307+
"slug": "roman-numerals",
1308+
"name": "Roman Numerals",
1309+
"uuid": "31e438b9-e2c9-4d35-9b3f-dc8a97dcb787",
1310+
"practices": [],
1311+
"prerequisites": [
1312+
"numbers",
1313+
"tabulation",
1314+
"higher-order-sequences",
1315+
"arrays",
1316+
"locals"
1317+
],
1318+
"difficulty": 4
1319+
},
12761320
{
12771321
"slug": "sieve",
12781322
"name": "Sieve",
@@ -1498,6 +1542,18 @@
14981542
],
14991543
"difficulty": 5
15001544
},
1545+
{
1546+
"slug": "robot-name",
1547+
"name": "Robot Name",
1548+
"uuid": "1f3cded8-c357-448d-9640-63e8e6ea8efe",
1549+
"practices": [],
1550+
"prerequisites": [
1551+
"dynamic-variables",
1552+
"randomness",
1553+
"hash-sets"
1554+
],
1555+
"difficulty": 5
1556+
},
15011557
{
15021558
"slug": "run-length-encoding",
15031559
"name": "Run-Length Encoding",
@@ -1629,6 +1685,19 @@
16291685
],
16301686
"difficulty": 6
16311687
},
1688+
{
1689+
"slug": "flower-field",
1690+
"name": "Flower Field",
1691+
"uuid": "d635beb6-bc14-4846-909c-b6216c3f4d41",
1692+
"practices": [],
1693+
"prerequisites": [
1694+
"strings",
1695+
"tabulation",
1696+
"higher-order-sequences",
1697+
"locals"
1698+
],
1699+
"difficulty": 6
1700+
},
16321701
{
16331702
"slug": "food-chain",
16341703
"name": "Food Chain",
@@ -1643,6 +1712,23 @@
16431712
],
16441713
"difficulty": 6
16451714
},
1715+
{
1716+
"slug": "intergalactic-transmission",
1717+
"name": "Intergalactic Transmission",
1718+
"uuid": "e2ce391c-7d7e-4442-aea6-71f310206ace",
1719+
"practices": [],
1720+
"prerequisites": [
1721+
"bitwise-operations",
1722+
"arrays",
1723+
"windows",
1724+
"reductions",
1725+
"higher-order-sequences",
1726+
"combinators",
1727+
"strings",
1728+
"errors"
1729+
],
1730+
"difficulty": 6
1731+
},
16461732
{
16471733
"slug": "ocr-numbers",
16481734
"name": "OCR Numbers",
@@ -1672,6 +1758,22 @@
16721758
],
16731759
"difficulty": 6
16741760
},
1761+
{
1762+
"slug": "piecing-it-together",
1763+
"name": "Piecing It Together",
1764+
"uuid": "46be0bec-ed8e-4d32-9640-556acdbb5221",
1765+
"practices": [],
1766+
"prerequisites": [
1767+
"assocs",
1768+
"combinators",
1769+
"arrays",
1770+
"higher-order-sequences",
1771+
"booleans",
1772+
"locals",
1773+
"errors"
1774+
],
1775+
"difficulty": 6
1776+
},
16751777
{
16761778
"slug": "rail-fence-cipher",
16771779
"name": "Rail Fence Cipher",

exercises/concept/boardwalk-games/.docs/introduction.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ USING: random random.mersenne-twister ;
100100

101101
Everything inside the quotation — `random`, `randomize`, `sample`
102102
draws from the seeded generator, so the same seed always reproduces the
103-
same outcome. Marking a word that wraps `with-random` as `inline` lets
104-
the quotation return values to the caller.
103+
same outcome. A word that wraps `with-random` should be marked `inline`
104+
so the quotation's result can flow back to the caller.
105105

106106
[random]: https://docs.factorcode.org/content/vocab-random.html
107107
[mt]: https://docs.factorcode.org/content/vocab-random.mersenne-twister.html

exercises/concept/pursers-pantry/.docs/introduction.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,27 @@ that on every iteration `each` only needs to supply the key.
9393
`keep` runs the quotation while preserving the hashtable for the
9494
final `.`.
9595

96-
## Converting back to a sequence
96+
## Keys, values, and pairs
9797

98-
`>alist` (in [`assocs`][assocs]) returns a sequence of `{ key value }`
99-
pairs. `sort-keys` (in [`sorting`][sorting]) returns the same
100-
sequence sorted by key.
98+
`keys` and `values` (in [`assocs`][assocs]) return just the keys or
99+
just the values; `>alist` returns the `{ key value }` pairs.
100+
101+
```
102+
keys ( assoc -- keys )
103+
values ( assoc -- values )
104+
>alist ( assoc -- alist )
105+
```
106+
107+
```factor
108+
H{ { "wood" 11 } { "coal" 7 } } keys . ! the keys (order not guaranteed)
109+
H{ { "wood" 11 } { "coal" 7 } } values . ! the matching values
110+
```
111+
112+
`keys` and `values` line up: the value at a given position belongs to
113+
the key at the same position.
114+
115+
`sort-keys` (in [`sorting`][sorting]) returns the `{ key value }` pairs
116+
sorted by key:
101117

102118
```factor
103119
H{ { "wood" 11 } { "coal" 7 } } sort-keys .
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Instructions
2+
3+
The diamond kata takes as its input a letter, and outputs it in a diamond shape.
4+
Given a letter, it prints a diamond starting with 'A', with the supplied letter at the widest point.
5+
6+
## Requirements
7+
8+
- The first row contains one 'A'.
9+
- The last row contains one 'A'.
10+
- All rows, except the first and last, have exactly two identical letters.
11+
- All rows have as many trailing spaces as leading spaces. (This might be 0).
12+
- The diamond is horizontally symmetric.
13+
- The diamond is vertically symmetric.
14+
- The diamond has a square shape (width equals height).
15+
- The letters form a diamond shape.
16+
- The top half has the letters in ascending order.
17+
- The bottom half has the letters in descending order.
18+
- The four corners (containing the spaces) are triangles.
19+
20+
## Examples
21+
22+
In the following examples, spaces are indicated by `·` characters.
23+
24+
Diamond for letter 'A':
25+
26+
```text
27+
A
28+
```
29+
30+
Diamond for letter 'C':
31+
32+
```text
33+
··A··
34+
·B·B·
35+
C···C
36+
·B·B·
37+
··A··
38+
```
39+
40+
Diamond for letter 'E':
41+
42+
```text
43+
····A····
44+
···B·B···
45+
··C···C··
46+
·D·····D·
47+
E·······E
48+
·D·····D·
49+
··C···C··
50+
···B·B···
51+
····A····
52+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"diamond/diamond.factor"
8+
],
9+
"test": [
10+
"diamond/diamond-tests.factor"
11+
],
12+
"example": [
13+
".meta/example.factor"
14+
]
15+
},
16+
"blurb": "Given a letter, print a diamond starting with 'A' with the supplied letter at the widest point.",
17+
"source": "Seb Rose",
18+
"source_url": "https://web.archive.org/web/20220807163751/http://claysnow.co.uk/recycling-tests-in-tdd/"
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
USING: kernel math sequences strings ;
2+
IN: diamond
3+
4+
<PRIVATE
5+
6+
:: diamond-row ( n i -- str )
7+
i CHAR: A + :> ch
8+
i zero?
9+
[ ch 1string ]
10+
[ ch 1string i 2 * CHAR: space pad-tail ch suffix ] if
11+
n i + 1 + CHAR: space pad-head
12+
n 2 * 1 + CHAR: space pad-tail ;
13+
14+
PRIVATE>
15+
16+
:: rows ( letter -- rows )
17+
letter CHAR: A - :> n
18+
n 1 + <iota> [ n swap diamond-row ] map
19+
dup but-last reverse append ;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Diamond
2+
3+
function gen_test_case(case)
4+
letter = case["input"]["letter"]
5+
expected = format_string_array(case["expected"])
6+
return "{ $(expected) }\n[ CHAR: $(letter) rows ] unit-test"
7+
end
8+
9+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[202fb4cc-6a38-4883-9193-a29d5cb92076]
13+
description = "Degenerate case with a single 'A' row"
14+
15+
[bd6a6d78-9302-42e9-8f60-ac1461e9abae]
16+
description = "Degenerate case with no row containing 3 distinct groups of spaces"
17+
18+
[af8efb49-14ed-447f-8944-4cc59ce3fd76]
19+
description = "Smallest non-degenerate case with odd diamond side length"
20+
21+
[e0c19a95-9888-4d05-86a0-fa81b9e70d1d]
22+
description = "Smallest non-degenerate case with even diamond side length"
23+
24+
[82ea9aa9-4c0e-442a-b07e-40204e925944]
25+
description = "Largest possible diamond"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
USING: diamond exercism-tools io kernel tools.test unicode ;
2+
IN: diamond.tests
3+
4+
"Degenerate case with a single 'A' row" description
5+
{ { "A" } }
6+
[ CHAR: A rows ] unit-test
7+
8+
STOP-HERE
9+
10+
"Degenerate case with no row containing 3 distinct groups of spaces" description
11+
{ { " A " "B B" " A " } }
12+
[ CHAR: B rows ] unit-test
13+
14+
"Smallest non-degenerate case with odd diamond side length" description
15+
{ { " A " " B B " "C C" " B B " " A " } }
16+
[ CHAR: C rows ] unit-test
17+
18+
"Smallest non-degenerate case with even diamond side length" description
19+
{ { " A " " B B " " C C " "D D" " C C " " B B " " A " } }
20+
[ CHAR: D rows ] unit-test
21+
22+
"Largest possible diamond" description
23+
{ { " A " " B B " " C C " " D D " " E E " " F F " " G G " " H H " " I I " " J J " " K K " " L L " " M M " " N N " " O O " " P P " " Q Q " " R R " " S S " " T T " " U U " " V V " " W W " " X X " " Y Y " "Z Z" " Y Y " " X X " " W W " " V V " " U U " " T T " " S S " " R R " " Q Q " " P P " " O O " " N N " " M M " " L L " " K K " " J J " " I I " " H H " " G G " " F F " " E E " " D D " " C C " " B B " " A " } }
24+
[ CHAR: Z rows ] unit-test

0 commit comments

Comments
 (0)