Skip to content

Commit

Permalink
acronym (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacvando authored Aug 27, 2024
1 parent 36278e0 commit 3eaef38
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 9 deletions.
20 changes: 11 additions & 9 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,9 @@
"highlightjs_language": "haskell"
},
"files": {
"solution": [
"%{pascal_slug}.roc"
],
"test": [
"%{kebab_slug}-test.roc"
],
"example": [
".meta/Example.roc"
]
"solution": ["%{pascal_slug}.roc"],
"test": ["%{kebab_slug}-test.roc"],
"example": [".meta/Example.roc"]
},
"exercises": {
"practice": [
Expand Down Expand Up @@ -188,6 +182,14 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "acronym",
"name": "Acronym",
"uuid": "5bc989a9-f7a8-489d-b9fc-6f65c2425fd7",
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "list-ops",
"name": "List Ops",
Expand Down
17 changes: 17 additions & 0 deletions exercises/practice/acronym/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Instructions

Convert a phrase to its acronym.

Techies love their TLA (Three Letter Acronyms)!

Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).

Punctuation is handled as follows: hyphens are word separators (like whitespace); all other punctuation can be removed from the input.

For example:

| Input | Output |
| ------------------------- | ------ |
| As Soon As Possible | ASAP |
| Liquid-crystal display | LCD |
| Thank George It's Friday! | TGIF |
32 changes: 32 additions & 0 deletions exercises/practice/acronym/.meta/Example.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module [abbreviate]

abbreviate : Str -> Str
abbreviate = \text ->
bytes = Str.toUtf8 text

{ acronym } = List.walk bytes { acronym: [], readyForLetter: Bool.true } \state, byte ->
if state.readyForLetter && isLetter byte then
{ acronym: List.append state.acronym byte, readyForLetter: Bool.false }
else if byte == ' ' || byte == '-' then
{ acronym: state.acronym, readyForLetter: Bool.true }
else
state

capitalized = List.map acronym capitalize

when Str.fromUtf8 capitalized is
Err _ -> crash "There was an error converting the bytes to a Str! This should never happen."
Ok str -> str

isLetter : U8 -> Bool
isLetter = \byte ->
('a' <= byte && byte <= 'z')
||
('A' <= byte && byte <= 'Z')

capitalize : U8 -> U8
capitalize = \byte ->
if 'a' <= byte && byte <= 'z' then
byte - 32
else
byte
19 changes: 19 additions & 0 deletions exercises/practice/acronym/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"isaacvando"
],
"files": {
"solution": [
"Acronym.roc"
],
"test": [
"acronym-test.roc"
],
"example": [
".meta/Example.roc"
]
},
"blurb": "Convert a long phrase to its acronym.",
"source": "Julien Vanier",
"source_url": "https://github.com/monkbroc"
}
101 changes: 101 additions & 0 deletions exercises/practice/acronym/.meta/template.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{%- import "generator_macros.j2" as macros with context -%}
{{ macros.canonical_ref() }}
{{ macros.header() }}

import {{ exercise | to_pascal }} exposing [abbreviate]

{% for case in cases -%}
# {{ case["description"] }}
expect
result = {{ case["property"] | to_camel }} "{{ case["input"]["phrase"] }}"
result == "{{ case["expected"] }}"

{% endfor %}

