This commit is contained in:
Avery Winters 2024-03-26 22:10:13 -05:00
parent 9a0e38bae9
commit 8734d3e21f
Signed by: avery
SSH key fingerprint: SHA256:eesvLB5MMqHLZrAMFt6kEhqJWnASMLcET6Sgmw0FqZI
8 changed files with 294 additions and 0 deletions

View file

@ -0,0 +1,40 @@
{
"authors": [
"gregcline"
],
"contributors": [
"AndrewKvalheim",
"ClashTheBunny",
"coriolinus",
"cwhakes",
"efx",
"ErikSchierboom",
"IanWhitney",
"ktomsic",
"lutostag",
"mkantor",
"navossoc",
"nfiles",
"petertseng",
"rofrol",
"stringparser",
"thvdburgt",
"xakon",
"ZapAnton"
],
"files": {
"solution": [
"src/lib.rs",
"Cargo.toml"
],
"test": [
"tests/acronym.rs"
],
"example": [
".meta/example.rs"
]
},
"blurb": "Convert a long phrase to its acronym.",
"source": "Julien Vanier",
"source_url": "https://github.com/monkbroc"
}

View file

@ -0,0 +1 @@
{"track":"rust","exercise":"acronym","id":"75baa794ac4846f6961531321521477f","url":"https://exercism.org/tracks/rust/exercises/acronym","handle":"averywinters","is_requester":true,"auto_approve":false}

8
rust/acronym/.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
# Generated by Cargo
# will have compiled files and executables
/target/
**/*.rs.bk
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock

6
rust/acronym/Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
edition = "2021"
name = "acronym"
version = "1.7.0"
[dependencies]

86
rust/acronym/HELP.md Normal file
View file

@ -0,0 +1,86 @@
# Help
## Running the tests
Execute the tests with:
```bash
$ cargo test
```
All but the first test have been ignored. After you get the first test to
pass, open the tests source file which is located in the `tests` directory
and remove the `#[ignore]` flag from the next test and get the tests to pass
again. Each separate test is a function with `#[test]` flag above it.
Continue, until you pass every test.
If you wish to run _only ignored_ tests without editing the tests source file, use:
```bash
$ cargo test -- --ignored
```
If you are using Rust 1.51 or later, you can run _all_ tests with
```bash
$ cargo test -- --include-ignored
```
To run a specific test, for example `some_test`, you can use:
```bash
$ cargo test some_test
```
If the specific test is ignored, use:
```bash
$ cargo test some_test -- --ignored
```
To learn more about Rust tests refer to the online [test documentation][rust-tests].
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
## Submitting your solution
You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` 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 [Rust track's documentation](https://exercism.org/docs/tracks/rust)
- The [Rust track's programming category on the forum](https://forum.exercism.org/c/programming/rust)
- [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.
## Rust Installation
Refer to the [exercism help page][help-page] for Rust installation and learning
resources.
## Submitting the solution
Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
## Feedback, Issues, Pull Requests
The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
If you want to know more about Exercism, take a look at the [contribution guide].
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
[help-page]: https://exercism.org/tracks/rust/learning
[github]: https://github.com/exercism/rust
[contribution guide]: https://exercism.org/docs/community/contributors

53
rust/acronym/README.md Normal file
View file

@ -0,0 +1,53 @@
# Acronym
Welcome to Acronym on Exercism's Rust Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## 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 |
## Source
### Created by
- @gregcline
### Contributed to by
- @AndrewKvalheim
- @ClashTheBunny
- @coriolinus
- @cwhakes
- @efx
- @ErikSchierboom
- @IanWhitney
- @ktomsic
- @lutostag
- @mkantor
- @navossoc
- @nfiles
- @petertseng
- @rofrol
- @stringparser
- @thvdburgt
- @xakon
- @ZapAnton
### Based on
Julien Vanier - https://github.com/monkbroc

21
rust/acronym/src/lib.rs Normal file
View file

@ -0,0 +1,21 @@
pub fn abbreviate(phrase: &str) -> String {
phrase
.split(|c: char| c.is_whitespace() || c == '-')
.fold(String::new(), |mut s, word| {
let alpha = word.chars().filter(|c| c.is_alphabetic());
let (count, upper_count) = alpha.clone().fold((0, 0), |(count, upper_count), c| {
(count + 1, upper_count + c.is_uppercase() as usize)
});
if (1..count).contains(&upper_count) {
// Some, but not all, of the word is uppercase. All the uppercase
// characters become part of the abbreviation.
s.extend(alpha.filter(|c| c.is_uppercase()));
} else {
// Either the whole word is uppercase, or none of it is. In either case,
// the abbreviation continues with the uppercase version of the next
// alphabetic character.
s.extend(alpha.take(1).flat_map(|c| c.to_uppercase()));
}
s
})
}

View file

@ -0,0 +1,79 @@
#[test]
fn basic() {
let input = "Portable Network Graphics";
let output = acronym::abbreviate(input);
let expected = "PNG";
assert_eq!(output, expected);
}
#[test]
fn lowercase_words() {
let input = "Ruby on Rails";
let output = acronym::abbreviate(input);
let expected = "ROR";
assert_eq!(output, expected);
}
#[test]
fn punctuation() {
let input = "First In, First Out";
let output = acronym::abbreviate(input);
let expected = "FIFO";
assert_eq!(output, expected);
}
#[test]
fn all_caps_word() {
let input = "GNU Image Manipulation Program";
let output = acronym::abbreviate(input);
let expected = "GIMP";
assert_eq!(output, expected);
}
#[test]
fn punctuation_without_whitespace() {
let input = "Complementary metal-oxide semiconductor";
let output = acronym::abbreviate(input);
let expected = "CMOS";
assert_eq!(output, expected);
}
#[test]
fn very_long_abbreviation() {
let input = "Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me";
let output = acronym::abbreviate(input);
let expected = "ROTFLSHTMDCOALM";
assert_eq!(output, expected);
}
#[test]
fn consecutive_delimiters() {
let input = "Something - I made up from thin air";
let output = acronym::abbreviate(input);
let expected = "SIMUFTA";
assert_eq!(output, expected);
}
#[test]
fn apostrophes() {
let input = "Halley's Comet";
let output = acronym::abbreviate(input);
let expected = "HC";
assert_eq!(output, expected);
}
#[test]
fn underscore_emphasis() {
let input = "The Road _Not_ Taken";
let output = acronym::abbreviate(input);
let expected = "TRNT";
assert_eq!(output, expected);
}
#[test]
fn camelcase() {
let input = "HyperText Markup Language";
let output = acronym::abbreviate(input);
let expected = "HTML";
assert_eq!(output, expected);
}