You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
-
(defalphabet {"A" [010001]
20
-
"B" [001010]
21
-
"C" [010010]
22
-
"D" [101000]
23
-
"E" [101100]
24
-
"F" [110100]
25
-
"G" [100110]
26
-
"H" [101001]
27
-
"I" [111000]
28
-
"J" [001111]
29
-
"K" [010101]
30
-
"L" [111001]
31
-
"M" [111011]
32
-
"N" [011101]
33
-
"O" [110110]
34
-
"P" [111110]
35
-
"Q" [101110]
36
-
"R" [111100]
37
-
"S" [011110]
38
-
"T" [100111]
39
-
"U" [001011]
40
-
"V" [011001]
41
-
"W" [110101]
42
-
"X" [101010]
43
-
"Y" [100011]
44
-
"Z" [110011]
45
-
"." [101101]
46
-
"" [001000]})
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.
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.
4
4
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.
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"})
10
19
11
-
```clojure
12
-
(defsloane-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
+
```
16
25
17
-
(defslone-second-name
18
-
{"a""Anstruther"
19
-
"b""Beaufort"
20
-
"c""Cholmondeley"})
26
+
## Elaborate on the design
21
27
22
-
(defslone-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.
27
29
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.
29
31
30
-
```clojure
31
-
(defslone-names
32
-
{:a ["Ally-Pally""Anstruther""Arbuthnot"]})
33
-
```
32
+
??? EXMAPLE ""
33
+
```clojure
34
+
(def celebrity-first-names
35
+
{:a["Ally-Pally" "Anstruther" "Arbuthnot"]})
36
+
```
34
37
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.
## Creating the algorithm to construct your sloane name
48
50
@@ -52,57 +54,62 @@ For extra points you could try and implement a function that generated your sloa
52
54
53
55
You can get the first element of a string by treating it just like a collection. However this returns a character
54
56
55
-
```clojure
56
-
(first"Strings also act as collections")
57
-
```
57
+
??? EXMAPLE ""
58
+
```clojure
59
+
(first "Strings also act as collections")
60
+
```
58
61
59
62
A string can be converted to a keyword, a character cannot
60
63
61
-
```clojure
62
-
(keyword"a")
63
-
```
64
+
??? EXMAPLE ""
65
+
```clojure
66
+
(keyword "a")
67
+
```
64
68
65
69
A character can be converted to a string using the str function
66
70
67
-
```clojure
68
-
(str (first"Strings also act as collections"))
69
-
```
71
+
??? EXMAPLE ""
72
+
```clojure
73
+
(str (first "Strings also act as collections"))
74
+
```
70
75
71
76
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.)
72
77
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
+
```
76
82
77
83
Putting it all together.
78
84
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
+
```
82
89
83
90
## Create a function to calculate your sloane name
84
91
85
92
Putting all this together in a function to generate your sloan name, given your a string with your first and last name.
86
93
87
-
```clojure
88
-
(defnsloane-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
+
```
100
108
101
109
Supply a name that will test if the `sloane-name` function works
0 commit comments