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

test idempotence of trim_me in strings3.rs #2215

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Rudxain
Copy link

@Rudxain Rudxain commented Mar 7, 2025

Technically, this change doesn't directly test for idempotence, as the fn is called once instead of being chained. I still believe it adds value, because it can make the challenge "harder" while still being fair.

Note

The following is just feedback from my recent experience. It can be ignored as "off-topic", but I believe it might be helpful for designing exercises in general.

BTW, when I was solving it, I felt like using trim method was "cheating", so I implemented it myself:

fn trim_me(input: &str) -> &str {
    let l = |c| c != ' ';
    let start = input.find(l).unwrap_or_default();
    // it's easier to let patterns do their job
    // than to handle arithmetic overflows manually,
    // hence the asymmetry
    match input.rfind(l) {
        Some(end) => &input[start..=end],
        _ => &input[start..],
    }
}

Originally, the code was this:

fn trim_me(input: &str) -> &str {
    let l = |c| c != ' ';
    &input[input.find(l).unwrap_or_default()..=input.rfind(l).unwrap_or(input.len() - 1)]
}

Aside from being an eyesore, it doesn't work for "" (input.len() - 1 is always an invalid index). That's why I added some extra tests locally, as a "challenge" to myself.

Should I add the empty str test?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant