-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP draft of squeaky-clean concept exercise
- Loading branch information
1 parent
c8ca094
commit 2d44c8f
Showing
8 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Instructions | ||
|
||
In this exercise you will implement a partial set of utility routines to help a developer clean up identifier names. | ||
|
||
In the 6 tasks you will gradually build up the functions `transform` to convert single characters and `clean` to convert strings. | ||
|
||
A valid identifier comprises zero or more letters, underscores, hyphens, question marks and emojis. | ||
|
||
If an empty string is passed to the `clean` function, an empty string should be returned. | ||
|
||
## 1. Replace any hyphens encountered with underscores | ||
|
||
Implement the `transform` function to replace any hyphens with underscores. | ||
|
||
```julia | ||
transform('-') # => "_" | ||
``` | ||
|
||
## 2. Remove all whitespace | ||
|
||
Remove all whitespace characters. | ||
This will include leading and trailing whitespace. | ||
|
||
```julia | ||
transform(' ') # => "" | ||
``` | ||
|
||
## 3. Convert camelCase to kebab-case | ||
|
||
Modify the `transform` function to convert camelCase to kebab-case | ||
|
||
```julia | ||
transform('D') # => "-d" | ||
``` | ||
|
||
## 4. Omit characters that are digits | ||
|
||
Modify the `transform` function to omit any characters that are numeric. | ||
|
||
```julia | ||
transform('7') # => "" | ||
``` | ||
|
||
## 5. Replace Greek lower case letters with question marks | ||
|
||
Modify the `transform` function to replace any Greek letters in the range 'α' to 'ω'. | ||
|
||
```julia | ||
transform('β') # => "?" | ||
``` | ||
|
||
## 6. Combine these operations to operate on a string | ||
|
||
Implement the `clean` function to apply these operations to an entire string. | ||
|
||
Characters which fall outside the rules should pass through unchanged. | ||
|
||
```julia | ||
clean(" a2b Cd-ω😀 ") # => "ab-cd_?😀" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Introduction | ||
|
||
_Left blank for now. Contents of `concepts/chars/introduction.md` to be copied here when finalized._ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"authors": [ | ||
"colinleach" | ||
], | ||
"files": { | ||
"solution": [ | ||
"squeaky-clean.jl" | ||
], | ||
"test": [ | ||
"runtests.jl" | ||
], | ||
"exemplar": [ | ||
".meta/exemplar.jl" | ||
] | ||
}, | ||
"forked_from": [ | ||
"fsharp/squeaky-clean" | ||
], | ||
"blurb": "Learn about characters by cleaning strings." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function transform(ch) | ||
if ch == '-' | ||
"_" | ||
elseif 'α' ≤ ch ≤ 'ω' | ||
"?" | ||
elseif isspace(ch) || isdigit(ch) | ||
"" | ||
elseif isuppercase(ch) | ||
"-$(lowercase(ch))" | ||
else | ||
string(ch) | ||
end | ||
end | ||
|
||
# The default delimiter is "", so it is only included below for documentation | ||
clean(str) = join([transform(ch) for ch in str], "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Hints | ||
|
||
## 1. Replace any spaces encountered with underscores | ||
|
||
- [Reference documentation][char-docs] for `Char`s is here. | ||
- `Char` literals are enclosed in 'single quotes', `String` literals in "double quotes". | ||
|
||
## 2. Remove all whitespace | ||
|
||
- See [this method][isspace] for detecting whitespace characters and [this method][isdigit] for digits. | ||
|
||
## 3. Convert camel-case to kebab-case | ||
|
||
- See [this method][isuppercase] to check if a character is case. | ||
- See [this method][lowercase] to convert a character to lower case. | ||
|
||
## 5. Omit Greek lower case letters | ||
|
||
- `Char`s support the default equality and comparison operators. | ||
|
||
## 6. Combine these operations to operate on a string | ||
|
||
- A string can be iterated over, in the same way as an array. | ||
- To do all the transforms, consider using a comprehension. | ||
- Multiple strings can be [joined][join] into one. | ||
|
||
[char-docs]: https://docs.julialang.org/en/v1/manual/strings/#man-characters | ||
[isspace]: https://docs.julialang.org/en/v1/base/strings/#Base.Unicode.isspace | ||
[isdigit]: https://docs.julialang.org/en/v1/base/strings/#Base.Unicode.isdigit | ||
[lowercase]: https://docs.julialang.org/en/v1/base/strings/#Base.Unicode.lowercase | ||
[isuppercase]: https://docs.julialang.org/en/v1/base/strings/#Base.Unicode.isuppercase | ||
[join]: https://docs.julialang.org/en/v1/base/strings/#Base.join |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Test | ||
|
||
include("squeaky-clean.jl") | ||
|
||
@testset verbose = true "tests" begin | ||
@testset "1. Hyphens" begin | ||
@test transform('a') == "a" | ||
@test transform('-') == "_" | ||
end | ||
|
||
@testset "2. Whitespace" begin | ||
@test transform(' ') == "" | ||
end | ||
|
||
@testset "3. camelCase" begin | ||
@test transform('Γ') == "-γ" | ||
end | ||
|
||
@testset "4. digits" begin | ||
@test transform('4') == "" | ||
end | ||
|
||
@testset "5. Greek lowercase" begin | ||
@test transform('ω') == "?" | ||
end | ||
|
||
@testset "6. Combine in string" begin | ||
@test clean("") == "" | ||
@test clean("àḃç") == "àḃç" | ||
@test clean("my id") == "myid" | ||
@test clean(" my id ") == "myid" | ||
@test clean("9 cAbcĐ😀ω") == "c-abc-đ😀?" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function transform(ch) | ||
|
||
end | ||
|
||
function clean(str) | ||
|
||
end |