high-school-sweetheart
This commit is contained in:
parent
5c704aa88f
commit
5cc59f9234
|
@ -0,0 +1,22 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"angelikatyborska"
|
||||||
|
],
|
||||||
|
"contributors": [
|
||||||
|
"neenjaw"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"lib/high_school_sweetheart.ex"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test/high_school_sweetheart_test.exs"
|
||||||
|
],
|
||||||
|
"exemplar": [
|
||||||
|
".meta/exemplar.ex"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"language_versions": ">=1.10",
|
||||||
|
"icon": "high-school-sweethearts",
|
||||||
|
"blurb": "Learn about strings and the pipe operator by helping high school sweethearts profess their love on social media via ASCII art."
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
{"track":"elixir","exercise":"high-school-sweetheart","id":"338aaf1c5adb4b5289ec37cdbaef88c0","url":"https://exercism.org/tracks/elixir/exercises/high-school-sweetheart","handle":"negrienko","is_requester":true,"auto_approve":false}
|
|
@ -0,0 +1,4 @@
|
||||||
|
# Used by "mix format"
|
||||||
|
[
|
||||||
|
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||||
|
]
|
|
@ -0,0 +1,24 @@
|
||||||
|
# The directory Mix will write compiled artifacts to.
|
||||||
|
/_build/
|
||||||
|
|
||||||
|
# If you run "mix test --cover", coverage assets end up here.
|
||||||
|
/cover/
|
||||||
|
|
||||||
|
# The directory Mix downloads your dependencies sources to.
|
||||||
|
/deps/
|
||||||
|
|
||||||
|
# Where third-party dependencies like ExDoc output generated docs.
|
||||||
|
/doc/
|
||||||
|
|
||||||
|
# Ignore .fetch files in case you like to edit your project deps locally.
|
||||||
|
/.fetch
|
||||||
|
|
||||||
|
# If the VM crashes, it generates a dump, let's ignore it too.
|
||||||
|
erl_crash.dump
|
||||||
|
|
||||||
|
# Also ignore archive artifacts (built via "mix archive.build").
|
||||||
|
*.ez
|
||||||
|
|
||||||
|
# Ignore package tarball (built via "mix hex.build").
|
||||||
|
basic_strings-*.tar
|
||||||
|
|
|
@ -0,0 +1,75 @@
|
||||||
|
# Help
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
From the terminal, change to the base directory of the exercise then execute the tests with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ mix test
|
||||||
|
```
|
||||||
|
|
||||||
|
This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs`
|
||||||
|
|
||||||
|
Documentation:
|
||||||
|
|
||||||
|
* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html)
|
||||||
|
* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html)
|
||||||
|
|
||||||
|
## Pending tests
|
||||||
|
|
||||||
|
In test suites of practice exercises, all but the first test have been tagged to be skipped.
|
||||||
|
|
||||||
|
Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
# @tag :pending
|
||||||
|
test "shouting" do
|
||||||
|
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ mix test --include pending
|
||||||
|
```
|
||||||
|
|
||||||
|
Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`.
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
# ExUnit.configure(exclude: :pending, trace: true)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Useful `mix test` options
|
||||||
|
|
||||||
|
* `test/<FILE>.exs:LINENUM` - runs only a single test, the test from `<FILE>.exs` whose definition is on line `LINENUM`
|
||||||
|
* `--failed` - runs only tests that failed the last time they ran
|
||||||
|
* `--max-failures` - the suite stops evaluating tests when this number of test failures
|
||||||
|
is reached
|
||||||
|
* `--seed 0` - disables randomization so the tests in a single file will always be ran
|
||||||
|
in the same order they were defined in
|
||||||
|
|
||||||
|
## Submitting your solution
|
||||||
|
|
||||||
|
You can submit your solution using the `exercism submit lib/high_school_sweetheart.ex` command.
|
||||||
|
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||||
|
|
||||||
|
It's possible to submit an incomplete solution which allows you to:
|
||||||
|
|
||||||
|
- See how others have completed the exercise
|
||||||
|
- Request help from a mentor
|
||||||
|
|
||||||
|
## Need to get help?
|
||||||
|
|
||||||
|
If you'd like help solving the exercise, check the following pages:
|
||||||
|
|
||||||
|
- The [Elixir track's documentation](https://exercism.org/docs/tracks/elixir)
|
||||||
|
- The [Elixir track's programming category on the forum](https://forum.exercism.org/c/programming/elixir)
|
||||||
|
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||||
|
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||||
|
|
||||||
|
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||||
|
|
||||||
|
If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found.
|
|
@ -0,0 +1,36 @@
|
||||||
|
# Hints
|
||||||
|
|
||||||
|
## General
|
||||||
|
|
||||||
|
- Read about strings in the official [Getting Started guide][getting-started-strings].
|
||||||
|
- Browse the [functions available in the _String module_][string-module-functions] to discover which operations on strings Elixir's standard library offers.
|
||||||
|
|
||||||
|
## 1. Get the name's first letter
|
||||||
|
|
||||||
|
- There is a [built-in function][string-first] to get the first character from a string.
|
||||||
|
- There are multiple [built-in functions][string-trim] to remove leading, trailing, or leading and trailing whitespaces from a string.
|
||||||
|
|
||||||
|
## 2. Format the first letter as an initial
|
||||||
|
|
||||||
|
- There is a [built-in function][string-upcase] to convert all characters in a string to their uppercase variant.
|
||||||
|
- There is an [operator][kernel-concat] that concatenates two strings.
|
||||||
|
|
||||||
|
## 3. Split the full name into the first name and the last name
|
||||||
|
|
||||||
|
- There is a [built-in function][string-split] that splits a string on whitespace characters.
|
||||||
|
- A few first elements of a list can be assigned to variables by pattern matching on the list.
|
||||||
|
|
||||||
|
## 4. Put the initials inside of the heart
|
||||||
|
|
||||||
|
- There is a special syntax for [interpolating][string-interpolation] an expression inside of a string.
|
||||||
|
- There is a special syntax for writing [multiline strings][heredoc-syntax] without needing to escape newlines.
|
||||||
|
|
||||||
|
[getting-started-strings]: https://elixir-lang.org/getting-started/basic-types.html#strings
|
||||||
|
[string-module-functions]: https://hexdocs.pm/elixir/String.html#functions
|
||||||
|
[string-first]: https://hexdocs.pm/elixir/String.html#first/1
|
||||||
|
[string-trim]: https://hexdocs.pm/elixir/String.html#trim/1
|
||||||
|
[string-upcase]: https://hexdocs.pm/elixir/String.html#upcase/2
|
||||||
|
[string-split]: https://hexdocs.pm/elixir/String.html#split/1
|
||||||
|
[string-interpolation]: https://hexdocs.pm/elixir/String.html#module-interpolation
|
||||||
|
[kernel-concat]: https://hexdocs.pm/elixir/Kernel.html#%3C%3E/2
|
||||||
|
[heredoc-syntax]: https://elixir-examples.github.io/examples/multiline-strings-heredocs
|
|
@ -0,0 +1,140 @@
|
||||||
|
# High School Sweetheart
|
||||||
|
|
||||||
|
Welcome to High School Sweetheart on Exercism's Elixir Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
## Strings
|
||||||
|
|
||||||
|
Strings in Elixir are delimited by double quotes, and they are encoded in UTF-8:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
"Hi!"
|
||||||
|
```
|
||||||
|
|
||||||
|
Strings can be concatenated using the `<>/2` operator:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
"Welcome to" <> " " <> "New York"
|
||||||
|
# => "Welcome to New York"
|
||||||
|
```
|
||||||
|
|
||||||
|
Strings in Elixir support interpolation using the `#{}` syntax:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
"6 * 7 = #{6 * 7}"
|
||||||
|
# => "6 * 7 = 42"
|
||||||
|
```
|
||||||
|
|
||||||
|
To put a newline character in a string, use the `\n` escape code:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
"1\n2\n3\n"
|
||||||
|
```
|
||||||
|
|
||||||
|
To comfortably work with texts with a lot of newlines, use the triple-double-quote heredoc syntax instead:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
"""
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
"""
|
||||||
|
```
|
||||||
|
|
||||||
|
Elixir provides many functions for working with strings in the `String` module.
|
||||||
|
|
||||||
|
## Pipe Operator
|
||||||
|
|
||||||
|
The `|>` operator is called the pipe operator. It can be used to chain function calls together in such a way that the value returned by the previous function call is passed as the first argument to the next function call.
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
"hello"
|
||||||
|
|> String.upcase()
|
||||||
|
|> Kernel.<>("?!")
|
||||||
|
# => "HELLO?!"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
In this exercise, you are going to help high school sweethearts profess their love on social media by generating an ASCII heart with their initials:
|
||||||
|
|
||||||
|
```
|
||||||
|
****** ******
|
||||||
|
** ** ** **
|
||||||
|
** ** ** **
|
||||||
|
** * **
|
||||||
|
** **
|
||||||
|
** J. K. + M. B. **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
***
|
||||||
|
*
|
||||||
|
```
|
||||||
|
|
||||||
|
## 1. Get the name's first letter
|
||||||
|
|
||||||
|
Implement the `HighSchoolSweetheart.first_letter/1` function. It should take a name and return its first letter. It should clean up any unnecessary whitespace from the name.
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
HighSchoolSweetheart.first_letter("Jane")
|
||||||
|
# => "J"
|
||||||
|
```
|
||||||
|
|
||||||
|
## 2. Format the first letter as an initial
|
||||||
|
|
||||||
|
Implement the `HighSchoolSweetheart.initial/1` function. It should take a name and return its first letter, uppercase, followed by a dot. Make sure to reuse `HighSchoolSweetheart.first_letter/1` that you defined in the previous step.
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
HighSchoolSweetheart.initial("Robert")
|
||||||
|
# => "R."
|
||||||
|
```
|
||||||
|
|
||||||
|
## 3. Split the full name into the first name and the last name
|
||||||
|
|
||||||
|
Implement the `HighSchoolSweetheart.initials/1` function. It should take a full name, consisting of a first name and a last name separated by a space, and return the initials. Make sure to reuse `HighSchoolSweetheart.initial/1` that you defined in the previous step.
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
HighSchoolSweetheart.initials("Lance Green")
|
||||||
|
# => "L. G."
|
||||||
|
```
|
||||||
|
|
||||||
|
## 4. Put the initials inside of the heart
|
||||||
|
|
||||||
|
Implement the `HighSchoolSweetheart.pair/2` function. It should take two full names and return the initials inside an ASCII heart. Make sure to reuse `HighSchoolSweetheart.initials/1` that you defined in the previous step.
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
HighSchoolSweetheart.pair("Blake Miller", "Riley Lewis")
|
||||||
|
# => """
|
||||||
|
# ****** ******
|
||||||
|
# ** ** ** **
|
||||||
|
# ** ** ** **
|
||||||
|
# ** * **
|
||||||
|
# ** **
|
||||||
|
# ** B. M. + R. L. **
|
||||||
|
# ** **
|
||||||
|
# ** **
|
||||||
|
# ** **
|
||||||
|
# ** **
|
||||||
|
# ** **
|
||||||
|
# ** **
|
||||||
|
# ***
|
||||||
|
# *
|
||||||
|
# """
|
||||||
|
```
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @angelikatyborska
|
||||||
|
|
||||||
|
### Contributed to by
|
||||||
|
|
||||||
|
- @neenjaw
|
|
@ -0,0 +1,42 @@
|
||||||
|
defmodule HighSchoolSweetheart do
|
||||||
|
def first_letter(name) do
|
||||||
|
name
|
||||||
|
|> String.trim()
|
||||||
|
|> String.first()
|
||||||
|
end
|
||||||
|
|
||||||
|
def initial(name) do
|
||||||
|
name
|
||||||
|
|> first_letter()
|
||||||
|
|> String.upcase()
|
||||||
|
|> Kernel.<>(".")
|
||||||
|
end
|
||||||
|
|
||||||
|
def initials(full_name) do
|
||||||
|
full_name
|
||||||
|
|> String.split(" ")
|
||||||
|
|> Enum.map(&initial/1)
|
||||||
|
|> Enum.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
def pair(full_name1, full_name2) do
|
||||||
|
i1 = initials(full_name1)
|
||||||
|
i2 = initials(full_name2)
|
||||||
|
"""
|
||||||
|
****** ******
|
||||||
|
** ** ** **
|
||||||
|
** ** ** **
|
||||||
|
** * **
|
||||||
|
** **
|
||||||
|
** #{i1} + #{i2} **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
***
|
||||||
|
*
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,28 @@
|
||||||
|
defmodule HighSchoolSweetheart.MixProject do
|
||||||
|
use Mix.Project
|
||||||
|
|
||||||
|
def project do
|
||||||
|
[
|
||||||
|
app: :high_school_sweetheart,
|
||||||
|
version: "0.1.0",
|
||||||
|
# elixir: "~> 1.10",
|
||||||
|
start_permanent: Mix.env() == :prod,
|
||||||
|
deps: deps()
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Run "mix help compile.app" to learn about applications.
|
||||||
|
def application do
|
||||||
|
[
|
||||||
|
extra_applications: [:logger]
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Run "mix help deps" to learn about dependencies.
|
||||||
|
defp deps do
|
||||||
|
[
|
||||||
|
# {:dep_from_hexpm, "~> 0.3.0"},
|
||||||
|
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,62 @@
|
||||||
|
defmodule HighSchoolSweetheartTest do
|
||||||
|
use ExUnit.Case
|
||||||
|
|
||||||
|
describe "first_letter/1" do
|
||||||
|
@tag task_id: 1
|
||||||
|
test "it gets the first letter" do
|
||||||
|
assert HighSchoolSweetheart.first_letter("Mary") == "M"
|
||||||
|
end
|
||||||
|
|
||||||
|
@tag task_id: 1
|
||||||
|
test "it doesn't change the letter's case" do
|
||||||
|
assert HighSchoolSweetheart.first_letter("john") == "j"
|
||||||
|
end
|
||||||
|
|
||||||
|
@tag task_id: 1
|
||||||
|
test "it strips extra whitespace" do
|
||||||
|
assert HighSchoolSweetheart.first_letter("\n\t Sarah ") == "S"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "initial/1" do
|
||||||
|
@tag task_id: 2
|
||||||
|
test "it gets the first letter and appends a dot" do
|
||||||
|
assert HighSchoolSweetheart.initial("Betty") == "B."
|
||||||
|
end
|
||||||
|
|
||||||
|
@tag task_id: 2
|
||||||
|
test "it uppercases the first letter" do
|
||||||
|
assert HighSchoolSweetheart.initial("james") == "J."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "initials/1" do
|
||||||
|
@tag task_id: 3
|
||||||
|
test "returns the initials for the first name and the last name" do
|
||||||
|
assert HighSchoolSweetheart.initials("Linda Miller") == "L. M."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "pair/2" do
|
||||||
|
@tag task_id: 4
|
||||||
|
test "prints the pair's initials inside a heart" do
|
||||||
|
assert HighSchoolSweetheart.pair("Avery Bryant", "Charlie Dixon") ==
|
||||||
|
"""
|
||||||
|
****** ******
|
||||||
|
** ** ** **
|
||||||
|
** ** ** **
|
||||||
|
** * **
|
||||||
|
** **
|
||||||
|
** A. B. + C. D. **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
***
|
||||||
|
*
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,2 @@
|
||||||
|
ExUnit.start()
|
||||||
|
ExUnit.configure(exclude: :pending, trace: true, seed: 0)
|
Loading…
Reference in New Issue