Skip to content

3. Rust Latin - Solution doesn't take capital letters into account #250

@jeremie2

Description

@jeremie2

Hi!

3. Rust Latin

  • Prepend sr to a word if it begins with a vowel.
  • Append rs to a word if it begins with a consonant.

In the "Task" paragraph it is said that the sentence:

Implement a function that splits a sentence into its words

should be transformed into:

srImplement sra functionrs thatrs splitsrs sra sentencers srinto srits wordsre

(By the way, there's a typo in the last word: wordsre should be wordsrs).

If we run the solution using the previous sentence:

#![allow(unused)]

const VOWELS: [char; 5] = ['a', 'e', 'i', 'o', 'u'];

fn rustlatin(sentence: &str) -> String {
    let mut collection_of_words = Vec::new();

    for word in sentence.split(' ') {
        let latinized_word = latinize(word);
        collection_of_words.push(latinized_word)
    }
    collection_of_words.join(" ")
}

fn latinize(word: &str) -> String {
    let first_char = word.chars().next().unwrap();
    if VOWELS.contains(&first_char) {
        let mut result = "sr".to_string();
        result.push_str(word);
        result
    } else {
        let mut result = word.to_string();
        result.push_str("rs");
        result
    }
}

fn main() {
    let sentence = "Implement a function that splits a sentence into its words";
    println!("{:?}", rustlatin(sentence));          
}

we can see that the first word Implement will be turned into Implementrs instead of srImplement. This because capital vowels are not taken into account.

Some solutions

  • Add the capital vowels to VOWELS constant:
    const VOWELS: [char; 5] = ['a', 'e', 'i', 'o', 'u'];
    so that it becomes:
    const VOWELS: [char; 10] = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
  • When initializing first_char use to_lowercase(). From:
    let first_char = word.chars().next().unwrap();
    
    to:
    let first_char = word.to_lowercase().chars().next().unwrap();
    
  • ...or make clear the exercise is intended for lowercase letters only, changing the sentence in the "Task" paragraph 😉

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions