cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

MathCAD Prime string challenge

Perez
12-Amethyst

MathCAD Prime string challenge

Perez_0-1714326625759.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
LucMeekes
23-Emerald III
(To:Perez)

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.

LucMeekes_0-1714374769997.png

 

With so many oneline solutions, I propose you add another property to the table: runs in express.

 

Success!

Luc

View solution in original post

14 REPLIES 14
Werner_E
24-Ruby V
(To:Perez)

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.

Werner_E_0-1714335834130.png

 

Perez
12-Amethyst
(To:Werner_E)

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.

 

Perez_0-1714335679721.png

 

I have corrected the original version but now it became a 10-liner:

Perez_1-1714336581899.png

Note: Attached prime 9 worksheet (still password protected until the challenge has a winner. Potential winner: Werner_E ) 

LucMeekes
23-Emerald III
(To:Perez)

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:

LucMeekes_0-1714341467387.png

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.

Capture.jpg

Cheers

Terry

Similar to my first attempt with similar flaws 😉

Werner_E_0-1714345447017.png

BTW, you may simply use  match(32,str2vec(a))  to get the index list for the trim command.

Werner_E
24-Ruby V
(To:Perez)

OK, here is a 1-liner which also deals with non-string arguments.

Werner_E_0-1714344612312.png

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:

Werner_E_1-1714344825048.png

 

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.

 

 

Perez
12-Amethyst
(To:Perez)

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  Perez_2-1714358967196.png

 

Werner_E

dropSpaces3(a)

1

1

yes

yes

yes

yes

240429-DropSpaces v3

Perez_0-1714365785139.png

terryhendicott

dropSpaces2(a)

1

1

yes

yes

yes

no

240429-DropSpaces v2

Perez_1-1714358926874.png

Werner_E

cleanstr_WE2(a)

4

1

yes

yes

no

no

WE_20240428(2)_cleanstr(a)

(Perez123)

Perez_3-1714359011144.png

Werner_E

cleanstr_WE4(a)

1

1

yes

yes

no

no

WE_20240428(2)_cleanstr(a)

(Perez123)

Perez_4-1714359036532.png

terryhendicott

dropSpaces(a)

1

1

no

no

no

no

240429-DropSpaces

Perez_5-1714359053389.png

LucMeekes

clean(a,”b”)

1

2

yes

no

no

no

 

 

Perez

cleanstr2(a)

10

1

yes

yes

no

no

PE_20240428_cleanstr(a)

(9f95e1a7c7)

 Perez_6-1714359070233.png

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

Hi,

OOps wrong file.

I would replace "Only Spaces" by an empty string ""

Quite well done, indeed, even though its not working recursively 😉

Werner_E_1-1714360960186.png

 

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 ("").

Werner_E_3-1714363598416.png

 

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 😉

 

LucMeekes
23-Emerald III
(To:Perez)

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.

LucMeekes_0-1714374769997.png

 

With so many oneline solutions, I propose you add another property to the table: runs in express.

 

Success!

Luc

Perez
12-Amethyst
(To:Perez)

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:  

Perez_0-1714443552225.png

 

 

@LucMeekes 

cleanstr(str)

1

1

213

yes

yes

yes

yes

yes

LM_20240429_Cleanstr

Perez_7-1714443776462.png
@Werner_E 

dropSpaces3(a)

1

1

135

yes

yes

yes

yes

no

240429-DropSpaces v3

Perez_1-1714443552249.png

 

@terryhendicott 

dropSpaces2(a)

1

1

149

yes

yes

yes

no

no

240429-DropSpaces v2

Perez_2-1714443552363.png

 

@Werner_E 

cleanstr_WE2(a)

4

1

--

yes

yes

no

no

no

WE_20240428(2)_cleanstr(a)

(Perez123)

Perez_3-1714443552319.png

 

@Werner_E 

cleanstr_WE4(a)

1

1

--

yes

yes

no

no

no

WE_20240428(2)_cleanstr(a)

(Perez123)

Perez_4-1714443552279.png

 

@terryhendicott 

dropSpaces(a)

1

1

--

no

no

no

no

no

240429-DropSpaces

Perez_5-1714443552201.png

 

@LucMeekes 

clean(a,”b”)

1

2

--

yes

no

no

no

yes

 

 

@Perez 

cleanstr2(a)

10

1

--

yes

yes

no

no

no

PE_20240428_cleanstr(a)

(9f95e1a7c7)

 Perez_6-1714443552341.png

 

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.

Perez
12-Amethyst
(To:Perez)

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:  

Perez_0-1714876339415.png
@LucMeekes 

cleanstr(str)

1

1

213

yes

yes

yes

yes

yes

LM_20240429_Cleanstr

Perez_1-1714876339529.png
@Werner_E 

dropSpaces3(a)

1

1

135

yes

yes

yes

yes

no

240429-DropSpaces v3

Perez_2-1714876339426.png

@terryhendicott 

dropSpaces2(a)

1

1

149

yes

yes

yes

no

no

240429-DropSpaces v2

Perez_3-1714876339419.png

@Werner_E 

cleanstr_WE2(a)

4

1

--

yes

yes

no

no

no

WE_20240428(2)_cleanstr(a)

(Perez123)

Perez_4-1714876339533.png

@Werner_E 

cleanstr_WE4(a)

1

1

--

yes

yes

no

no

no

WE_20240428(2)_cleanstr(a)

(Perez123)

Perez_5-1714876339431.png

@terryhendicott 

dropSpaces(a)

1

1

--

no

no

no

no

no

240429-DropSpaces

Perez_6-1714876339259.png

@LucMeekes 

clean(a,”b”)

1

2

--

yes

no

no

no

yes

 

 

@Perez 

cleanstr2(a)

10

1

--

yes

yes

no

no

no

PE_20240428_cleanstr(a)

(9f95e1a7c7)

 Perez_7-1714876339515.png
Top Tags