Skip to content

Commit ddc93bf

Browse files
reference: update to mkdocs examples
1 parent 0a90a27 commit ddc93bf

File tree

7 files changed

+280
-284
lines changed

7 files changed

+280
-284
lines changed

docs/designing-data-structures/modeling-alphabet-codes.md

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,42 @@ Maps in Clojure are used to model key and value pairs.
77

88
Vectors in Clojure are a general data structure that are good for handing any kind of information.
99

10-
> **Note** Define a data structure where each letter of the alphabet is represented by a 6 digit binary code
11-
12-
<!--sec data-title="Reveal answer" data-id="answer001" data-collapse=true ces-->
13-
14-
Lets define a name called `alphabet` that is bound to a map. Each key in the map is a character of the alphabet and each value is a vector of numbers that represent a binary code.
15-
16-
The map also includes a binary code for a full stop and space character
17-
18-
```clojure
19-
(def alphabet {"A" [0 1 0 0 0 1]
20-
"B" [0 0 1 0 1 0]
21-
"C" [0 1 0 0 1 0]
22-
"D" [1 0 1 0 0 0]
23-
"E" [1 0 1 1 0 0]
24-
"F" [1 1 0 1 0 0]
25-
"G" [1 0 0 1 1 0]
26-
"H" [1 0 1 0 0 1]
27-
"I" [1 1 1 0 0 0]
28-
"J" [0 0 1 1 1 1]
29-
"K" [0 1 0 1 0 1]
30-
"L" [1 1 1 0 0 1]
31-
"M" [1 1 1 0 1 1]
32-
"N" [0 1 1 1 0 1]
33-
"O" [1 1 0 1 1 0]
34-
"P" [1 1 1 1 1 0]
35-
"Q" [1 0 1 1 1 0]
36-
"R" [1 1 1 1 0 0]
37-
"S" [0 1 1 1 1 0]
38-
"T" [1 0 0 1 1 1]
39-
"U" [0 0 1 0 1 1]
40-
"V" [0 1 1 0 0 1]
41-
"W" [1 1 0 1 0 1]
42-
"X" [1 0 1 0 1 0]
43-
"Y" [1 0 0 0 1 1]
44-
"Z" [1 1 0 0 1 1]
45-
"." [1 0 1 1 0 1]
46-
" " [0 0 1 0 0 0]})
47-
48-
```
49-
50-
<!--endsec-->
10+
!!! NOTE "Name a data structure"
11+
Define a name for a data structure where each letter of the alphabet is represented by a 6 digit binary code
12+
13+
14+
??? EXAMPLE "Example solution"
15+
Define a name called `alphabet` that is bound to a map. Each key in the map is a character of the alphabet and each value is a vector of numbers that represent a binary code.
16+
17+
The map includes a binary code for a full stop and space character, to help create sentences.
18+
19+
```clojure
20+
(def alphabet {"A" [0 1 0 0 0 1]
21+
"B" [0 0 1 0 1 0]
22+
"C" [0 1 0 0 1 0]
23+
"D" [1 0 1 0 0 0]
24+
"E" [1 0 1 1 0 0]
25+
"F" [1 1 0 1 0 0]
26+
"G" [1 0 0 1 1 0]
27+
"H" [1 0 1 0 0 1]
28+
"I" [1 1 1 0 0 0]
29+
"J" [0 0 1 1 1 1]
30+
"K" [0 1 0 1 0 1]
31+
"L" [1 1 1 0 0 1]
32+
"M" [1 1 1 0 1 1]
33+
"N" [0 1 1 1 0 1]
34+
"O" [1 1 0 1 1 0]
35+
"P" [1 1 1 1 1 0]
36+
"Q" [1 0 1 1 1 0]
37+
"R" [1 1 1 1 0 0]
38+
"S" [0 1 1 1 1 0]
39+
"T" [1 0 0 1 1 1]
40+
"U" [0 0 1 0 1 1]
41+
"V" [0 1 1 0 0 1]
42+
"W" [1 1 0 1 0 1]
43+
"X" [1 0 1 0 1 0]
44+
"Y" [1 0 0 0 1 1]
45+
"Z" [1 1 0 0 1 1]
46+
"." [1 0 1 1 0 1]
47+
" " [0 0 1 0 0 0]})
48+
```
Lines changed: 72 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,50 @@
11
# Design a map for name generation
22

3-
Imagine you are writing a simple name generator that takes your name and creates an alternative version. For example this could be a generator of your "Posh" or "Hipster" name.
3+
Imagine you are writing a simple celebrity name generator that takes your name and creates a silly version.
44

