Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X
Ah, you want the function to just return the value if it's non-string. That can be done,
Being a version that runs in express, it is necessarily a oneliner.
I've enhanced it to allow arrays (vectors, and even nested, matrices), and it now requires no second parameter.
With so many oneline solutions, I propose you add another property to the table: runs in express.
Success!
Luc
How about a 1-liner ?
But, as you can see in the sheet attached, my 1-liner suffers from the same problem as your function - it fails if the string does not contain any spaces.
That's a bug which can easily be fixed by using a "try...on error..." statement, But now the new function is four lines high 😞
EDIT: OK, the third attempt "cleanstr_WE3( )" now is a 1-liner again even though it deals correctly with strings containing no spaces.
This last function still has a drawback (that the second version "cleanstr_WE2( )" does NOT suffer from) and can fail, but I leave that as a challenge for you to find out when and why 😉
As nobody else has answered so far, I simply attached the new file (format P9) containing all three of my attempts in a collapsed and password protected area with a time stamp.
If "cleanstr_WE3( )" is a 1-liner, then it is working great except when the string is not a string. I think inside a "try..on" statement will become a 4-liner.
I have corrected the original version but now it became a 10-liner:
Note: Attached prime 9 worksheet (still password protected until the challenge has a winner. Potential winner: Werner_E )
Mine is a one-liner, with the requirement that it (necessarily) takes two arguments, the second argument is a string (any string). But it runs in Prime express.
It has the following behaviour:
I cannot hide it in a password protected collapsed area, so will not disclose it yet.
What I do know is that you can build it into a two-liner for full-Prime, where it then requires only one parameter, the string itself.
Luc
Hi,
I'll bite.
One liner in Prime full version.
Utilizes a trim function that is only availanble in full Prime
Uses a truth table to sort the vector of ASCII codes.
Not interested in password protecting it.
Cheers
Terry
Similar to my first attempt with similar flaws 😉
BTW, you may simply use match(32,str2vec(a)) to get the index list for the trim command.
OK, here is a 1-liner which also deals with non-string arguments.
The next step would then be a function that handles the input recursively and removes the spaces in all the strings it contains, but leaves the nested structure intact, so that the result in the last example would look like this:
BTW, this fourth attempt still suffers from the same little flaw as the third one.
The most stable so far is the second one which also correctly dealt with non-strings, but because of the used "try ... on error" takes up four lines.
Below is the updated summary so far:
Participant | Function Name | Number of lines | Number of arguments | Can handle no spaces? | Can handle non string? | Can handle matrix? | Can handle nested matrix? | File name (password) | Sample Output for
|
Werner_E | dropSpaces3(a) | 1 | 1 | yes | yes | yes | yes | 240429-DropSpaces v3 | |
terryhendicott | dropSpaces2(a) | 1 | 1 | yes | yes | yes | no | 240429-DropSpaces v2 | |
Werner_E | cleanstr_WE2(a) | 4 | 1 | yes | yes | no | no | WE_20240428(2)_cleanstr(a) (Perez123) | |
Werner_E | cleanstr_WE4(a) | 1 | 1 | yes | yes | no | no | WE_20240428(2)_cleanstr(a) (Perez123) | |
terryhendicott | dropSpaces(a) | 1 | 1 | no | no | no | no | 240429-DropSpaces | |
LucMeekes | clean(a,”b”) | 1 | 2 | yes | no | no | no |
| |
Perez | cleanstr2(a) | 10 | 1 | yes | yes | no | no | PE_20240428_cleanstr(a) (9f95e1a7c7) |
Hi,
herewith is one liner with testing. dropSpaces2(a)
Can handle data besides strings
Can handle string with no spaces,
Can handle string of any number of spaces only,
Can be applied element by element to a matrix.
Cheers
Terry
I would replace "Only Spaces" by an empty string ""
Quite well done, indeed, even though its not working recursively 😉
My functions will deal with matrices (also not recursively for nested ones) if called vectorized.
But I just noticed that none of my functions would deal correctly with a string containing all spaces. They either throw an error or return that string unchanged 😞
When I tried to fix this (and still stay with a one liner) I came up with something which was very (too) similar to your function.
So I abandoned it and instead I modified your dropSpaces2 so that it now will recursively deal with nested matrices (no vectorization necessary when you call the function) and also streamlined it a little bit (str2vec(a) is now only calculated once and assigned a local variable).
I also fixed a bug in your function as it threw an error if the parameter was the empty string ("").
I also guess it's time to put our cards on the table - the password for the protected area in my files is "Perez123" (without the quotes 😉
Ah, you want the function to just return the value if it's non-string. That can be done,
Being a version that runs in express, it is necessarily a oneliner.
I've enhanced it to allow arrays (vectors, and even nested, matrices), and it now requires no second parameter.
With so many oneline solutions, I propose you add another property to the table: runs in express.
Success!
Luc
Updated summary (Apr/29):
Below is the updated summary so far:
Participant | Function Name | Number of lines | Number of arguments | Length (character count) | Can handle no spaces? | Can handle non string? | Can handle matrix? | Can handle nested matrix? | Runs in express?
| File name (password) | Sample Output for:
|
@LucMeekes | cleanstr(str) | 1 | 1 | 213 | yes | yes | yes | yes | yes | LM_20240429_Cleanstr | |
@Werner_E | dropSpaces3(a) | 1 | 1 | 135 | yes | yes | yes | yes | no | 240429-DropSpaces v3 |
|
dropSpaces2(a) | 1 | 1 | 149 | yes | yes | yes | no | no | 240429-DropSpaces v2 |
| |
cleanstr_WE2(a) | 4 | 1 | -- | yes | yes | no | no | no | WE_20240428(2)_cleanstr(a) (Perez123) |
| |
cleanstr_WE4(a) | 1 | 1 | -- | yes | yes | no | no | no | WE_20240428(2)_cleanstr(a) (Perez123) |
| |
dropSpaces(a) | 1 | 1 | -- | no | no | no | no | no | 240429-DropSpaces |
| |
clean(a,”b”) | 1 | 2 | -- | yes | no | no | no | yes |
| ||
cleanstr2(a) | 10 | 1 | -- | yes | yes | no | no | no | PE_20240428_cleanstr(a) (9f95e1a7c7) |
|
Hello @Perez,
It looks like you have some responses from some community members. If any of these replies helped you solve your question please mark the appropriate reply as the Accepted Solution.
Of course, if you have more to share on your issue, please let the Community know so other community members can continue to help you.
Thanks,
Community Moderation Team.
Hello everyone,
After a thorough and insightful review of all submissions, the moment has arrived to crown the champion of this Mathcad challenge. Selecting the winner was as intricate as solving a complex equation, but ultimately one submission met all our criteria with exceptional finesse.
Congratulations to @LucMeekes ! Your function cleanstr(str) not only seamlessly handled non-spaces and non-strings, but it also gracefully managed nested matrices and compatibility with Mathcad Express. Your code is like a master key for Mathcad users, unlocking efficiency and simplicity in one fell swoop!
A big thank you to @Werner_E , and @terryhendicott . Each entry was a testament to the innovation and talent within this forum.
Participant | Function Name | Number of lines | Number of arguments | Length (character count) | Can handle no spaces? | Can handle non string? | Can handle matrix? | Can handle nested matrix? | Runs in express?
| File name (password) | Sample Output for: |
@LucMeekes | cleanstr(str) | 1 | 1 | 213 | yes | yes | yes | yes | yes | LM_20240429_Cleanstr | |
@Werner_E | dropSpaces3(a) | 1 | 1 | 135 | yes | yes | yes | yes | no | 240429-DropSpaces v3 | |
dropSpaces2(a) | 1 | 1 | 149 | yes | yes | yes | no | no | 240429-DropSpaces v2 | ||
cleanstr_WE2(a) | 4 | 1 | -- | yes | yes | no | no | no | WE_20240428(2)_cleanstr(a) (Perez123) | ||
cleanstr_WE4(a) | 1 | 1 | -- | yes | yes | no | no | no | WE_20240428(2)_cleanstr(a) (Perez123) | ||
dropSpaces(a) | 1 | 1 | -- | no | no | no | no | no | 240429-DropSpaces | ||
clean(a,”b”) | 1 | 2 | -- | yes | no | no | no | yes |
| ||
cleanstr2(a) | 10 | 1 | -- | yes | yes | no | no | no | PE_20240428_cleanstr(a) (9f95e1a7c7) |