Skip to content

Commit

Permalink
reference: update to mkdocs examples
Browse files Browse the repository at this point in the history
  • Loading branch information
practicalli-johnny committed Mar 8, 2024
1 parent 0a90a27 commit ddc93bf
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 284 deletions.
80 changes: 39 additions & 41 deletions docs/designing-data-structures/modeling-alphabet-codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,42 @@ Maps in Clojure are used to model key and value pairs.

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

> **Note** Define a data structure where each letter of the alphabet is represented by a 6 digit binary code
<!--sec data-title="Reveal answer" data-id="answer001" data-collapse=true ces-->

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.

The map also includes a binary code for a full stop and space character

```clojure
(def alphabet {"A" [0 1 0 0 0 1]
"B" [0 0 1 0 1 0]
"C" [0 1 0 0 1 0]
"D" [1 0 1 0 0 0]
"E" [1 0 1 1 0 0]
"F" [1 1 0 1 0 0]
"G" [1 0 0 1 1 0]
"H" [1 0 1 0 0 1]
"I" [1 1 1 0 0 0]
"J" [0 0 1 1 1 1]
"K" [0 1 0 1 0 1]
"L" [1 1 1 0 0 1]
"M" [1 1 1 0 1 1]
"N" [0 1 1 1 0 1]
"O" [1 1 0 1 1 0]
"P" [1 1 1 1 1 0]
"Q" [1 0 1 1 1 0]
"R" [1 1 1 1 0 0]
"S" [0 1 1 1 1 0]
"T" [1 0 0 1 1 1]
"U" [0 0 1 0 1 1]
"V" [0 1 1 0 0 1]
"W" [1 1 0 1 0 1]
"X" [1 0 1 0 1 0]
"Y" [1 0 0 0 1 1]
"Z" [1 1 0 0 1 1]
"." [1 0 1 1 0 1]
" " [0 0 1 0 0 0]})

```

<!--endsec-->
!!! NOTE "Name a data structure"
Define a name for a data structure where each letter of the alphabet is represented by a 6 digit binary code


??? EXAMPLE "Example solution"
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.

The map includes a binary code for a full stop and space character, to help create sentences.

```clojure
(def alphabet {"A" [0 1 0 0 0 1]
"B" [0 0 1 0 1 0]
"C" [0 1 0 0 1 0]
"D" [1 0 1 0 0 0]
"E" [1 0 1 1 0 0]
"F" [1 1 0 1 0 0]
"G" [1 0 0 1 1 0]
"H" [1 0 1 0 0 1]
"I" [1 1 1 0 0 0]
"J" [0 0 1 1 1 1]
"K" [0 1 0 1 0 1]
"L" [1 1 1 0 0 1]
"M" [1 1 1 0 1 1]
"N" [0 1 1 1 0 1]
"O" [1 1 0 1 1 0]
"P" [1 1 1 1 1 0]
"Q" [1 0 1 1 1 0]
"R" [1 1 1 1 0 0]
"S" [0 1 1 1 1 0]
"T" [1 0 0 1 1 1]
"U" [0 0 1 0 1 1]
"V" [0 1 1 0 0 1]
"W" [1 1 0 1 0 1]
"X" [1 0 1 0 1 0]
"Y" [1 0 0 0 1 1]
"Z" [1 1 0 0 1 1]
"." [1 0 1 1 0 1]
" " [0 0 1 0 0 0]})
```
137 changes: 72 additions & 65 deletions docs/designing-data-structures/modeling-name-generation-map.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
# Design a map for name generation

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.
Imagine you are writing a simple celebrity name generator that takes your name and creates a silly version.

> **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).
!!! NOTE ""
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.

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

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

```clojure
(def sloane-first-name
{"a" "Ally-Pally"
"b" "Bongo"
"c" "Chipper"})
(def celebrity-third-name
{"a" "Arbuthnot"
"b" "Battenburg"
"c" "Coutts"})
```

(def slone-second-name
{"a" "Anstruther"
"b" "Beaufort"
"c" "Cholmondeley"})
## Elaborate on the design

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

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.
The position of the names is not defined in terms of the context of the problem.

```clojure
(def slone-names
{:a ["Ally-Pally" "Anstruther" "Arbuthnot"]})
```
??? EXMAPLE ""
```clojure
(def celebrity-first-names
{:a ["Ally-Pally" "Anstruther" "Arbuthnot"]})
```

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.
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.

```clojure
(def slone-names
{:a {:first "Ally-Pally" :second "Anstruther" :third "Arbuthnot"}})
```
<!--endsec-->
??? EXMAPLE ""
```clojure
(def slone-names
{:a {:first "Ally-Pally" :second "Anstruther" :third "Arbuthnot"}})
```

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

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

## Creating the algorithm to construct your sloane name

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

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

```clojure
(first "Strings also act as collections")
```
??? EXMAPLE ""
```clojure
(first "Strings also act as collections")
```

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

```clojure
(keyword "a")
```
??? EXMAPLE ""
```clojure
(keyword "a")
```

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

```clojure
(str (first "Strings also act as collections"))
```
??? EXMAPLE ""
```clojure
(str (first "Strings also act as collections"))
```

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.)

```clojure
(clojure.string/lower-case (first "Strings also act as collections"))
```
??? EXMAPLE ""
```clojure
(clojure.string/lower-case (first "Strings also act as collections"))
```

Putting it all together.

```clojure
(keyword (clojure.string/lower-case (first "Strings also act as collections")))
```
??? EXMAPLE ""
```clojure
(keyword (clojure.string/lower-case (first "Strings also act as collections")))
```

## Create a function to calculate your sloane name

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

```clojure
(defn sloane-name
"Given a first and last name as a string, returns your equivalent Sloane name as a string"
[name]
(let [first-name (keyword (clojure.string/lower-case (first (first (clojure.string/split name #" ")))))
middle-name (keyword (clojure.string/lower-case (first (second (clojure.string/split name #" ")))))
last-name (keyword (clojure.string/lower-case (second (second (clojure.string/split name #" ")))))]
(str (get-in slone-names [first-name :first])
" "
(get-in slone-names [middle-name :second])
" "
(get-in slone-names [last-name :third]))))
```
??? EXMAPLE ""
```clojure
(defn sloane-name
"Given a first and last name as a string, returns your equivalent Sloane name as a string"
[name]
(let [first-name (keyword (clojure.string/lower-case (first (first (clojure.string/split name #" ")))))
middle-name (keyword (clojure.string/lower-case (first (second (clojure.string/split name #" ")))))
last-name (keyword (clojure.string/lower-case (second (second (clojure.string/split name #" ")))))]
(str (get-in slone-names [first-name :first])
" "
(get-in slone-names [middle-name :second])
" "
(get-in slone-names [last-name :third]))))
```

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

```clojure
(sloane-name "Billy Abstainer")
;; => "Bongo Anstruther Battenburg"
```

<!--endsec-->
??? EXMAPLE ""
```clojure
(sloane-name "Billy Abstainer")
;; => "Bongo Anstruther Battenburg"
```
Loading

0 comments on commit ddc93bf

Please sign in to comment.