# acronym/canonical-data.json
{
"exercise": "acronym",
"cases": [
{
"uuid": "1e22cceb-c5e4-4562-9afe-aef07ad1eaf4",
"description": "basic",
"property": "abbreviate",
"input": {
"phrase": "Portable Network Graphics"
},
"expected": "PNG"
},
{
"uuid": "79ae3889-a5c0-4b01-baf0-232d31180c08",
"description": "lowercase words",
"property": "abbreviate",
"input": {
"phrase": "Ruby on Rails"
},
"expected": "ROR"
},
{
"uuid": "ec7000a7-3931-4a17-890e-33ca2073a548",
"description": "punctuation",
"property": "abbreviate",
"input": {
"phrase": "First In, First Out"
},
"expected": "FIFO"
},
{
"uuid": "32dd261c-0c92-469a-9c5c-b192e94a63b0",
"description": "all caps word",
"property": "abbreviate",
"input": {
"phrase": "GNU Image Manipulation Program"
},
"expected": "GIMP"
},
{
"uuid": "ae2ac9fa-a606-4d05-8244-3bcc4659c1d4",
"description": "punctuation without whitespace",
"property": "abbreviate",
"input": {
"phrase": "Complementary metal-oxide semiconductor"
},
"expected": "CMOS"
},
{
"uuid": "0e4b1e7c-1a6d-48fb-81a7-bf65eb9e69f9",
"description": "very long abbreviation",
"property": "abbreviate",
"input": {
"phrase": "Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me"
},
"expected": "ROTFLSHTMDCOALM"
},
{
"uuid": "6a078f49-c68d-4b7b-89af-33a1a98c28cc",
"description": "consecutive delimiters",
"property": "abbreviate",
"input": {
"phrase": "Something - I made up from thin air"
},
"expected": "SIMUFTA"
},
{
"uuid": "5118b4b1-4572-434c-8d57-5b762e57973e",
"description": "apostrophes",
"property": "abbreviate",
"input": {
"phrase": "Halley's Comet"
},
"expected": "HC"
},
{
"uuid": "adc12eab-ec2d-414f-b48c-66a4fc06cdef",
"description": "underscore emphasis",
"property": "abbreviate",
"input": {
"phrase": "The Road _Not_ Taken"
},
"expected": "TRNT"
}
]
}
37 changes: 37 additions & 0 deletions exercises/practice/acronym/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[1e22cceb-c5e4-4562-9afe-aef07ad1eaf4]
description = "basic"

[79ae3889-a5c0-4b01-baf0-232d31180c08]
description = "lowercase words"

[ec7000a7-3931-4a17-890e-33ca2073a548]
description = "punctuation"

[32dd261c-0c92-469a-9c5c-b192e94a63b0]
description = "all caps word"

[ae2ac9fa-a606-4d05-8244-3bcc4659c1d4]
description = "punctuation without whitespace"

[0e4b1e7c-1a6d-48fb-81a7-bf65eb9e69f9]
description = "very long abbreviation"

[6a078f49-c68d-4b7b-89af-33a1a98c28cc]
description = "consecutive delimiters"

[5118b4b1-4572-434c-8d57-5b762e57973e]
description = "apostrophes"

[adc12eab-ec2d-414f-b48c-66a4fc06cdef]
description = "underscore emphasis"
5 changes: 5 additions & 0 deletions exercises/practice/acronym/Acronym.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module [abbreviate]

abbreviate : Str -> Str
abbreviate = \text ->
crash "Please implement the 'abbreviate' function"
58 changes: 58 additions & 0 deletions exercises/practice/acronym/acronym-test.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# These tests are auto-generated with test data from:
# https://github.com/exercism/problem-specifications/tree/main/exercises/acronym/canonical-data.json
# File last updated on 2024-08-27
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.14.0/dC5ceT962N_4jmoyoffVdphJ_4GlW3YMhAPyGPr-nU0.tar.br",
}

import pf.Task exposing [Task]

main =
Task.ok {}

import Acronym exposing [abbreviate]

# basic
expect
result = abbreviate "Portable Network Graphics"
result == "PNG"

# lowercase words
expect
result = abbreviate "Ruby on Rails"
result == "ROR"

# punctuation
expect
result = abbreviate "First In, First Out"
result == "FIFO"

# all caps word
expect
result = abbreviate "GNU Image Manipulation Program"
result == "GIMP"

# punctuation without whitespace
expect
result = abbreviate "Complementary metal-oxide semiconductor"
result == "CMOS"

# very long abbreviation
expect
result = abbreviate "Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me"
result == "ROTFLSHTMDCOALM"

# consecutive delimiters
expect
result = abbreviate "Something - I made up from thin air"
result == "SIMUFTA"

# apostrophes
expect
result = abbreviate "Halley's Comet"
result == "HC"

# underscore emphasis
expect
result = abbreviate "The Road _Not_ Taken"
result == "TRNT"

0 comments on commit 3eaef38

Please sign in to comment.