Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP draft of squeaky-clean concept exercise #806

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@
"basics"
],
"status": "wip"
},
{
"slug": "squeaky-clean",
"name": "squeaky-clean",
"uuid": "7c06c4e8-4bc1-42c3-982a-f973f45193bf",
"concepts": [
"chars"
],
"prerequisites": [],
"status": "wip"
}
],
"practice": [
Expand Down
32 changes: 32 additions & 0 deletions exercises/concept/squeaky-clean/.docs/hints.md
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
60 changes: 60 additions & 0 deletions exercises/concept/squeaky-clean/.docs/instructions.md
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_?😀"
```
3 changes: 3 additions & 0 deletions exercises/concept/squeaky-clean/.docs/introduction.md
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._
20 changes: 20 additions & 0 deletions exercises/concept/squeaky-clean/.meta/config.json
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."
}
16 changes: 16 additions & 0 deletions exercises/concept/squeaky-clean/.meta/exemplar.jl
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], "")
34 changes: 34 additions & 0 deletions exercises/concept/squeaky-clean/runtests.jl
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
7 changes: 7 additions & 0 deletions exercises/concept/squeaky-clean/squeaky-clean.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function transform(ch)

end

function clean(str)

end
Loading