5-
> **Note** Define a data structure to model sloane names that has three names for every letter of the alphabet. For name suggestions, see the [Tattler sloane name generator](http://www.tatler.com/news/articles/july-2015/sloane-name-generator).
5+
!!! NOTE ""
6+
Define a data structure to model celebrity names, containing 3 or more first, second and third names, that has three names for every letter of the alphabet.
67

7-
<!--sec data-title="Reveal answer" data-id="answer001" data-collapse=true ces-->
8+
??? EXAMPLE "Suggested Example"
9+
```clojure
10+
(def celebrity-first-name
11+
{"a" "Ally-Pally"
12+
"b" "Bongo"
13+
"c" "Chipper"})
814

9-
The following seems to be the simplest way to model the sloane names. This follows the representation in the original source material.
15+
(def celebrity-second-name
16+
{"a" "Anstruther"
17+
"b" "Beaufort"
18+
"c" "Cholmondeley"})
1019

11-
```clojure
12-
(def sloane-first-name
13-
{"a" "Ally-Pally"
14-
"b" "Bongo"
15-
"c" "Chipper"})
20+
(def celebrity-third-name
21+
{"a" "Arbuthnot"
22+
"b" "Battenburg"
23+
"c" "Coutts"})
24+
```
1625

17-
(def slone-second-name
18-
{"a" "Anstruther"
19-
"b" "Beaufort"
20-
"c" "Cholmondeley"})
26+
## Elaborate on the design
2127

22-
(def slone-third-name
23-
{"a" "Arbuthnot"
24-
"b" "Battenburg"
25-
"c" "Coutts"})
26-
```
28+
The following alternative data structure design is very simple and more concise, however it does loose some of the semantic meaning.
2729

28-
The following alternative data structure design is very simple and more concise, however it does loose some of the semantic meaning. The position of the names is not defined in terms of the context of the problem.
30+
The position of the names is not defined in terms of the context of the problem.
2931

30-
```clojure
31-
(def slone-names
32-
{:a ["Ally-Pally" "Anstruther" "Arbuthnot"]})
33-
```
32+
??? EXMAPLE ""
33+
```clojure
34+
(def celebrity-first-names
35+
{:a ["Ally-Pally" "Anstruther" "Arbuthnot"]})
36+
```
3437

35-
This next design removes some of the redundancy in defining each letter of the alphabet several times. Apart from less typing and therefore reading by the development team, it also explicitly defines the semantic meaning of each name within the context of this problem.
38+
This next design removes some of the redundancy in defining each letter of the alphabet several times. Apart from less typing and therefore reading by the development team, it also explicitly defines the semantic meaning of each name within the context of this problem.
3639

37-
```clojure
38-
(def slone-names
39-
{:a {:first "Ally-Pally" :second "Anstruther" :third "Arbuthnot"}})
40-
```
41-
<!--endsec-->
40+
??? EXMAPLE ""
41+
```clojure
42+
(def slone-names
43+
{:a {:first "Ally-Pally" :second "Anstruther" :third "Arbuthnot"}})
44+
```
4245

43-
For extra points you could try and implement a function that generated your sloane name.
46+
The design could be taken further by defining a function that generates a celebrity name.
4447

45-
<!--sec data-title="Reveal answer" data-id="answer002" data-collapse=true ces-->
4648

4749
## Creating the algorithm to construct your sloane name
4850

@@ -52,57 +54,62 @@ For extra points you could try and implement a function that generated your sloa
5254

5355
You can get the first element of a string by treating it just like a collection. However this returns a character
5456

55-
```clojure
56-
(first "Strings also act as collections")
57-
```
57+
??? EXMAPLE ""
58+
```clojure
59+
(first "Strings also act as collections")
60+
```
5861

5962
A string can be converted to a keyword, a character cannot
6063

61-
```clojure
62-
(keyword "a")
63-
```
64+
??? EXMAPLE ""
65+
```clojure
66+
(keyword "a")
67+
```
6468

6569
A character can be converted to a string using the str function
6670

67-
```clojure
68-
(str (first "Strings also act as collections"))
69-
```
71+
??? EXMAPLE ""
72+
```clojure
73+
(str (first "Strings also act as collections"))
74+
```
7075

7176
The keywords need to be the same case, so convert the first character to lower case (which returns a string, so the explicit str function is no longer required.)
7277

73-
```clojure
74-
(clojure.string/lower-case (first "Strings also act as collections"))
75-
```
78+
??? EXMAPLE ""
79+
```clojure
80+
(clojure.string/lower-case (first "Strings also act as collections"))
81+
```
7682

7783
Putting it all together.
7884

79-
```clojure
80-
(keyword (clojure.string/lower-case (first "Strings also act as collections")))
81-
```
85+
??? EXMAPLE ""
86+
```clojure
87+
(keyword (clojure.string/lower-case (first "Strings also act as collections")))
88+
```
8289

8390
## Create a function to calculate your sloane name
8491

8592
Putting all this together in a function to generate your sloan name, given your a string with your first and last name.
8693

87-
```clojure
88-
(defn sloane-name
89-
"Given a first and last name as a string, returns your equivalent Sloane name as a string"
90-
[name]
91-
(let [first-name (keyword (clojure.string/lower-case (first (first (clojure.string/split name #" ")))))
92-
middle-name (keyword (clojure.string/lower-case (first (second (clojure.string/split name #" ")))))
93-
last-name (keyword (clojure.string/lower-case (second (second (clojure.string/split name #" ")))))]
94-
(str (get-in slone-names [first-name :first])
95-
" "
96-
(get-in slone-names [middle-name :second])
97-
" "
98-
(get-in slone-names [last-name :third]))))
99-
```
94+
??? EXMAPLE ""
95+
```clojure
96+
(defn sloane-name
97+
"Given a first and last name as a string, returns your equivalent Sloane name as a string"
98+
[name]
99+
(let [first-name (keyword (clojure.string/lower-case (first (first (clojure.string/split name #" ")))))
100+
middle-name (keyword (clojure.string/lower-case (first (second (clojure.string/split name #" ")))))
101+
last-name (keyword (clojure.string/lower-case (second (second (clojure.string/split name #" ")))))]
102+
(str (get-in slone-names [first-name :first])
103+
" "
104+
(get-in slone-names [middle-name :second])
105+
" "
106+
(get-in slone-names [last-name :third]))))
107+
```
100108

101109
Supply a name that will test if the `sloane-name` function works
102110

103-
```clojure
104-
(sloane-name "Billy Abstainer")
105-
;; => "Bongo Anstruther Battenburg"
106-
```
107-
108-
<!--endsec-->
111+
??? EXMAPLE ""
112+
```clojure
113+
(sloane-name "Billy Abstainer")
114+
;; => "Bongo Anstruther Battenburg"
115+
```

0 commit comments

Comments
 (0)